-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoublyLinkedList.py
More file actions
242 lines (207 loc) · 4.81 KB
/
Copy pathdoublyLinkedList.py
File metadata and controls
242 lines (207 loc) · 4.81 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
"""
data is the value.
prev is the reference to the previous element of the currnt element.
head is the beginning of the linked list.
"""
import platform
import os
# Just to clear the output
def clear_console():
if platform.system() == "Windows":
os.system("cls")
else:
os.system("clear")
class Node:
def __init__(self, data=None, prev=None, next=None):
self.data = data
self.next = next
self.prev = prev
class DoublyLinkedList:
def __init__(self):
self.head = None
def print_backwards(self):
if self.head == None:
print("Empty Linked List\n\n")
return
itr = self.get_last_node()
llstr = "None --> "
while itr:
llstr += str(itr.data) + " --> "
itr = itr.prev
clear_console()
llstr += "None"
print("Linked List in reverse order: ")
print(f"{llstr}\n\n")
def insert_at_begining(self, data):
node = Node(data, None, self.head)
if self.head:
self.head.prev = node
self.head = node
def insert_at_end(self, data):
if self.head is None:
self.head = Node(data, None, None)
return
itr = self.head
while itr.next:
itr = itr.next
itr.next = Node(data, itr, None)
def count(self):
count = 0
itr = self.head
while itr:
count += 1
itr = itr.next
return count
def remove_at(self, index):
if index < 0 or index >= self.count():
raise Exception("Invalid Index")
if index == 0:
self.head = self.head.next
self.head.prev = None
return
count = 0
itr = self.head
while itr:
if count == index:
itr.prev.next = itr.next
if itr.next:
itr.next.prev = itr.prev
break
itr = itr.next
count += 1
def insert_at(self, index, data):
if index < 0 or index >= self.count():
raise Exception("Invalid index")
if index == 0:
self.insert_at_begining(data)
return
count = 0
itr = self.head
while itr:
if count == index - 1:
node = Node(data, itr, itr.next)
if node.next:
itr.next.prev = node
itr.next = node
break
itr = itr.next
count += 1
def insert_after_value(self, val, data):
if self.head is None:
return
if self.head == val:
node = Node(data, self.head, self.head.next)
if self.head.next:
self.head.next.prev = node
self.head.next = node
return
itr = self.head
while itr:
if itr.data == val:
node = Node(data, itr, itr.next)
if itr.next:
itr.next.prev = node
itr.next = node
break
itr = itr.next
def remove_val(self, data):
if self.head is None:
return
if self.head.data == data:
self.head.next.prev = self.head.prev
self.head = self.head.next
return
itr = self.head
while itr:
if itr.next is None:
return
if itr.next.data == data:
itr.next.next.prev = itr
itr.next = itr.next.next
break
itr = itr.next
def get_last_node(self):
itr = self.head
while itr.next:
itr = itr.next
return itr
def print(self):
if self.head is None:
clear_console()
print("Empty Linked List\n\n")
return
itr = self.head
llstr = 'None --> '
while itr:
llstr += str(itr.data) + " --> "
itr = itr.next
llstr += "None\n\n"
clear_console()
print(llstr)
exit_no = 11
cmd = f"""[1] Insert at the beggining [2] Insert at the end
[3] Count [4] Print
[5] Remove at index [6] Insert at index
[7] Insert after value [8] Remove by value
[9] Get last element [10] Print Backwards
[{exit_no}] Exit\n
Enter : """
if __name__ == "__main__":
ll = DoublyLinkedList()
inp = 0
while inp != exit_no:
inp = 0
try:
inp = int(input(cmd))
except (KeyboardInterrupt, EOFError):
clear_console()
print("Exiting...\n")
break
except ValueError:
clear_console()
print("Enter a number...\n\n")
inp = 0
continue
if inp == 1:
str_ele = input("Enter elements: ")
ele = str_ele.split(' ')
for i in ele:
ll.insert_at_begining(int(i))
ll.print()
elif inp == 2:
str_ele = input("Enter elements: ")
ele = str_ele.split(" ")
for i in ele:
ll.insert_at_end(int(i))
ll.print()
elif inp == 3:
clear_console()
print(f"Length of Linked List is: {str(ll.count())}\n\n")
elif inp == 4:
ll.print()
elif inp == 5:
i = int(input("Enter the index to remove: "))
ll.remove_at(i)
ll.print()
elif inp == 6:
i = int(input("Enter the index to add: "))
data = int(input("Enter the element: "))
ll.insert_at(i, data)
ll.print()
elif inp == 7:
val = int(input("Enter the value to search: "))
data = int(input("Enter the data to insert after the value: "))
ll.insert_after_value(val, data)
ll.print()
elif inp == 8:
val = int(input("Enter the value to remove: "))
ll.remove_val(val)
ll.print()
elif inp == 9:
clear_console()
print(f"Last element is : {ll.get_last_node().data}")
elif inp == 10:
ll.print_backwards()
elif inp != exit_no:
clear_console()
print("Invalid Input.\n\n")