-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoublyLinkedList.java
168 lines (149 loc) · 4.89 KB
/
DoublyLinkedList.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
// Node class representing each element of the doubly linked list
class Node {
int data;
Node prev;
Node next;
// Constructor to create a new node with given data
Node(int data) {
this.data = data;
this.prev = null;
this.next = null;
}
}
// DoublyLinkedList class containing operations for the list
public class DoublyLinkedList {
Node head;
Node tail;
// Constructor to initialize an empty list
DoublyLinkedList() {
this.head = null;
this.tail = null;
}
// Method to add a node at the end of the list
void addNode(int data) {
Node newNode = new Node(data);
if (head == null) {
// If the list is empty, head and tail point to the new node
head = tail = newNode;
} else {
// Add the new node at the end and update tail
tail.next = newNode;
newNode.prev = tail;
tail = newNode;
}
}
// Method to insert a node at the beginning of the list
void insertAtBeginning(int data) {
Node newNode = new Node(data);
if (head == null) {
head = tail = newNode;
} else {
newNode.next = head;
head.prev = newNode;
head = newNode;
}
}
// Method to insert a node at a specified position
void insertAtPosition(int data, int position) {
if (position == 0) {
insertAtBeginning(data);
return;
}
Node newNode = new Node(data);
Node current = head;
int index = 0;
while (current != null && index < position - 1) {
current = current.next;
index++;
}
if (current == null) {
// If position is greater than the length of the list, append at the end
addNode(data);
} else {
newNode.next = current.next;
newNode.prev = current;
if (current.next != null) {
current.next.prev = newNode;
}
current.next = newNode;
if (newNode.next == null) {
tail = newNode;
}
}
}
// Method to display the list from head to tail
void displayForward() {
Node current = head;
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
// Method to display the list from tail to head
void displayBackward() {
Node current = tail;
while (current != null) {
System.out.print(current.data + " ");
current = current.prev;
}
System.out.println();
}
// Method to delete a node from the list given its data
void deleteNode(int data) {
Node current = head;
while (current != null) {
if (current.data == data) {
// If node to be deleted is the head node
if (current == head) {
head = head.next;
if (head != null) {
head.prev = null;
}
}
// If node to be deleted is the tail node
else if (current == tail) {
tail = tail.prev;
if (tail != null) {
tail.next = null;
}
}
// If node to be deleted is in the middle
else {
current.prev.next = current.next;
current.next.prev = current.prev;
}
return;
}
current = current.next;
}
// Print a message if the element is not found
System.out.println("Element not found.");
}
public static void main(String[] args) {
DoublyLinkedList dll = new DoublyLinkedList();
// Adding nodes to the list
dll.addNode(10);
dll.addNode(30);
// Inserting at the beginning
dll.insertAtBeginning(0);
System.out.print("List after inserting 0 at the beginning: ");
dll.displayForward();
// Inserting at position 1 (between 0 and 10)
dll.insertAtPosition(5, 1);
System.out.print("List after inserting 5 at position 1: ");
dll.displayForward();
// Inserting at position 4 (end of the list)
dll.insertAtPosition(40, 4);
System.out.print("List after inserting 40 at the end: ");
dll.displayForward();
// Delete a node and display the list again
dll.deleteNode(30);
System.out.print("List after deleting 30: ");
dll.displayForward();
// Attempt to delete a non-existent node
dll.deleteNode(50); // This will print "Element not found."
System.out.print("List after attempting to delete 50: ");
dll.displayForward();
}
}