-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoublyLinkedList.java
More file actions
205 lines (177 loc) · 5.33 KB
/
DoublyLinkedList.java
File metadata and controls
205 lines (177 loc) · 5.33 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
import java.util.*;
class Node {
Node prev;
int data;
Node next;
Node(int value) {
prev = null;
data = value;
next = null;
}
}
class DoublyLinkedList {
static Node head = null;
static Node tail = null;
static void insertAtBeginning(int data) {
Node temp = new Node(data);
if (head == null) {
head = temp;
tail = temp;
} else {
temp.next = head;
head.prev = temp;
head = temp;
}
}
static void insertAtEnd(int data) {
Node temp = new Node(data);
if (tail == null) {
head = temp;
tail = temp;
} else {
tail.next = temp;
temp.prev = tail;
tail = temp;
}
}
static void insertAtPosition(int data, int position) {
Node temp = new Node(data);
if (position == 1) {
insertAtBeginning(data);
} else {
Node current = head;
int currPosition = 1;
while (current != null
&& currPosition < position) {
current = current.next;
currPosition++;
}
if (current == null) {
insertAtEnd(data);
} else {
temp.next = current;
temp.prev = current.prev;
current.prev.next = temp;
current.prev = temp;
}
}
}
static void deleteAtBeginning() {
if (head == null) {
return;
}
if (head == tail) {
head = null;
tail = null;
return;
}
Node temp = head;
head = head.next;
head.prev = null;
temp.next = null;
}
static void deleteAtEnd() {
if (tail == null) {
return;
}
if (head == tail) {
head = null;
tail = null;
return;
}
Node temp = tail;
tail = tail.prev;
tail.next = null;
temp.prev = null;
}
static void deleteAtSpecificPosition(int pos) {
if (head == null) {
return;
}
if (pos == 1) {
deleteAtBeginning();
return;
}
Node current = head;
int count = 1;
while (current != null && count != pos) {
current = current.next;
count++;
}
if (current == null) {
System.out.println("Position wrong");
return;
}
if (current == tail) {
deleteAtEnd();
return;
}
current.prev.next = current.next;
current.next.prev = current.prev;
current.prev = null;
current.next = null;
}
static void display(Node head) {
Node temp = head;
while (temp != null) {
System.out.print(temp.data + " --> ");
temp = temp.next;
}
System.out.println("NULL");
}
// Drivers code
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
for(;;){
System.out.println("1. Insert at beginning");
System.out.println("2. Insert at end");
System.out.println("3. Insert at position");
System.out.println("4. Delete at beginning");
System.out.println("5. Delete at end");
System.out.println("6. Delete at position");
System.out.println("7. Display");
System.out.println("8. Exit");
System.out.println("Enter your choice");
int choice = sc.nextInt();
switch(choice){
case 1:
System.out.println("Enter the value to be inserted");
int value = sc.nextInt();
insertAtBeginning(value);
break;
case 2:
System.out.println("Enter the value to be inserted");
value = sc.nextInt();
insertAtEnd(value);
break;
case 3:
System.out.println("Enter the value to be inserted");
value = sc.nextInt();
System.out.println("Enter the position");
int position = sc.nextInt();
insertAtPosition(value, position);
break;
case 4:
deleteAtBeginning();
break;
case 5:
deleteAtEnd();
break;
case 6:
System.out.println("Enter the position");
position = sc.nextInt();
deleteAtSpecificPosition(position);
break;
case 7:
display(head);
break;
case 8:
System.exit(0);
sc.close();
break;
default:
System.out.println("Wrong choice");
}
}
}
}