forked from kant003/JavaPracticeHacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoublyLinkedList.java
More file actions
145 lines (128 loc) · 4.02 KB
/
DoublyLinkedList.java
File metadata and controls
145 lines (128 loc) · 4.02 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
public class DoublyLinkedList {
// Head and Tail pointers for the list
private Node head;
private Node tail;
private int size;
// Constructor
public DoublyLinkedList() {
this.head = null;
this.tail = null;
this.size = 0;
}
// --- Basic Operations ---
/**
* Adds a new node to the front (head) of the list.
*/
public void addFirst(int data) {
Node newNode = new Node(data);
if (head == null) {
// List is empty
head = newNode;
tail = newNode;
} else {
// List is not empty
newNode.next = head;
head.prev = newNode;
head = newNode;
}
size++;
}
/**
* Adds a new node to the end (tail) of the list.
*/
public void addLast(int data) {
Node newNode = new Node(data);
if (tail == null) {
// List is empty
head = newNode;
tail = newNode;
} else {
// List is not empty
tail.next = newNode;
newNode.prev = tail;
tail = newNode;
}
size++;
}
/**
* Deletes the first occurrence of a node with the given data.
*/
public boolean deleteNode(int data) {
Node current = head;
// Traverse to find the node
while (current != null && current.data != data) {
current = current.next;
}
// If the node was not found
if (current == null) {
return false;
}
// 1. If it's the HEAD node
if (current == head) {
head = head.next;
if (head != null) {
head.prev = null;
} else {
// List became empty
tail = null;
}
}
// 2. If it's the TAIL node
else if (current == tail) {
tail = tail.prev;
tail.next = null;
}
// 3. If it's a node in the MIDDLE
else {
current.prev.next = current.next;
current.next.prev = current.prev;
}
size--;
return true;
}
/**
* Prints the list from head to tail.
*/
public void printForward() {
Node current = head;
System.out.print("Forward List: ");
while (current != null) {
System.out.print(current.data + (current.next != null ? " <-> " : ""));
current = current.next;
}
System.out.println();
}
/**
* Prints the list from tail to head.
*/
public void printBackward() {
Node current = tail;
System.out.print("Backward List: ");
while (current != null) {
System.out.print(current.data + (current.prev != null ? " <-> " : ""));
current = current.prev;
}
System.out.println();
}
// --- Main Method for Testing ---
public static void main(String[] args) {
DoublyLinkedList list = new DoublyLinkedList();
System.out.println("--- Adding Elements ---");
list.addLast(10); // List: [10]
list.addFirst(5); // List: [5 <-> 10]
list.addLast(20); // List: [5 <-> 10 <-> 20]
list.addFirst(1); // List: [1 <-> 5 <-> 10 <-> 20]
list.addLast(30); // List: [1 <-> 5 <-> 10 <-> 20 <-> 30]
list.printForward();
list.printBackward();
System.out.println("Current Size: " + list.size);
System.out.println("\n--- Deleting Elements ---");
System.out.println("Deleting 1 (Head): " + list.deleteNode(1)); // Delete Head
System.out.println("Deleting 30 (Tail): " + list.deleteNode(30)); // Delete Tail
System.out.println("Deleting 10 (Middle): " + list.deleteNode(10)); // Delete Middle
System.out.println("Deleting 99 (Not Found): " + list.deleteNode(99)); // Not Found
list.printForward(); // Should be [5 <-> 20]
list.printBackward(); // Should be [20 <-> 5]
System.out.println("New Size: " + list.size);
}
}