View difference between Paste ID: GQFvRRJ7 and RKq3CM4T
SHOW: | | - or go back to the newest paste.
1
package Zadachiche;
2
3
4
5
6
import java.io.BufferedReader;
7
import java.io.IOException;
8
import java.io.InputStreamReader;
9
import java.util.Iterator;
10
import java.util.NoSuchElementException;
11
12
13
14
class SLLNode<E extends Comparable> {
15
    public E element;
16
    public SLLNode<E> succ;
17
18
    public SLLNode(E elem, SLLNode<E> succ) {
19
        this.element = elem;
20
        this.succ = succ;
21
    }
22
23
    @Override
24
    public String toString() {
25
        return element.toString();
26
    }
27
}
28
29
class SLL<E extends Comparable> {
30
    SLLNode<E> first;
31
32
    public SLL() {
33
        // Construct an empty SLL
34
        this.first = null;
35
    }
36
37
    public void deleteList() {
38
        first = null;
39
    }
40
41
    public int length() {
42
        int ret;
43
        if (first != null) {
44
            SLLNode<E> tmp = first;
45
            ret = 1;
46
            while (tmp.succ != null) {
47
                tmp = tmp.succ;
48
                ret++;
49
            }
50
            return ret;
51
        } else
52
            return 0;
53
54
    }
55
56
    @Override
57
    public String toString() {
58
        String ret = new String();
59
        if (first != null) {
60
            SLLNode<E> tmp = first;
61
            ret += tmp + "->";
62
            while (tmp.succ != null) {
63
                tmp = tmp.succ;
64
                ret += tmp + "->";
65
            }
66
        } else
67
            ret = "Prazna lista!!!";
68
        return ret;
69
    }
70
71
    public void insertFirst(E o) {
72
        SLLNode<E> ins = new SLLNode<E>(o, first);
73
        first = ins;
74
    }
75
76
    public void insertAfter(E o, SLLNode<E> node) {
77
        if (node != null) {
78
            SLLNode<E> ins = new SLLNode<E>(o, node.succ);
79
            node.succ = ins;
80
        } else {
81
            System.out.println("Dadenot jazol e null");
82
        }
83
    }
84
85
    public void insertBefore(E o, SLLNode<E> before) {
86
87
        if (first != null) {
88
            SLLNode<E> tmp = first;
89
            if(first==before) {
90
                this.insertFirst(o);
91
                return;
92
            }
93
            //ako first!=before
94
            while (tmp.succ != before)
95
                tmp = tmp.succ;
96
            if (tmp.succ == before) {
97
                SLLNode<E> ins = new SLLNode<E>(o, before);
98
                tmp.succ = ins;
99
            } else {
100
                System.out.println("Elementot ne postoi vo listata");
101
            }
102
        } else {
103
            System.out.println("Listata e prazna");
104
        }
105
    }
106
107
    public void insertLast(E o) {
108
        if (first != null) {
109
            SLLNode<E> tmp = first;
110
            while (tmp.succ != null)
111
                tmp = tmp.succ;
112
            SLLNode<E> ins = new SLLNode<E>(o, null);
113
            tmp.succ = ins;
114
        } else {
115
            insertFirst(o);
116
        }
117
    }
118
119
    public E deleteFirst() {
120
        if (first != null) {
121
            SLLNode<E> tmp = first;
122
            first = first.succ;
123
            return tmp.element;
124
        } else {
125
            System.out.println("Listata e prazna");
126
            return null;
127
        }
128
    }
129
130
    public E delete(SLLNode<E> node) {
131
        if (first != null) {
132
            SLLNode<E> tmp = first;
133
            if(first ==node) {
134
                return this.deleteFirst();
135
            }
136
            while (tmp.succ != node&&tmp.succ.succ != null)
137
                tmp = tmp.succ;
138
            if (tmp.succ == node) {
139
                tmp.succ = tmp.succ.succ;
140
                return node.element;
141
            } else {
142
                System.out.println("Elementot ne postoi vo listata");
143
                return null;
144
            }
145
        } else {
146
            System.out.println("Listata e prazna");
147
            return null;
148
        }
149
150
    }
151
152
    public SLLNode<E> getFirst() {
153
        return first;
154
    }
155
    
156
    public SLLNode<E> getLast(){
157
    	SLLNode<E> temp=first;
158
    	while(temp.succ!=null) {
159
    		temp=temp.succ;
160
    	}
161
    	return temp;
162
    }
163
164
    public SLLNode<E> find(E o) {
165
        if (first != null) {
166
            SLLNode<E> tmp = first;
167
            while (tmp.element != o&&tmp.succ != null)
168
                tmp = tmp.succ;
169
            if (tmp.element == o) {
170
                return tmp;
171
            } else {
172
                System.out.println("Elementot ne postoi vo listata");
173
            }
174
        } else {
175
            System.out.println("Listata e prazna");
176
        }
177
        return first;
178
    }
179
180
    public Iterator<E> iterator () {
181
        // Return an iterator that visits all elements of this list, in left-to-right order.
182
        return new LRIterator<E>();
183
    }
184
185
    // //////////Inner class ////////////
186
187
    private class LRIterator<E extends Comparable> implements Iterator<E> {
188
189
        private SLLNode<E> place, curr;
190
191
        private LRIterator() {
192
            place = (SLLNode<E>) first;
193
            curr = null;
194
        }
195
196
        public boolean hasNext() {
197
            return (place != null);
198
        }
199
200
        public E next() {
201
            if (place == null)
202
                throw new NoSuchElementException();
203
            E nextElem = place.element;
204
            curr = place;
205
            place = place.succ;
206
            return nextElem;
207
        }
208
209
        public void remove() {
210
            //Not implemented
211
        }
212
    }
213
    public SLL<E> joinLists(SLL<E> l2) {
214
        SLLNode<E> d1 = this.getFirst();//this e referenca na lista1
215
        SLLNode<E> d2 = l2.getFirst();
216
217
        SLL<E> result = new SLL();
218
219
220
        while(d1!=null && d2!=null) {
221
            if(d1.element.compareTo(d2.element)<0) {
222
                result.insertLast(d1.element);
223
                d1 = d1.succ;
224
            } else if(d1.element.compareTo(d2.element)>0) {
225
                result.insertLast(d2.element);
226
                d2 = d2.succ;
227
            } else {
228
                result.insertLast(d1.element);
229
                d1 = d1.succ;
230
                d2 = d2.succ;
231
            }
232
        }
233
        while(d1!=null) {
234
            result.insertLast(d1.element);
235
            d1 = d1.succ;
236
        }
237
        while(d2!=null) {
238
            result.insertLast(d2.element);
239
            d2 = d2.succ;
240
        }
241
        return result;
242
    }
243
244
245
}
246
247
248
public class hehe {
249
	
250
	public static void presmetaj(SLL<Integer> lista,Integer N) {
251
		
252
		
253
		SLLNode<Integer> dvizi=lista.getFirst();
254
		
255
		
256
		
257
		while(N/2-1!=0) {
258
			
259
			SLLNode<Integer> posleden=lista.getLast();
260
			lista.insertAfter(lista.getLast().element, dvizi);	
261
			if(dvizi.succ.succ!=null) {
262
				dvizi=dvizi.succ.succ;
263
				lista.delete(posleden);
264
265
				
266
				
267
			}
268
			
269
	
270
		N--;
271
		}
272
		System.out.print(lista);
273
		
274
}
275
	
276
277
	public static void main(String[] args) throws IOException {
278
		
279
		SLL<Integer> lista=new SLL<>();
280
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
281
		int N=Integer.parseInt(br.readLine());
282
		String line=br.readLine();
283
		String[] pomniza=line.split(" ");
284
		for(int i=0;i<pomniza.length;i++) {
285
			lista.insertLast(Integer.parseInt(pomniza[i]));
286
		}
287
		
288
		
289
		presmetaj(lista,N);
290
		
291
292
	}
293
294
}