-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoublyLinkedList
More file actions
215 lines (152 loc) · 3.43 KB
/
doublyLinkedList
File metadata and controls
215 lines (152 loc) · 3.43 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
import java.awt.print.PrinterGraphics;
public class DlinkedList {
class Node{
int data;
Node next;
Node prev;
Node(int data){
this.data = data;
}
}
public Node head = null;
public Node tail = null;
// adding the node
public void addNode(int data) {
Node newNode = new Node(data);
if(head == null) {
head = newNode;
}else {
tail.next = newNode;
newNode.prev = tail;
}
tail = newNode;
}
// method to delete a node;
public void delete(int data) {
Node temp = head , temp1 = null, temp2 = null;
if(temp.data == data ) {
head = temp.next;
return;
}
while(temp != null && temp.data != data) {
temp1 = temp;
temp = temp.next;
temp2 = temp.next;
}
if(temp == null) {
return;
}if(temp== tail) {
tail = temp2;
tail.next = null;
return;
}
temp2.prev = temp.prev;
temp1.next = temp.next;
}
// inserting a data after a element is the Node;
public void insertAfter(int nextTo , int data) {
Node newNode = new Node(data);
Node temp = head;
//if the nextTo is head;
if(temp.data == nextTo ) {
newNode.next = temp.next;
newNode.prev = temp;
temp.next.prev = newNode;
temp.next = newNode;
return;
}
//for finding the nextTo we use traverse;
while(temp != null && temp.data != nextTo) {
temp = temp.next;
}
//case : if the element is not found;
if(temp == null) {
System.out.println("the nextTo element not found");
return;
}
//the nextTo is find at the tail;
if(temp == tail) {
tail.next = newNode;
newNode.prev = tail;
tail = newNode;
tail.next = null;
return;
}
newNode.next = temp.next;
newNode.prev = temp;
temp.next.prev = newNode;
temp.next = newNode;
return;
}
//insertBefore;
public void insertBefore(int target, int data) {
Node newNode = new Node(data);
Node temp = head;
//case if the target is head;
if(temp.data == target) {
head = newNode;
newNode.prev= null;
newNode.next = temp;
temp.prev = newNode;
return;
}
// traverse;
while(temp != null && temp.data != target) {
temp= temp.next;
}
// if null
if(temp== null) {
System.out.println("the target is not found");
return;
}
// the case: we found the target at somewhere;
newNode.prev = temp.prev;
newNode.next = temp;
temp.prev.next = newNode;
temp.prev = newNode;
}
// display node;
public void display() {
Node temp = head;
if(head == null) {
System.out.println(" List is empty");
return;
}
while(temp != null) {
System.out.println(temp.data);
temp= temp.next;
}
}
//display reverse
public void displayReverse() {
Node temp = tail;
if(tail == null) {
System.out.println(" List is empty");
return;
}
while(temp != null) {
System.out.println(temp.data);
temp= temp.prev;
}
}
// main method
public static void main(String [] args) {
DlinkedList list = new DlinkedList();
list.addNode(10);
list.addNode(20);
list.addNode(50);
list.addNode(46);
list.addNode(32);
list.addNode(45);
// list.display();
// list.displayReverse();
list.delete(46);
// list.displayReverse();
// list.display();
list.insertBefore(10, 11);
list.insertBefore(45, 46);
list.insertBefore(50, 51);
list.display();
// list.displayReverse();
}
}