Skip to content

Commit b6f63e4

Browse files
committed
Added Doubly Linked List AST operations in Python + Linked List in Java
1 parent 727f4c8 commit b6f63e4

2 files changed

Lines changed: 300 additions & 0 deletions

File tree

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
import java.util.Scanner;
2+
3+
class Node {
4+
int data;
5+
Node next;
6+
Node (int x) {
7+
data = x;
8+
next = null;
9+
}
10+
}
11+
12+
public class LL {
13+
public static void printLL(Node head) {
14+
Node current = head;
15+
while (current!=null) {
16+
System.out.print(current.data+" -> ");
17+
current = current.next;
18+
}
19+
System.out.println("NULL");
20+
}
21+
22+
public Node insertAtBeginning(Node head, int x) {
23+
Node temp = new Node(x);
24+
temp.next = head;
25+
return temp;
26+
}
27+
28+
public Node insertAtEnd(Node head, int x){
29+
Node temp = new Node(x);
30+
if (head == null) {
31+
return temp;
32+
}
33+
Node current = head;
34+
while (current.next != null) {
35+
current = current.next;
36+
}
37+
current.next = temp;
38+
return head;
39+
}
40+
41+
public Node insertAtPosition(Node head, int data, int position) {
42+
Node temp = new Node(data);
43+
if (position == 1) {
44+
temp.next = head;
45+
return temp;
46+
}
47+
48+
Node curr = head;
49+
for (int i = 1; i<=position-2 && curr!=null; i++) {
50+
curr=curr.next;
51+
}
52+
if (curr == null) {
53+
return head;
54+
}
55+
56+
temp.next = curr.next;
57+
curr.next = temp;
58+
return head;
59+
}
60+
61+
public Node deleteHeadNode(Node head) {
62+
if (head == null) {
63+
return null;
64+
} else {
65+
return head.next;
66+
}
67+
}
68+
69+
public Node deleteNodeAtEnd(Node head) {
70+
if (head == null) return null;
71+
if (head.next == null) return null;
72+
73+
Node curr = head;
74+
while (curr.next.next!=null) {
75+
curr = curr.next;
76+
}
77+
curr.next = null;
78+
return head;
79+
}
80+
81+
public Node deleteNodeAnyPosition(Node head, int position) {
82+
if (head==null) return null;
83+
if (position==1) return head.next;
84+
85+
Node curr = head;
86+
for (int i=1; i<=position-2 && curr!=null; i++) {
87+
curr = curr.next;
88+
}
89+
90+
if (curr == null || curr.next == null) {
91+
return head;
92+
}
93+
94+
curr.next = curr.next.next;
95+
return head;
96+
}
97+
98+
public static void main(String[] args) {
99+
100+
Scanner sc = new Scanner(System.in);
101+
102+
LL list = new LL();
103+
Node head = null;
104+
int choice = 0;
105+
106+
while (choice!=8) {
107+
System.out.println("\n");
108+
System.out.println("Choose what to do?");
109+
System.out.println("1. Insert Node at the beginning");
110+
System.out.println("2. Insert Node at the end");
111+
System.out.println("3. Insert Node at any position");
112+
System.out.println("4. Delete Node at the beginning");
113+
System.out.println("5. Delete Node at the end");
114+
System.out.println("6. Delete Node at any position");
115+
System.out.println("7. Print the Linked List");
116+
System.out.print("Enter your choice: ");
117+
choice = sc.nextInt();
118+
119+
switch (choice) {
120+
case 1: // Insert at beginning
121+
System.out.print("Enter value: ");
122+
int val1 = sc.nextInt();
123+
head = list.insertAtBeginning(head, val1);
124+
break;
125+
case 2: // Insert at any end
126+
System.out.print("Enter value: ");
127+
val1 = sc.nextInt();
128+
head = list.insertAtEnd(head, val1);
129+
break;
130+
case 3: // Insert at any position
131+
System.out.print("Enter value: ");
132+
val1 = sc.nextInt();
133+
System.out.print("Enter position: ");
134+
int pos = sc.nextInt();
135+
head = list.insertAtPosition(head, val1, pos);
136+
break;
137+
case 4: // Delete at the beginning
138+
head = list.deleteHeadNode(head);
139+
System.out.println("Node deleted at beginning!");
140+
break;
141+
case 5: // Delete at the end
142+
head = list.deleteNodeAtEnd(head);
143+
System.out.println("Node deleted at end!");
144+
break;
145+
case 6: // Delete at any position
146+
System.out.print("Enter position: ");
147+
pos = sc.nextInt();
148+
head = list.deleteNodeAnyPosition(head, pos);
149+
System.out.println("Node deleted at position "+pos);
150+
break;
151+
case 7: // Print List
152+
System.out.println("\n");
153+
LL.printLL(head);
154+
System.out.println("\n");
155+
break;
156+
}
157+
}
158+
159+
sc.close();
160+
}
161+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
# Doubly Linked List implementation
2+
class Node:
3+
def __init__(self, values):
4+
self.data = values
5+
self.previous = None
6+
self.next = None
7+
8+
def insert_at_beginning(head, x):
9+
node = Node(x)
10+
node.next = head
11+
if head:
12+
head.previous = node
13+
return node
14+
15+
def insert_at_end(head, x):
16+
node = Node(x)
17+
if head is None:
18+
return node
19+
temp = head
20+
while temp.next:
21+
temp = temp.next
22+
temp.next = node
23+
node.previous = temp
24+
return head
25+
26+
def insert_after_node(node, x):
27+
if node is None:
28+
print("Node cannot be null")
29+
return
30+
a = Node(x)
31+
a.next = node.next
32+
a.previous = node
33+
if node.next:
34+
node.next.previous = a
35+
node.next = a
36+
37+
def insert_before_node(head, node, val):
38+
if node is None:
39+
print("Node cannot be null")
40+
return head
41+
42+
new = Node(val)
43+
new.previous = node.previous
44+
new.next = node
45+
if node.previous: # if not the head
46+
node.previous.next = new
47+
else: # if it was the head, new becomes the head
48+
head = new
49+
node.previous = new
50+
return head
51+
52+
def delete_head(head):
53+
if head is None:
54+
return None
55+
if head.next is None: # only one element
56+
return None
57+
new_head = head.next
58+
new_head.previous = None
59+
return new_head
60+
61+
def delete_at_end(head):
62+
if head is None:
63+
return None
64+
if head.next is None: # only one element
65+
return None
66+
temp = head
67+
while temp.next:
68+
temp = temp.next
69+
temp.previous.next = None
70+
return head
71+
72+
def delete_by_value(head, val):
73+
if head is None:
74+
return None
75+
temp = head
76+
77+
# if head itself needs to be deleted
78+
if temp.data == val:
79+
return delete_head(head)
80+
81+
while temp and temp.data != val:
82+
temp = temp.next
83+
84+
if temp is None:
85+
print(f"Value {val} not found in the list.")
86+
return head
87+
88+
if temp.next:
89+
temp.next.previous = temp.previous
90+
if temp.previous:
91+
temp.previous.next = temp.next
92+
return head
93+
94+
def print_LL(head):
95+
temp = head
96+
while temp:
97+
print(temp.data, end=" <-> ")
98+
temp = temp.next
99+
print("None")
100+
101+
# print the doubly linked list backwards
102+
def print_reverse(head):
103+
if head is None:
104+
print("None")
105+
return
106+
107+
temp = head
108+
while temp.next:
109+
temp = temp.next
110+
111+
# print backwards
112+
while temp:
113+
print(temp.data, end=" <-> ")
114+
temp = temp.previous
115+
print("None")
116+
117+
118+
if __name__ == '__main__':
119+
head = None
120+
head = insert_at_beginning(head, 89)
121+
head = insert_at_beginning(head, 63)
122+
insert_after_node(head.next, 56)
123+
head = insert_before_node(head, head.next, 10)
124+
head = insert_at_end(head, 96)
125+
head = insert_at_beginning(head, 22)
126+
head = insert_at_end(head, 32)
127+
print_LL(head)
128+
head = delete_head(head)
129+
head = delete_head(head)
130+
head = delete_head(head)
131+
print_LL(head)
132+
head = insert_at_end(head, 88)
133+
head = insert_at_end(head, 44)
134+
print_LL(head)
135+
head = delete_at_end(head)
136+
print_LL(head)
137+
head = delete_by_value(head, 32)
138+
print_LL(head)
139+
print_reverse(head)

0 commit comments

Comments
 (0)