-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoubly_linked_list.py
More file actions
188 lines (152 loc) · 5.43 KB
/
doubly_linked_list.py
File metadata and controls
188 lines (152 loc) · 5.43 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 23 10:27:20 2020
@author: yashodhara
"""
#doubly linked list implimentation with Insert - Delete - Display - Search Functions
import os # For only clearing screen after every operation.
######################### Class Block #################################
class Node:
def __init__(self,val,llink=None,rlink=None):
self.val = val
self.llink = llink
self.rlink = rlink
################################################
def display(self):
curr = self.rlink
print()
while curr:
print(" <= ",curr.val," => ",end=" ")
curr = curr.rlink
print()
return
################################################
def length(self):
curr = self.rlink
cnt=0
while curr:
cnt+=1
curr = curr.rlink
return cnt
################################################
def insert(self,n):
curr = self
while curr.rlink:
curr = curr.rlink
for i in range(n):
print("Enter value for Node {}: ".format(i+1),end=" ")
val = int(input())
new_node = Node(val)
new_node.llink = curr
curr.rlink = new_node
curr = curr.rlink
print(" Elements inserted successfullly...")
return
###############################################
def insert_pos(self,pos):
curr = self
cnt = 0
l = curr.length()
if pos == l+1:
curr.insert(1)
return
if pos > l:
print("-----------------Invalid position------------------")
return
val = int(input(" Enter Element to insert : "))
while curr:
if cnt+1 == pos:
new_node = Node(val)
new_node.rlink = curr.rlink
curr.rlink.llink = new_node
new_node.llink = curr
curr.rlink = new_node
print("Element {} Inserted..".format(new_node.val))
return
else:
curr = curr.rlink
cnt+=1
##############################################
def delete(self):
curr = self
cnt = 0
l = curr.length()
if l == 0:
print(" No nodes to delete")
else:
pos = int(input(" Enter position to delete : "))
if pos > l:
print("--------------------Invalid position--------------------")
return
else:
while curr:
if cnt+1 == pos:
dele = curr.rlink.val
del_nxt_node = curr.rlink.rlink
curr.rlink = del_nxt_node
if del_nxt_node:
del_nxt_node.llink = curr
print(" Element {} Deleted successfully...".format(dele))
return
else:
curr = curr.rlink
cnt+=1
#############################################
def search(self,key):
curr =self.rlink
cnt = 1
while curr:
if curr.val == key:
print("Element {} found at '{}'th position..".format(key,cnt))
return
else:
cnt+=1
curr = curr.rlink
print("-----------------Element not found in linked List-------------------")
#############################################
def reverse_display(self):
curr = self.rlink
while curr.rlink:
curr = curr.rlink
while curr.llink:
print(" <= ",curr.val," => ",end = "")
curr = curr.llink
print()
return
############################### Class Block Ends ########################################
# To clear screen after every operation.
def clear():
os.system('clear')
############################### Main block starts #######################################
def main():
first = Node(0)
print("--------------Doubly Linked List---------------")
# Operation performing loop
while(True):
choice = int(input("Operations available :\n 1.insert\n 2.Insert at Position\n 3.display\n 4.Display reverse\n 5.Delete\n 6.Search\n 7.Exit\nEnter Number to perform specified operation : "))
if choice == 1:
n = int(input("Enter number of nodes to insert : "))
first.insert(n)
elif choice == 2:
pos = int(input("Enter position to insert : "))
first.insert_pos(pos)
elif choice == 3:
first.display()
elif choice == 4:
first.reverse_display()
elif choice == 5:
first.delete()
elif choice == 6:
key = int(input(" Enter the key element to search : "))
first.search(key)
elif choice == 7:
break
else:
print("-----------Invalid choice-----------")
input()
clear()
############################## Main block Ends ######################################
# Main block invoking...
if __name__ == '__main__':
main()