-
Notifications
You must be signed in to change notification settings - Fork 0
/
Linked List.py
140 lines (122 loc) · 3.56 KB
/
Linked List.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
class Node:
def __init__(self,data=None,next=None):
self.data = data
self.next = next
class LinkedList:
def __init__(self):
self.head = None
def insert_at_beg(self,data):
node = Node(data,self.head)
self.head = node
def insert_at_end(self,data):
if self.head == None:
node = Node(data,None)
return
itr = self.head
while itr.next:
itr = itr.next
node = Node(data,itr.next)
itr.next = node
def delete_at_beg(self):
if self.head == None:
print("Linked list is empty")
return
itr = self.head
self.head = itr.next
def delete_at_end(self):
if self.head == None:
print("Linked List is empty")
return
itr = self.head
while itr.next.next:
itr = itr.next
itr.next = None
def get_length(self):
count = 0
itr = self.head
while itr:
itr = itr.next
count += 1
return count
def insert_values(self,values):
for data in values:
self.insert_at_end(data)
def insert_at(self,index,data):
if index<0 or index>self.get_length():
raise Exception("Invalid index")
if index==0:
self.insert_at_beg(data)
return
count = 0
itr = self.head
while True:
if(count==index-1):
temp = itr.next
node = Node(data,temp)
itr.next = node
return
itr = itr.next
count += 1
def delete_at(self,index):
if index<0 or index>self.get_length():
raise Exception("Invalid Exception")
if index==0:
self.delete_at_beg()
return
itr = self.head
count = 0
while True:
if count==index-1:
itr.next = itr.next.next
return
itr = itr.next
count += 1
def insert_after_value(self,value,data):
itr = self.head
while itr:
if itr.data==value:
node = Node(data,itr.next)
itr.next = node
return
itr = itr.next
print("Value not found")
return
def remove_by_value(self,value):
itr = self.head
if self.head==None:
return
if self.head.data==value:
self.head = self.head.next
return
while itr.next:
if itr.next.data==value:
itr.next = itr.next.next
return
itr = itr.next
def print(self):
if self.head == None:
print("Linked List is empty")
return
itr = self.head
while itr:
print(itr.data,"->",end='')
itr = itr.next
if __name__ == '__main__':
ll = LinkedList()
ll.insert_at_beg(10)
ll.insert_at_beg(5)
ll.insert_at_end(15)
ll.insert_at_end(20)
ll.insert_at_end(25)
ll.delete_at_beg()
ll.insert_values(['A','B','C','D'])
ll.delete_at_end()
ll.insert_at(7,'New')
ll.delete_at(7)
ll.insert_at(0,5)
ll.delete_at(0)
ll.insert_after_value('C','D')
ll.remove_by_value(25)
ll.insert_after_value(20,25)
print("Lengtb of the linked list: ",ll.get_length())
ll.print()