-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoubly_linked_list.java
More file actions
171 lines (138 loc) · 4.1 KB
/
Copy pathdoubly_linked_list.java
File metadata and controls
171 lines (138 loc) · 4.1 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
import java.util.Scanner;
class Dnode {
Dnode next;
Dnode prev;
char ch;
Dnode(char ch) {
this.ch = ch;
this.next = null;
this.prev = null;
}
}
class Dlist {
Dnode head = null;
void addNode(char ch) {
Dnode newNode = new Dnode(ch);
if (head == null) {
head = newNode;
} else {
Dnode temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = newNode;
newNode.prev = temp;
}
}
void display() {
int i = 0;
if (head == null) {
System.out.println("No Nodes Created");
} else {
Dnode temp = head;
while (temp != null) {
System.out.println(" Index [" + i + "] : " + temp.ch);
i++;
temp = temp.next;
}
}
}
void insert(int pos, char ch) {
Dnode newNode = new Dnode(ch);
if (pos == 0) {
newNode.next = head;
if (head != null) {
head.prev = newNode;
}
head = newNode;
return;
}
Dnode temp = head;
for (int i = 0; i < pos - 1 && temp != null; i++) {
temp = temp.next;
}
if (temp == null) {
System.out.println("Invalid position");
return;
}
newNode.next = temp.next;
newNode.prev = temp;
if (temp.next != null) {
temp.next.prev = newNode;
}
temp.next = newNode;
}
void delete(int pos) {
if (head == null) {
return;
}
if (pos == 0) {
head = head.next;
if (head != null) {
head.prev = null;
}
return;
}
Dnode temp = head;
for (int i = 0; i < pos && temp != null; i++) {
temp = temp.next;
}
if (temp == null) {
System.out.println("Invalid position.");
return;
}
if (temp.prev != null) {
temp.prev.next = temp.next;
}
if (temp.next != null) {
temp.next.prev = temp.prev;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Dlist list = new Dlist();
int choice;
do {
System.out.println("\n--- Doubly Linked List Menu ---");
System.out.println("1. Add new node at end");
System.out.println("2. Insert node at position");
System.out.println("3. Delete node at position");
System.out.println("4. Display list");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
choice = sc.nextInt();
sc.nextLine(); // consume newline
switch (choice) {
case 1:
System.out.print("Enter character to add: ");
char ch1 = sc.nextLine().charAt(0);
list.addNode(ch1);
break;
case 2:
list.display();
System.out.print("Enter position to insert: ");
int pos1 = sc.nextInt();
sc.nextLine(); // consume newline
System.out.print("Enter character to insert: ");
char ch2 = sc.nextLine().charAt(0);
list.insert(pos1, ch2);
break;
case 3:
list.display();
System.out.print("Enter position to delete: ");
int pos2 = sc.nextInt();
list.delete(pos2);
break;
case 4:
list.display();
break;
case 5:
System.out.println("Exiting program...");
break;
default:
System.out.println("Invalid choice!");
}
} while (choice != 5);
sc.close();
}
}