-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoubleLinkedList.java
235 lines (206 loc) · 3.89 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
/*
Copyright (C) Deepali Srivastava - All Rights Reserved
This code is part of DSA course available on CourseGalaxy.com
*/
package doubleLinkedList;
import java.util.Scanner;
public class DoubleLinkedList
{
private Node start;
public DoubleLinkedList()
{
start=null;
}
public void displayList()
{
Node p;
if(start==null)
{
System.out.println("List is empty");
return;
}
p=start;
System.out.print("List is : ");
while(p!=null)
{
System.out.print(p.info + " ");
p=p.next;
}
System.out.println();
}/*End of displayList()*/
public void insertInBeginning(int data)
{
Node temp=new Node(data);
temp.next=start;
start.prev=temp;
start=temp;
}/*End of insertInBeginning()*/
public void insertInEmptyList(int data)
{
Node temp=new Node(data);
start=temp;
}/*End of insertInEmptyList()*/
public void insertAtEnd(int data)
{
Node temp=new Node(data);
Node p=start;
while(p.next!=null)
p=p.next;
p.next=temp;
temp.prev=p;
}/*End of insertAtEnd()*/
public void createList()
{
int i,n,data;
Scanner scan = new Scanner(System.in);
System.out.print("Enter the number of nodes : ");
n = scan.nextInt();
if(n==0)
return;
System.out.print("Enter the first element to be inserted : ");
data = scan.nextInt();
insertInEmptyList(data);
for(i=2; i<=n; i++)
{
System.out.println("Enter the next element to be inserted : ");
data = scan.nextInt();
insertAtEnd(data);
}
}/*End of createList()*/
public void insertAfter(int data,int x)
{
Node temp=new Node(data);
Node p=start;
while(p!=null)
{
if(p.info==x)
break;
p=p.next;
}
if(p==null)
System.out.println(x + " not present in the list");
else
{
temp.prev=p;
temp.next=p.next;
if(p.next!=null)
p.next.prev=temp; /*should not be done when p refers to last node*/
p.next=temp;
}
}/*End of insertAfter()*/
public void insertBefore(int data,int x)
{
if(start==null)
{
System.out.println("List is empty");
return;
}
if(start.info==x)
{
Node temp=new Node(data);
temp.next=start;
start.prev=temp;
start=temp;
return;
}
Node p=start;
while(p!=null)
{
if(p.info==x)
break;
p=p.next;
}
if(p==null)
System.out.println(x + " not present in the list");
else
{
Node temp=new Node(data);
temp.prev=p.prev;
temp.next = p;
p.prev.next=temp;
p.prev=temp;
}
}/*End of insertBefore()*/
public void deleteFirstNode()
{
if(start==null) /*list is empty*/
return;
if(start.next==null) /*list has only one node*/
{
start=null;
return;
}
start=start.next;
start.prev=null;
}
public void deleteLastNode()
{
if(start==null) /*list is empty*/
return;
if(start.next==null) /*list has only one node*/
{
start=null;
return;
}
Node p=start;
while(p.next!=null)
p=p.next;
p.prev.next=null;
}
public void deleteNode(int x)
{
if(start==null) /*list is empty*/
return;
if(start.next==null) /*list has only one node*/
{
if(start.info==x)
start=null;
else
System.out.println(x + " not found");
return;
}
/*Deletion of first node*/
if(start.info==x)
{
start=start.next;
start.prev=null;
return;
}
Node p=start.next;
while(p.next!=null)
{
if(p.info==x)
break;
p=p.next;
}
if(p.next!=null) /*node to be deleted is in between*/
{
p.prev.next=p.next;
p.next.prev=p.prev;
}
else /*p refers to last node*/
{
if(p.info==x)/*node to be deleted is last node*/
p.prev.next=null;
else
System.out.println(x + " not found");
}
}/*End of deleteNode()*/
public void reverseList()
{
if(start==null)
return;
Node p1=start;
Node p2=p1.next;
p1.next=null;
p1.prev=p2;
while(p2!=null)
{
p2.prev=p2.next;
p2.next=p1;
p1=p2;
p2=p2.prev;
}
start=p1;
}/*End of reverseList()*/
}