-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy path3_linked_lists.py
158 lines (140 loc) · 3.9 KB
/
3_linked_lists.py
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
class Node:
def __init__(self,val=None,next=None):
self.val = val
self.next = next
class LinkedList:
def __init__(self):
self.head=None
def show(self):
if self.head == None:
print('Linked List is empty')
return
else:
itr = self.head
while itr:
print(itr.val,' --> ',end='')
itr=itr.next
print('None')
def getlength(self):
itr = self.head
c=0
while itr:
c+=1
itr = itr.next
return c
def insert_at_end(self,val):
if self.head is None:
node = Node(val,None)
self.head = node
return
else:
itr = self.head
while itr.next :
itr=itr.next
itr.next=Node(val,None)
def insert_at_beginning(self,val):
node = Node(val,self.head)
self.head=node
def insert_values(self,x):
self.head = None
for d in x:
self.insert_at_end(d)
def insert_at_pos(self,pos,val):
if pos < 0 or pos >= self.getlength():
raise Exception("Invalid syntax")
if pos == 0:
self.insert_at_beginning(val)
return
elif pos == self.getlength() - 1:
self.insert_at_end(val)
return
else:
itr = self.head
c = 0
while itr:
if c==pos -1:
node = Node(val,itr.next)
itr.next=node
break
c+=1
itr = itr.next
def remove_at_pos(self,pos):
if pos < 0 or pos >= self.getlength():
raise Exception('Invalid position')
if pos ==0:
self.head=self.head.next
return
itr = self.head
c=0
while itr:
if c==pos-1:
itr.next=itr.next.next
break
c+=1
itr=itr.next
pass
# Adding for task
def insert_after_value(self,val_before,val):
if self.head is None:
return
if self.head.val==val_before:
self.head.next = Node(val,self.head.next)
return
itr=self.head
c=0
while itr:
if val_before==itr.val:
self.insert_at_pos(c+1,val)
break
c+=1
itr=itr.next
else:
print('Data is not in the linked list')
def remove_by_val(self,val):
if self.head is None:
return
if self.head.val == val:
self.head = self.head.next
return
itr=self.head
c=0
while itr:
if itr.val == val:
self.remove_at_pos(c)
break
itr=itr.next
c+=1
else:
print('No such value present in linked list')
if __name__ == '__main__':
'''
l=LinkedList()
l.insert_values(['A','B','C','D'])
l.show()
l.insert_at_pos(2,'F')
l.show()
l.remove_at_pos(3)
l.show()
l.insert_at_end('R')
l.show()
l.insert_at_beginning('J')
l.show()
l.insert_after_value('A','K')
l.show()
l.remove_by_val('K')
l.show()
'''
ll = LinkedList()
ll.insert_values(["banana","mango","grapes","orange"])
ll.show()
ll.insert_after_value("mango","apple") # insert apple after mango
ll.show()
ll.remove_by_val("orange") # remove orange from linked list
ll.show()
ll.remove_by_val("figs")
ll.show()
ll.remove_by_val("banana")
ll.remove_by_val("mango")
ll.remove_by_val("apple")
ll.remove_by_val("grapes")
ll.show()