-
Notifications
You must be signed in to change notification settings - Fork 0
/
DoublyLL.java
176 lines (158 loc) · 4 KB
/
DoublyLL.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
class LL{
private Node head;
private Node tail;
private int size;
private class Node{
private int value;
private Node next;
private Node prev;
public Node(int value) {
this.value = value;
}
public Node(int value, Node next, Node prev) {
this.value = value;
this.next = next;
this.prev = prev;
}
}
public LL(){
this.size = 0;
}
public void insertfirst(int value){
Node node = new Node(value);
node.next = head;
node.prev = null;
if(head!=null){
head.prev = node;
}
head = node;
if (tail==null){
tail = head;
}
size +=1;
}
public void insertlast(int value){
// Node node = new Node(value);
// tail.next = node;
// node.prev = tail;
// if(tail!=null){
// tail = node;
// }
// size +=1;
Node node = new Node(value);
Node last = head;
node.next = null;
if(head == null){
node.prev = null;
head = node;
}
while(last.next!=null){
last = last.next;
}
last.next = node;
node.prev = last;
}
public int deletelast(){
if(size<=1){
return deletefirst();
}
Node second_last = get(size - 2);
int val = tail.value;
tail = second_last;
tail.next = null;
return val;
}
public Node find(int value){
Node node = head;
while(node!=null){
if(node.value==value){
return node;
}
node = node.next;
}
return null;
}
public Node get(int index){
Node node = head;
for(int i=0; i< index;i++){
node = node.next;
}
return node;
}
public void insert(int after, int value){
Node prev = find(after);
if(prev==null){
System.out.println("Does not exist");
return;
}
Node node = new Node(value);
node.next = prev.next;
prev.next = node;
node.prev = prev;
if(node.next!=null){
node.next.prev = node;
}
}
public int deletefirst(){
int val = head.value;
head = head.next;
if(head == null){
tail = null;
}
return val;
}
public int delete(int index){
if(index == 0){
return deletefirst();
}
if(index == size-1){
return deletelast();
}
Node prev = get(index-1);
int val = prev.next.value;
prev.next = prev.next.next;
return val;
}
public void display(){
Node node = head;
while (node != null){
System.out.print(node.value + " -> ");
node = node.next;
}
System.out.println("END");
}
public void displayrev(){
Node node = head;
Node last = null;
while (node != null){
last = node;
node = node.next;
}
while(last!=null){
System.out.print(last.value + " -> ");
last = last.prev;
}
System.out.println("START");
}
}
public class DoublyLL{
public static void main(String[] args) {
LL list = new LL();
list.insertfirst(0);
list.insertfirst(1);
list.insertfirst(2);
list.insertfirst(3);
list.insertlast(-1);
list.display();
list.displayrev();
list.insert(2, 8);
list.display();
// System.out.println(list.deletefirst());
// list.display();
// System.out.println(list.deletelast());
// list.display();
// System.out.println(list.delete(2));
// list.display();
// System.out.println(list.find(1));
}
}