-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoubleLinkedList.java
366 lines (348 loc) · 9.46 KB
/
doubleLinkedList.java
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
356
357
358
359
360
361
362
363
364
365
366
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
interface ILinkedList {
public void printList();
/**
* 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 {
/* Implement your linked list class here*/
public int length = 0;
public static boolean flag;
public class Node {
Object item;
Node next;
Node prev;
}
public Node head = null;
public Node tail = null;
public void printList() {
Node current = head;
if(current == null) {
System.out.println("[]");
}
else {
System.out.print("[" + current.item);
current = current.next;
while (current != null) {
System.out.print(", " + current.item);
current = current.next;
}
System.out.println("]");
}
}
public void insertFirst(Object element) {
Node newNode = new Node();
newNode.item = element;
if (length == 0) {
newNode.next =newNode.prev= null;
head = tail = newNode;
}
else {
newNode.next = head;
newNode.prev =null;
head = newNode;
}
length++;
}
public void add(int index, Object element) {
flag = true;
if(index < 0 || index > length) {
flag = false;
}
else {
Node newNode = new Node();
newNode.item = element;
if(index == 0) {
insertFirst(element);
}
else if (index == length) {
add(element);
}
else {
Node current = head;
for(int i = 1; i < index; i++) {
current = current.next;
}
//warnnnnnnnnnn
newNode.next = current.next;
newNode.prev= current;
current.next.prev =newNode;
current.next = newNode;
length++;
}
}
}
//insert last
public void add(Object element) {
Node newNode = new Node();
newNode.item = element;
if (length == 0) {
newNode.next =newNode.prev= null;
head = tail = newNode;
}
else {
newNode.next = null;
newNode.prev =tail;
tail.next = newNode;
tail = newNode;
}
length++;
}
public Object get(int index) {
if(index < 0 || index >= length) {
return 0;
}
else if(index == 0) {
return head.item;
}
else if(index == length - 1) {
return tail.item;
}
else {
Node current = head;
for(int i = 0; i < index; i++) {
current = current.next;
}
return current.item;
}
}
public void set(int index, Object element) {
Node current = head;
flag = true;
if(index < 0 || index >= length) {
flag = false;
}
else if(index == 0) {
head.item = element;
}
else if(index == length - 1) {
tail.item = element;
}
else {
for(int i = 0; i < index; i++) {
current = current.next;
}
current.item = element;
}
}
public void clear() {
head = tail = null;
length = 0;
}
public boolean isEmpty() {
if (length == 0) {
return true;
}
else {
return false;
}
}
public void remove(int index) {
flag = true;
Node current = head;
if(index < 0 || index >= length) {
flag = false;
}
else if(length == 1) {
head = tail = null;
length--;
}
else if (index == 0) {
head = head.next;
head.prev=null;
length--;
}
else if (index == length - 1) {
for(int i = 0; i < index - 1; i++) {
current = current.next;
}
current.next = null;
tail = current;
length--;
}
else {
for(int i = 0; i < index - 1; i++) {
current = current.next;
}
current.next = current.next.next;
current.next.next.prev= current;
length--;
}
}
public int size() {
return length;
}
public ILinkedList sublist(int fromIndex, int toIndex) {
ILinkedList sub = new DoubleLinkedList();
if(fromIndex < 0 || fromIndex >= length || toIndex < 0 || toIndex >= length || fromIndex > toIndex || isEmpty()){
return null;
}
else {
for (int i = fromIndex; i <= toIndex; i++) {
sub.add(get(i));
}
return sub;
}
}
public boolean contains(Object o) {
Node current = head;
while(current != null) {
if(current.item == o) {
return true;
}
current = current.next;
}
return false;
}
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. */
DoubleLinkedList list = new DoubleLinkedList();
Scanner sc = new Scanner(System.in);
String st = sc.nextLine().replaceAll("\\[|\\]", "");
String[] s = st.split(", ");
if (!(s.length == 1 && s[0].isEmpty())){
for(int i=0; i < s.length; i++){
Object o = Integer.parseInt(s[i]);
list.add(o);
}
}
String keyword = sc.nextLine();
if(Objects.equals(keyword, "add")){
int element = sc.nextInt();
list.add(element);
list.printList();
}
if(Objects.equals(keyword, "addToIndex")){
int index = sc.nextInt();
int element =sc.nextInt();
list.add(index, element);
if(flag == true) {
list.printList();
}
else {
System.out.println("Error");
}
}
if(Objects.equals(keyword, "get")){
int index = sc.nextInt();
Object res = list.get(index);
if((int)res == 0) {
System.out.println("Error");
}
else {
System.out.println(res);
}
}
if(Objects.equals(keyword, "set")) {
int index = sc.nextInt();
int element =sc.nextInt();
list.set(index, element);
if(flag == false){
System.out.println("Error");
}
else {
list.printList();
}
}
if(Objects.equals(keyword, "clear")) {
list.clear();
list.printList();
}
if(Objects.equals(keyword, "isEmpty")) {
boolean res = list.isEmpty();
if(res == true) {
System.out.println("True");
}
else {
System.out.println("False");
}
}
if(Objects.equals(keyword, "remove")) {
int index = sc.nextInt();
list.remove(index);
if(flag == false) {
System.out.println("Error");
}
else {
list.printList();
}
}
if(Objects.equals(keyword, "size")){
System.out.println(list.size());
}
if(Objects.equals(keyword, "sublist")) {
int fromIndex = sc.nextInt();
int toIndex = sc.nextInt();
ILinkedList sub = list.sublist(fromIndex, toIndex);
if(sub == null){
System.out.println("Error");
}
else{
sub.printList();
}
}
if(Objects.equals(keyword, "contains")) {
int element =sc.nextInt();
boolean res = list.contains(element);
if(res == true) {
System.out.println("True");
}
else {
System.out.println("False");
}
}
}
}