-
Notifications
You must be signed in to change notification settings - Fork 0
/
DoublyLinkedList.java
144 lines (123 loc) · 4.06 KB
/
DoublyLinkedList.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
/** @author Utkarsh Gandhi and Achyut Bhandiwad
* Doubly linked list
* Ver 1.0
*/
package usg170030;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class DoublyLinkedList<T> extends SinglyLinkedList<T> {
static class Entry<E> extends SinglyLinkedList.Entry<E> {
Entry<E> prev;
Entry(E x, Entry<E> next, Entry<E> prev) {
super(x, next);
this.prev = prev;
}
}
public DoublyLinkedList() {
head = new Entry<>(null,null, null);
tail = head;
size = 0;
}
public interface DoublyItr<T> {
boolean hasPrev();
boolean hasNext();
void remove();
void add(T ent);
T next();
T prev();
}
public DLLIterator dllIterator() { return new DLLIterator(); }
public class DLLIterator extends SLLIterator implements DoublyItr<T>{
DLLIterator(){
super();
}
@Override
public void remove(){
if (ready == false)
throw new NoSuchElementException();
if (cursor != tail)
((Entry)cursor.next).prev = ((Entry)cursor).prev;
super.remove();
return;
}
public boolean hasPrev() {
if (((Entry)cursor).prev == head || ((Entry)cursor).prev == null ){
throw new NoSuchElementException();
}
return true;
}
public T prev() {
cursor = ((Entry)cursor).prev;
ready = true;
if(cursor == head)
ready = false;
prev=((Entry) prev).prev;
return cursor.element;
}
// Add new elements
public void add(T x) {
add(new Entry<>(x, null, null));
}
public void add(Entry<T> ent) {
ent.next = cursor.next;
cursor.next = ent ;
ent.prev = ((Entry)cursor);
prev = cursor;
if (cursor != tail)
((Entry<T>) ent.next).prev = ent;
else
tail = ent;
cursor = cursor.next;
ready = false;
size++;
}
}
@Override
public void add(T x) {
add(new Entry<T>(x, null, (Entry<T>) tail));
}
public static void main(String[] args) throws NoSuchElementException {
DoublyLinkedList<Integer> lst = new DoublyLinkedList<>();
DoublyItr d1 = lst.dllIterator();
// adding elements in the Linked list
lst.add(10);
lst.add(12);
lst.add(13);
lst.add(14);
lst.add(15);
Scanner in = new Scanner(System.in);
whileloop:
while(in.hasNext()) {
int com = in.nextInt();
switch(com) {
case 1: // Move to next element and print it
if (d1.hasNext()) {
System.out.println( d1.next());
} else {
break whileloop;
}
break;
case 2: // insert x before the element that will be returned by a call to next()
System.out.println("Enter the value of element:");
int val = in.nextInt();
d1.add(Integer.valueOf(val));
lst.printList();
break;
case 3: // remove element and print the linked list
d1.remove();
lst.printList();
break;
case 4: // Move to previous element and print it
if (d1.hasPrev())
System.out.println(d1.prev());
else
break whileloop;
break;
case 5: // print list
lst.printList();
default: // Exit loop
break whileloop;
}
}
}
}