-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDoubleLinkedList_GFG.java
More file actions
230 lines (199 loc) · 4.78 KB
/
Copy pathDoubleLinkedList_GFG.java
File metadata and controls
230 lines (199 loc) · 4.78 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
package DATA_STRUCTURE;
public class DoubleLinkedList_GFG {
static class Node{
int data;
Node pre;
Node next;
public Node(int d) {
data = d;
}
}
//creating two linking part
Node head,tail = null;
public void add(int d){ //adding nodes
Node new_node = new Node(d);
if(head == null){
head = tail = new_node;
head.pre = null;
tail.next = null;
}
else{
tail.next = new_node;
new_node.pre = tail;
tail = new_node;
tail.next = null;
}
}
//displaying forward
public void display_forward(){
Node n = head;
while(n!=null){
System.out.print(" "+n.data);
n = n.next;
}
}
//displaying backward
public void display_backward(){
Node n = tail;
while(n!=null){
System.out.print(n.data+" ");
n = n.pre;
}
}
//counting Nodes
public int count(){
Node n = head;
int p = 0;
while(n!=null){
p++;
n = n.next;
}
System.out.println("Total number of Nodes is "+p);
return p;
}
//searching ing Dll
public void search(int e){
Node n = head;
int p = 0;
while(n!=null){
p++;
if(n.data == e){
System.out.println(e+" is in NODE "+p);
return;
}
n = n.next;
}
System.out.println("Element not found");
}
//Inserting a Node in front
public void Insert_start(int d){
Node n = new Node(d);
if (head == null){
head = tail = n;
}
else{
n.next = head;
head.pre = n;
head = n;
}
}
//Inserting a Node at end
public void Insert_end(int d){
Node n = new Node(d);
if(head == null){
head = tail = n;
}
else{
tail.next = n;
n.pre = tail;
tail = n;
}
}
//Inserting a Node anywhere
public void Insert_anywhere(int d,int p){
Node n = new Node(d);
Node current = head;
Node previous = null;
int pos = 0;
if(head == null){
Insert_start(d);
}
else if(p == count()+1){
Insert_end(d);
}
else{
while(pos<p){
previous = current;
current = current.next;
if (current.next == null){
break;
}
pos++;
}
previous.next = n;
n.pre = previous;
n.next = current;
current.pre = n;
}
}
//delete from start
public void Delete_start(){
if(head == null){
System.out.println("Underflow");
return;
}
if(head.next == null){
head = tail = null;
}
else{
head = head.next;
head.pre = null;
}
}
//Delete from end
public void Delete_end(){
if(head == null){
System.out.println("Underflow");
return;
}
if(head.next == null){
head = tail = null;
}
else{
tail = tail.pre;
tail.next = null;
}
}
//Delete anywhere
public void Delete_Anywhere(int p){
int i = 0;
Node current = head;
Node previous = null;
if(current==null){
System.out.println("Underflow");
return;
}
if(head.next == null){
Delete_start();
}
else if(p == count()){
Delete_end();
}
else{
while(i<p){
previous = current;
current = current.next;
if(current == null){
break;
}
i++;
}
previous.next = current.next;
current.next.pre = previous;
}
}
public static void main(String[] args) {
DoubleLinkedList_GFG dll = new DoubleLinkedList_GFG();
dll.add(1);
dll.add(2);
dll.add(3);
dll.add(4);
dll.add(5);
dll.add(6);
dll.add(7);
dll.add(8);
dll.Insert_start(0);
dll.Insert_end(9);
dll.Insert_anywhere(100,5);
// dll.Delete_start();
// dll.Delete_end();
dll.Delete_Anywhere(5);
dll.display_forward();
System.out.println("\n\n");
dll.display_backward();
// System.out.println("\n");
// dll.count();
// System.out.println("\n");
// dll.search(4);
}
}