1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import java.util.*;

// use ListNode to represent the balls.
class ListNode{

    protected int element;
    protected ListNode next= null;
    protected ListNode prev= null;

    public ListNode(int item, ListNode prev, ListNode next) {
        this.element = item;
        this.prev = prev;
        this.next = next;
    }

    public ListNode(int item) {
        this.element = item;
    }

    public int getElement(){return this.element;}
    public ListNode getNext(){return next;} 
    public ListNode getPrev(){return prev;} 

}

class Result{

    // declare the member field
    private ListNode head = null;
    private ListNode tail = null;
    private ListNode current = null;
    private ListNode a = null;
    private int num_codes;

    // declare the constructor
    public Result(int num){
        num_codes= num;
        System.out.println(num_codes);
        for(int i=1; i<num_codes+1;i++){
            addLast(i,num_codes);
            /**        for(Listnode n = head; n!= null; n=n.next){
              addLast(n);**/
        }
        }


        public void print() throws NoSuchElementException {
            if (head == null)
                throw new NoSuchElementException("Nothing to print...");

            ListNode ln = head;
            System.out.printf("List is: ");
            System.out.println(ln + " : "+ ln.getElement());
            for (int i=1; i < num_codes; i++) {
                ln = ln.next;
                System.out.print(ln);
                System.out.println(" : " + ln.getElement());
            }
            System.out.println(".");
        }


        public void addLast(int i,int num_codes){
            if(head!=null){

                if(i==num_codes){

                    tail.next = new ListNode(i, null, null);
                    tail = tail.next;
                }else{
                    System.out.print(" body ");
                    tail.next = new ListNode(i, null,tail);
                    tail = tail.next;}
            }else{// nothing in head
                System.out.print(" head ");
                head = new ListNode(i,null,null);// nothing before and after
                tail = head;
            }
            num_codes++;
        }

        public boolean contains(int item) {
            for (ListNode n = head; n!= null; n=n.next)
                if (n.getElement()==(item)) 
                    return true;
            return false;
        }

        /* implement the operation for A
         *     PRE-Condition  :
         *     POST-Condition :
         */  
        public void doA(int x, int y) {
            ListNode current= null;
            ListNode a = null;

            for(ListNode ln=head;ln!= null;ln=ln.next){
                System.out.println(" iteration " + ln.getElement());    
                if((ln.getElement())==(y)){
                    current  =ln;
                }

                if((ln.getElement())==(x)){
                    a = ln;
                }}

                //implement operation

                //If already to the left to right
                if((x+1)==y){
                    ;
                }else{// arrange it.
                    if(current!= null){
                        ListNode temp;
                        if(current.prev!=null){
                            temp = current.prev;
                            current.prev.next = a;
                            current.prev = a;
                            a.next = current;
                            a.prev= temp;
                            print();
                        }else{//if current is head

                            if(current.prev==null){
                                current.prev = a;
                                head = a;
                                head.prev= null;
                                print();
                            }

                            }}}
        }


        /* implement the operation for B
         *     PRE-Condition  :
         *     POST-Condition :
         */  
        public void doB(int x, int y) {

        }

        /* implement the operation for R
         *     PRE-Condition  :
         *     POST-Condition :
         */
        public void doR(int x) {

            ListNode reme=null;
            for(ListNode m = head; m!=null; m=m.next){

                if(m.getElement()==x){
                    reme = m;
                }}
          
            //execution of removing
            if(reme == null){
                ;  //throw new NoSuchElementExeption("remove fails");
            }else{
              num_codes--;
                if(reme.prev!=null){                  
                    reme.next.prev= reme.prev;
                }else{ //its a head
                    head = reme.next;
                    reme.prev= null;
                    
                }

                if(reme.next!=null){
                    
                    reme.prev.next= reme.next;
                }else{//its a tail
                    tail=reme.prev;
                    tail.next= null;

                }
            }
            
        }
    }

    public class Balls {

        public static void main(String[] args) {

            // declare the necessary variables    
            int num, ops;
            String task;

            // create new object from class Result;
            Result b1;

            // declare a Scanner object to read input
            Scanner myScanner = new Scanner(System.in);

            // read input and process them accordingly
            num = myScanner.nextInt();
            b1= new Result(num);
            b1.print();
            ops = myScanner.nextInt();

            for(int i=0;i<ops;i++){
                task = myScanner.next();
                if(task.equals("A")){
                    System.out.println("A");
                    b1.doA(myScanner.nextInt(),myScanner.nextInt());
                    b1.print();
                }
                else if (task.equals("B")){
                    System.out.println("B");
                    b1.doB(myScanner.nextInt(), myScanner.nextInt());
                }
                else if(task.equals("R")){
                    System.out.println("R");
                    b1.doR(myScanner.nextInt());
                }
            }
        }
    }