Skip to content

Commit 5004454

Browse files
authored
Add files via upload
1 parent dbc6b8f commit 5004454

File tree

1 file changed

+149
-0
lines changed

1 file changed

+149
-0
lines changed

Doubly_linklist.py

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
#Doubly Linklist implementation
2+
3+
class Node:
4+
def __init__(self,data):
5+
self.data=data
6+
self.next=None
7+
self.prev=None
8+
9+
# Doubly Link list class
10+
class Doubly_linklist:
11+
#constructor
12+
def __init__(self):
13+
self.head=None
14+
self.tail=None
15+
#funcion to insert in Doubly link list at end
16+
def insert(self,data):
17+
a=Node(data)
18+
if self.head is None:
19+
self.head=self.tail=a
20+
else:
21+
self.tail.next=a
22+
a.prev=self.tail
23+
self.tail=a
24+
#to insert node at begining of list
25+
def insert_At_Beg(self,data):
26+
a=Node(data)
27+
if self.head is None:
28+
self.head=self.tail=a
29+
else :
30+
a.next=self.head
31+
self.head.prev=a
32+
self.head=a
33+
#Insertion at perticular Position
34+
def insert_At_Pos(self,pos,data):
35+
a=Node(data)
36+
p=self.head
37+
i=0
38+
if pos==0:
39+
self.insert_At_Beg(data)
40+
else:
41+
while p!=None and pos-1!=i:
42+
i+=1
43+
p=p.next
44+
if p is None and pos>i :
45+
print("INVALID POSITION")
46+
else:
47+
a.prev=p
48+
a.next=p.next
49+
p.next.prev=a
50+
p.next=a
51+
#to display list
52+
def display(self):
53+
p=self.head
54+
print("\nDoubly Link list is:::",end ="")
55+
if p is None:
56+
print("Empty!!!!")
57+
while p is not None:
58+
print("<=> ",p.data,end =" ")
59+
p=p.next
60+
#Display list in reverse order
61+
def reverse(self):
62+
p=self.tail
63+
if p is None:
64+
print("List is Empty")
65+
else:
66+
while p is not None:
67+
print("<=>",p.data,end="")
68+
p=p.prev
69+
#delete the starting node from List
70+
def delete(self):
71+
if self.head is None:
72+
print("List is Empty")
73+
else:
74+
self.head=self.head.next
75+
self.head.prev=None
76+
#deletion from End
77+
def delete_form_End(self):
78+
if self.head is None:
79+
print("List is Empty")
80+
else:
81+
self.tail=self.tail.prev
82+
self.tail.next=None
83+
#Deletion from a perticular position
84+
def delete_From_Pos(self,pos):
85+
if self.head==None:
86+
print("List is Empty")
87+
elif pos==0:
88+
self.delete()
89+
else:
90+
p=self.head
91+
i=0
92+
while p!=None and pos-1 is not i:
93+
i+=1
94+
p=p.next
95+
if p is None and pos>=i:
96+
print("Invalid Position")
97+
elif p.next.next is None and i is pos-1:
98+
p.next=None
99+
self.tail=p;
100+
else:
101+
p.next.next.prev=p
102+
p.next=p.next.next
103+
104+
def main():
105+
l=Doubly_linklist()
106+
ch='y'
107+
while ch is 'y':
108+
print('''1.Insertion
109+
2.Insert at begining
110+
3.Insert at End
111+
4.Insert at Position
112+
5.Display list
113+
6.Display in reverse order
114+
7.Delete from begining
115+
8.Delete from end
116+
9.Delete from position
117+
10.EXIT''')
118+
119+
option=int(input("Enter option:::"))
120+
121+
if option is 1:
122+
a=int(input("Enter data::"))
123+
l.insert(a)
124+
elif option is 2:
125+
a=int(input("Enter data::"))
126+
l.insert_At_Beg(a)
127+
elif option is 3:
128+
a=int(input("Enter data::"))
129+
l.insert(a)
130+
elif option is 4:
131+
pos=int(input("Enter Position(0 ......n)::"))
132+
a=int(input("Enter data::"))
133+
l.insert_At_Pos(pos,a)
134+
elif option is 5:
135+
l.display()
136+
elif option is 6:
137+
print("List in Reverse Order is :::",end="")
138+
l.reverse()
139+
elif option is 7:
140+
l.delete()
141+
elif option is 8:
142+
l.delete_form_End()
143+
elif option is 9:
144+
pos=int(input("Enter Position(0 ......n)::"))
145+
l.delete_From_Pos(pos)
146+
else:
147+
quit()
148+
ch=input("\nEnter y to continue program:::")
149+
main()

0 commit comments

Comments
 (0)