Skip to content

Commit fa4f4c0

Browse files
Merge pull request #32 from SS56Bo/java/dsa
Added Double Linked List implementation in Java
2 parents 6e5f29c + 5aca2a6 commit fa4f4c0

File tree

1 file changed

+189
-0
lines changed

1 file changed

+189
-0
lines changed

DoublyLL.java

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
class Node {
2+
int data;
3+
Node prev;
4+
Node next;
5+
6+
Node(int x) {
7+
data = x;
8+
prev = next = null;
9+
}
10+
}
11+
12+
public class DoublyLL {
13+
14+
// 🧩 Insert at beginning
15+
public Node insertAtBeginning(Node head, int x) {
16+
Node temp = new Node(x);
17+
temp.next = head;
18+
if (head != null) {
19+
head.prev = temp;
20+
}
21+
return temp;
22+
}
23+
24+
// 🧩 Insert at end
25+
public Node insertAtEnd(Node head, int x) {
26+
Node temp = new Node(x);
27+
if (head == null) return temp;
28+
29+
Node curr = head;
30+
while (curr.next != null) {
31+
curr = curr.next;
32+
}
33+
curr.next = temp;
34+
temp.prev = curr;
35+
return head;
36+
}
37+
38+
// 🧩 Insert at specific position (1-based index)
39+
public Node insertAtPosition(Node head, int pos, int x) {
40+
if (pos <= 0) {
41+
System.out.println("Invalid position!");
42+
return head;
43+
}
44+
45+
if (pos == 1) return insertAtBeginning(head, x);
46+
47+
Node temp = new Node(x);
48+
Node curr = head;
49+
50+
for (int i = 1; i < pos - 1 && curr != null; i++) {
51+
curr = curr.next;
52+
}
53+
54+
if (curr == null) {
55+
System.out.println("Position out of bounds!");
56+
return head;
57+
}
58+
59+
temp.next = curr.next;
60+
if (curr.next != null) {
61+
curr.next.prev = temp;
62+
}
63+
curr.next = temp;
64+
temp.prev = curr;
65+
66+
return head;
67+
}
68+
69+
// 🧩 Delete from beginning
70+
public Node deleteAtBeginning(Node head) {
71+
if (head == null) return null;
72+
head = head.next;
73+
if (head != null) {
74+
head.prev = null;
75+
}
76+
return head;
77+
}
78+
79+
// 🧩 Delete from end
80+
public Node deleteAtEnd(Node head) {
81+
if (head == null || head.next == null) return null;
82+
83+
Node curr = head;
84+
while (curr.next != null) {
85+
curr = curr.next;
86+
}
87+
88+
curr.prev.next = null;
89+
return head;
90+
}
91+
92+
// 🧩 Delete at specific position (1-based index)
93+
public Node deleteAtPosition(Node head, int pos) {
94+
if (head == null || pos <= 0) return head;
95+
96+
if (pos == 1) return deleteAtBeginning(head);
97+
98+
Node curr = head;
99+
100+
for (int i = 1; i < pos && curr != null; i++) {
101+
curr = curr.next;
102+
}
103+
104+
if (curr == null) {
105+
System.out.println("Position out of bounds!");
106+
return head;
107+
}
108+
109+
if (curr.next != null) {
110+
curr.next.prev = curr.prev;
111+
}
112+
if (curr.prev != null) {
113+
curr.prev.next = curr.next;
114+
}
115+
116+
return head;
117+
}
118+
119+
// 🧩 Reverse the doubly linked list
120+
public Node reverse(Node head) {
121+
if (head == null) return null;
122+
123+
Node curr = head, prevNode = null;
124+
while (curr != null) {
125+
prevNode = curr.prev;
126+
curr.prev = curr.next;
127+
curr.next = prevNode;
128+
curr = curr.prev;
129+
}
130+
131+
if (prevNode != null) {
132+
head = prevNode.prev;
133+
}
134+
return head;
135+
}
136+
137+
// 🧩 Print list forward
138+
public void printList(Node head) {
139+
Node curr = head;
140+
System.out.print("Forward: ");
141+
while (curr != null) {
142+
System.out.print(curr.data + " ");
143+
curr = curr.next;
144+
}
145+
System.out.println();
146+
}
147+
148+
// 🧩 Print list backward
149+
public void printReverse(Node head) {
150+
if (head == null) return;
151+
Node curr = head;
152+
while (curr.next != null) {
153+
curr = curr.next;
154+
}
155+
System.out.print("Backward: ");
156+
while (curr != null) {
157+
System.out.print(curr.data + " ");
158+
curr = curr.prev;
159+
}
160+
System.out.println();
161+
}
162+
163+
// 🧩 Example usage
164+
public static void main(String[] args) {
165+
DoublyLL dll = new DoublyLL();
166+
Node head = null;
167+
168+
head = dll.insertAtEnd(head, 10);
169+
head = dll.insertAtEnd(head, 20);
170+
head = dll.insertAtEnd(head, 30);
171+
head = dll.insertAtBeginning(head, 5);
172+
head = dll.insertAtPosition(head, 3, 15);
173+
174+
dll.printList(head);
175+
dll.printReverse(head);
176+
177+
head = dll.deleteAtBeginning(head);
178+
head = dll.deleteAtEnd(head);
179+
head = dll.deleteAtPosition(head, 2);
180+
181+
dll.printList(head);
182+
dll.printReverse(head);
183+
184+
head = dll.reverse(head);
185+
System.out.println("After reversal:");
186+
dll.printList(head);
187+
dll.printReverse(head);
188+
}
189+
}

0 commit comments

Comments
 (0)