-
Notifications
You must be signed in to change notification settings - Fork 8
/
linked_list_dsa.py
200 lines (184 loc) · 5.11 KB
/
linked_list_dsa.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
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
"""
LinkedList
Node - [Done]
LinkedList - [Done]
Append - [Done]
Length - [Done]
Print - [Done]
Insert in head - [Done]
Insert using position - [Done]
Indexing - [Done]
Clear - [Done]
Remove from head - [Done]
pop [Remove from last] - [Done]
remove by value - [Done]
delete using index 'del' - [Done]
count - [Done]
reverse - [Done]
maximum - [Done]
"""
#creating a Node
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
self.n = 0
#append to the list
def append(self, value):
if self.head == None:
self.head = Node(value)
else:
curr = self.head
while curr.next != None:
curr = curr.next
curr.next = Node(value)
self.n+=1
#length of the LL
def __len__(self):
return self.n
#printing element
def __str__(self):
result = ""
curr = self.head
while curr != None:
result = result+str(curr.data)+" -> "
curr = curr.next
return "[ " + result[:-4] + " ]"
def __repr__(self):
return self.__str__()
#inserting from head
def insert_from_head(self, value):
new_node = Node(value)
new_node.next = self.head
self.head = new_node
self.n+=1
#insert using position
def insert(self, pos, value):
if pos < 0:
raise Exception("Negative indexing on next version")
if pos >= self.n:
raise Exception("Index out of range")
if pos == 0:
self.insert_from_head(value)
self.n+=1
else:
new_node = Node(value)
curr = self.head
i = 1
while curr != None and i < self.n:
if i == pos:
break
curr = curr.next
i+=1
new_node.next = curr.next
curr.next = new_node
self.n+=1
#indexing
def __getitem__(self, pos):
if pos < 0:
pos = self.n+pos
if pos < 0:
raise Exception("Index out of range")
if pos >= self.n:
raise Exception("Index out of range")
if 0 <= pos < self.n:
curr = self.head
i = 0
while curr != None and i < self.n:
if i == pos:
return curr.data
curr = curr.next
i+=1
#clear
def clear(self):
self.head = None
self.n = 0
#remove from head
def remove_from_head(self):
if self.head != None:
self.head = self.head.next
self.n-=1
else:
return "Linked list is empty"
#remove from last - pop
def pop(self):
if self.head == None:
return "Linked List is empty"
curr = self.head
if curr.next == None:
data = self.head.data
self.head = None
self.n = 0
return data
else:
while curr.next.next != None:
curr = curr.next
data = curr.next.data
curr.next = curr.next.next
self.n-=1
return data
#remove using value
def remove(self, value):
if self.head == None:
return "Linked List is Empty"
if self.head.next == None:
if self.head.data == value:
self.head = None
self.n = 0
else:
return f"{value} not in the Linked List"
else:
curr = self.head
if curr.data == value:
self.head = curr.next
self.n-=1
else:
while curr.next != None:
if curr.next.data == value:
break
curr = curr.next
if curr.next != None:
curr.next = curr.next.next
self.n-=1
else:
return f"{value} not in the linked list"
#delete using index and __delitem__
def __delitem__(self, pos):
item = self.__getitem__(pos)
self.remove(item)
#count
def count(self, value):
if self.head == None:
return "Linked list is empty"
else:
i = 0
curr = self.head
while curr != None:
if curr.data == value:
i+=1
curr = curr.next
return i
#reversing a linked_list
def reverse(self):
if self.head == None:
return "Linked List is empty"
new_ = LinkedList()
curr = self.head
while curr != None:
new_.insert_from_head(curr.data)
curr = curr.next
return new_
#maximum value of an linked list
def maximum(self):
if self.head == None:
return "Linked List is empty"
curr = self.head
max_ = curr.data
while curr != None:
if max_ < curr.data:
max_ = curr.data
curr = curr.next
return max_