-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoubleLinkedList.java
More file actions
355 lines (340 loc) · 9.6 KB
/
Copy pathDoubleLinkedList.java
File metadata and controls
355 lines (340 loc) · 9.6 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
interface ILinkedList {
/**
* Inserts a specified element at the specified position in the list.
* @param index
* @param element
*/
public void add(int index, Object element);
/**
* Inserts the specified element at the end of the list.
* @param element
*/
public void add(Object element);
/**
* @param index
* @return the element at the specified position in this list.
*/
public Object get(int index);
/**
* Replaces the element at the specified position in this list with the
* specified element.
* @param index
* @param element
*/
public void set(int index, Object element);
/**
* Removes all of the elements from this list.
*/
public void clear();
/**
* @return true if this list contains no elements.
*/
public boolean isEmpty();
/**
* Removes the element at the specified position in this list.
* @param index
*/
public void remove(int index);
/**
* @return the number of elements in this list.
*/
public int size();
/**
* @param fromIndex
* @param toIndex
* @return a view of the portion of this list between the specified fromIndex and toIndex, inclusively.
*/
public ILinkedList sublist(int fromIndex, int toIndex);
/**
* @param o
* @return true if this list contains an element with the same value as the specified element.
*/
public boolean contains(Object o);
}
public class DoubleLinkedList implements ILinkedList{
public class SNode{
Object data;
SNode next;
SNode prev;
}
SNode head = null;
SNode tail = null;
int size = 0;
public void add(Object element){
if(isEmpty()){
SNode tmp = new SNode();
tmp.data = (int) element ;
//head.prev=null;
tail = head;
head = tmp;
tail = tmp;
// size++;
}
else{
SNode tmp = new SNode();
tmp.data = (int) element;
tail.next = tmp;
tail = tmp;
// size++;
}
}
public void print(){
SNode h = head ;
while (h != null){
if (h.next != null)
System.out.print(h.data + ", ");
else
System.out.print(h.data);
h.prev=h;
h = h.next;
}
}
public void clear(){
head = null;
tail= null;
size = 0;
print();
}
public boolean contains(Object element){
SNode h = head;
while (h != null && !isEmpty()){
if (h.data == element){
return true;
}
h.prev=h;
h= h.next;
}
return false;
}
public boolean isEmpty(){
if(head == null) {return true;}
else{return false;}
}
public Object get(int index){
SNode ptr=head;
for(int i=0 ; i< index ; i++)
{
ptr.prev=ptr;
ptr=ptr.next;
}
return ptr.data;
}
public int size(){
int count = 0;
if (isEmpty()) {
count = 0;
} else {
SNode current = head;
while (current != null) {
count++;
current.prev=current;
current = current.next;
}
}
return count;
}
public void remove(int index){
SNode temp = head;
SNode nll = null;
if (index == 0)
{
if(size()==1){
System.out.print("");
}
else{
head = head.next;
head.prev=null;
//head.next=null;
print();}
}
else{
for (int i=0;i<index; i++){
nll = temp;
temp.prev=temp;
temp = temp.next;
}
nll.next = temp.next;
nll.prev = temp.prev;
temp.next = null;
print();}
}
public void insertAtStart(Object data)
{
SNode node = new SNode();
node.data = data;
node.prev = null;
node.next = head;
head = node;
}
public void add(int index, Object data){
SNode node = new SNode();
node.data = data;
node.next = null;
if (index > size()){
System.out.print("Error");
}
if(index==0)
{
insertAtStart(data);
}
else{
SNode n = head;
for(int i=0;i<index-1;i++)
{
n.prev=n;
n = n.next;
}
node.next = n.next;
n.next = node;
}
}
public DoubleLinkedList sublist(int fromIndex, int toIndex){
SNode current = head;
SNode t = null;
DoubleLinkedList listout = new DoubleLinkedList();
int count = 0;
if (head == null)
{
System.out.println("Error");
System.exit(0);
}
while (count < fromIndex) {
current.prev=current;
current = current.next;
count++;
}
while (count <= toIndex) {
t = current;
t.data = current.data;
listout.add(t.data);
current.prev=current;
current = current.next;
count++;
}
return listout;
}
public void set(int index, Object element){
SNode h = head;
int count = 0;
if (isEmpty()) {
System.out.print("Error");
} else if (index > size() - 1) {
System.out.print("Error");
} else {
while (count < index) {
h.prev=h;
h = h.next;
count++;
}
h.data = (int) element;
}
}
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. */
DoubleLinkedList LL = new DoubleLinkedList ();
Scanner sc = new Scanner(System.in);
String fun;
String sin = sc.nextLine().replaceAll("\\[|\\]", "");
String[] s = sin.split(", " );
int i = 0;
if (s.length == 1 && s[0].isEmpty() ) {
LL.clear();
}
else{
while (i <= s.length - 1){
LL.add(Integer.parseInt(s[i]));
i++;
}
}
fun = sc.nextLine();
switch (fun){
case "add" :
int el0 = sc.nextInt();
System.out.print("[");
// for(i = 0; i < s.length; ++i) {
// System.out.print(s[i]);
// if(i != s.length - 1)
// System.out.print(", ");
// }
LL.add(el0);
LL.print();
System.out.print("]");
break;
case "contains":
int el1 = sc.nextInt();
if(LL.contains(el1))
System.out.println("True");
else
System.out.println("False");
break;
case "isEmpty" :
if(LL.isEmpty())
System.out.println("True");
else
System.out.println("False");
break;
case "clear" :
System.out.print("[");
LL.clear();
System.out.print("]");
break;
case "get" :
int index = sc.nextInt();
if(LL.isEmpty()||index >LL.size()- 1||index<0)
System.out.println("Error");
else
System.out.print(LL.get(index));
break;
case "remove" :
int index1 = sc.nextInt();
if (index1 >= LL.size() || index1<0 || LL.isEmpty())
System.out.println("Error");
else{
System.out.print("[");
LL.remove(index1);
System.out.print("]");
}
break;
case "size" :
System.out.println(LL.size());
break;
case "addToIndex" :
int index2 = sc.nextInt();
Object el2 = sc.nextInt();
if (index2>LL.size()-1||index2<0){
System.out.print("Error");
break;
}
System.out.print("[");
LL.add(index2,el2);
LL.print();
System.out.print("]");
break;
case "set" :
int index3 = sc.nextInt();
int el3 = sc.nextInt();
if (index3>LL.size()-1||index3<0){
System.out.print("Error");
break;
}
System.out.print("[");
LL.set(index3,el3);
LL.print();
System.out.print("]");
break;
case "sublist" :
int in=sc.nextInt();
int it=sc.nextInt();
DoubleLinkedList sub = new DoubleLinkedList();
if(LL.isEmpty()||in>LL.size()- 1 || it>LL.size()- 1||in>it||in<0||it<0 )
{System.out.print("Error");
break;}
else{
sub = LL.sublist(in,it);
System.out.print("[");
sub.print();
System.out.print("]");
break;}
}
}
}