Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 859ec51

Browse files
Doubly linked list in Python
1 parent f20f028 commit 859ec51

File tree

1 file changed

+229
-0
lines changed

1 file changed

+229
-0
lines changed

‎linked-list/double-linked-list.py

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
# Copyright (C) Deepali Srivastava - All Rights Reserved
2+
# This code is part of DSA course available on CourseGalaxy.com
3+
4+
class Node(object):
5+
6+
def __init__(self,value):
7+
self.info = value
8+
self.prev = None
9+
self.next = None
10+
11+
12+
class DoubleLinkedList(object):
13+
14+
def __init__(self):
15+
self.start = None
16+
17+
def display_list(self):
18+
if self.start is None:
19+
print("List is empty")
20+
return
21+
22+
print("List is : ")
23+
p = self.start
24+
while p is not None:
25+
print(p.info, " ", end='')
26+
p = p.next
27+
print()
28+
29+
30+
def insert_in_beginning(self,data):
31+
temp = Node(data)
32+
temp.next = self.start
33+
self.start.prev = temp
34+
self.start = temp
35+
36+
def insert_in_empty_list(self,data):
37+
temp = Node(data)
38+
self.start = temp
39+
40+
def insert_at_end(self,data):
41+
temp = Node(data)
42+
p = self.start
43+
while p.next is not None:
44+
p = p.next
45+
p.next = temp
46+
temp.prev = p
47+
48+
49+
def create_list(self):
50+
n = int(input("Enter the number of nodes : "))
51+
if n == 0:
52+
return
53+
data = int(input("Enter the first element to be inserted : "))
54+
self.insert_in_empty_list(data)
55+
56+
for i in range(n-1):
57+
data = int(input("Enter the next element to be inserted : "))
58+
self.insert_at_end(data)
59+
60+
61+
def insert_after(self,data, x):
62+
temp = Node(data)
63+
p = self.start
64+
while p is not None:
65+
if p.info == x:
66+
break
67+
p = p.next
68+
69+
if p is None:
70+
print(x," not present in the list")
71+
else:
72+
temp.prev = p
73+
temp.next = p.next
74+
if p.next is not None:
75+
p.next.prev = temp # should not be done when p refers to last node
76+
p.next = temp;
77+
78+
def insert_before(self,data,x):
79+
if self.start is None:
80+
print("List is empty")
81+
return
82+
83+
if self.start.info == x:
84+
temp = Node(data)
85+
temp.next = self.start
86+
self.start.prev = temp
87+
self.start = temp
88+
return
89+
90+
p = self.start;
91+
while p is not None:
92+
if p.info == x:
93+
break
94+
p = p.next
95+
96+
if p is None:
97+
print(x, " not present in the list")
98+
else:
99+
temp = Node(data)
100+
temp.prev = p.prev
101+
temp.next = p
102+
p.prev.next = temp
103+
p.prev = temp
104+
105+
def delete_first_node(self):
106+
if self.start is None: # list is empty
107+
return
108+
if self.start.next is None: # list has only one node
109+
self.start = None
110+
return
111+
self.start = self.start.next
112+
self.start.prev = None
113+
114+
def delete_last_node(self):
115+
if self.start is None: # list is empty
116+
return
117+
if self.start.next is None: # list has only one node
118+
self.start = None
119+
return
120+
121+
p = self.start;
122+
while p.next != None:
123+
p = p.next;
124+
p.prev.next = None
125+
126+
127+
def delete_node(self,x):
128+
if self.start is None: # list is empty
129+
return
130+
if self.start.next is None: # list has only one node
131+
if self.start.info == x:
132+
self.start = None;
133+
else:
134+
print(x," not found")
135+
return
136+
137+
# Deletion of first node
138+
if self.start.info == x:
139+
self.start = self.start.next
140+
self.start.prev = None
141+
return
142+
143+
p = self.start.next
144+
while p.next is not None:
145+
if p.info == x:
146+
break
147+
p = p.next
148+
149+
150+
if p.next is not None : # node to be deleted is in between
151+
p.prev.next = p.next
152+
p.next.prev = p.prev;
153+
else: # p refers to last node
154+
if p.info == x: # node to be deleted is last node
155+
p.prev.next = None
156+
else:
157+
print(x," not found")
158+
159+
def reverse_list(self):
160+
if self.start is None:
161+
return
162+
163+
p1 = self.start
164+
p2 = p1.next
165+
p1.next = None
166+
p1.prev = p2
167+
while p2 is not None:
168+
p2.prev = p2.next
169+
p2.next = p1
170+
p1 = p2
171+
p2 = p2.prev
172+
self.start = p1
173+
174+
#########################################################################
175+
176+
list = DoubleLinkedList()
177+
list.create_list()
178+
179+
while True:
180+
print("1.Display list")
181+
print("2.Insert in empty list")
182+
print("3.Insert a node in the beginning of the list")
183+
print("4.Insert a node at the end of the list")
184+
print("5.Insert a node after a specified node")
185+
print("6.Insert a node before a specified node")
186+
print("7.Delete first node")
187+
print("8.Delete last node")
188+
print("9.Delete any node")
189+
print("10.Reverse the list")
190+
print("11.Quit")
191+
192+
option = int(input("Enter your choice : " ))
193+
194+
if option == 1:
195+
list.display_list()
196+
elif option == 2:
197+
data = int(input("Enter the element to be inserted : "))
198+
list.insert_in_empty_list(data)
199+
elif option == 3:
200+
data = int(input("Enter the element to be inserted : "))
201+
list.insert_in_beginning(data)
202+
elif option == 4:
203+
data = int(input("Enter the element to be inserted : "))
204+
list.insert_at_end(data)
205+
elif option == 5:
206+
data = int(input("Enter the element to be inserted : "))
207+
x = int(input("Enter the element after which to insert : "))
208+
list.insert_after(data,x)
209+
elif option == 6:
210+
data = int(input("Enter the element to be inserted : "))
211+
x = int(input("Enter the element before which to insert : "))
212+
list.insert_before(data,x)
213+
elif option == 7:
214+
list.delete_first_node()
215+
elif option == 8:
216+
list.delete_last_node()
217+
elif option == 9:
218+
data = int(input("Enter the element to be deleted : "))
219+
list.delete_node(data)
220+
elif option == 10:
221+
list.reverse_list()
222+
elif option == 11:
223+
break
224+
else:
225+
print("Wrong option")
226+
print()
227+
228+
229+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /