-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list.py
More file actions
133 lines (117 loc) · 3.03 KB
/
Copy pathlinked_list.py
File metadata and controls
133 lines (117 loc) · 3.03 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
class Node:
"""
Represents individual element in the linked list.
nxt is a pointer to the next element in the linked list
"""
def __init__(self, data=None, nxt=None):
self.data = data
self.next = nxt
class LinkedList:
def __init__(self):
# head variable is a variable which points to the head of the linked list
self.head = None
def insert_at_beginning(self, data):
node = Node(data, self.head)
self.head = node
def insert_at_end(self, data):
if self.head is None:
self.head = Node(data, None)
return
itr = self.head
while itr.next:
itr = itr.next
itr.next = Node(data, None)
def insert_values(self, data_list):
self.head = None
for data in data_list:
self.insert_at_end(data)
def get_length(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.get_length():
raise Exception("Invalid index value")
if index == 0:
self.head = self.head.next
itr = self.head
count = 0
while itr:
if count == index - 1:
itr.next = itr.next.next
break
itr = itr.next
count += 1
def insert_at(self, index, data):
if index <0 or index > self.get_length():
raise Exception("Invalid index value")
if index == 0:
self.insert_at_beginning(data)
itr = self.head
count = 0
while itr:
if count == index - 1:
node = Node(data, itr.next)
itr.next = node
break
itr = itr.next
count += 1
def print_linked_list(self):
if self.head is None:
print("Linked list is empty!!!!")
return
itr = self.head
ll_str = ''
while itr:
ll_str += str(itr.data) + '----->'
itr = itr.next
print(ll_str)
def insert_after_value(self, data_after, data_to_insert):
if self.head is None:
return
if self.head.data == data_after:
self.head.next = Node(data_to_insert, self.head.next)
return
itr = self.head
while itr:
if itr.data == data_after:
itr.next = Node(data_to_insert, itr.next)
break
itr = itr.next
def remove_by_value(self, data):
if self.head is None:
return
if self.head.data == data:
self.head = self.head.next
return
itr = self.head
while itr.next:
if itr.next.data == data:
itr.next = itr.next.next
break
itr = itr.next
if __name__ == '__main__':
llist = LinkedList()
llist.insert_values(["apple", "banana", "cherry", "raspberry", "blueberry"])
llist.print_linked_list()
llist.remove_at(3)
llist.print_linked_list()
llist.insert_at(0, 'fig')
llist.print_linked_list()
llist.insert_at(3, 'kivi')
llist.print_linked_list()
llist.insert_values([18,12,23,84,26,17])
llist.print_linked_list()
llist.remove_at(4)
llist.print_linked_list()
llist.insert_values(["banana", "mango", "grapes", "orange"])
llist.print_linked_list()
llist.insert_after_value("mango", "apple") # insert apple after mango
llist.print_linked_list()
llist.insert_after_value("grapes", "fig") # insert fig after grapes
llist.print_linked_list()
llist.remove_by_value("apple") # remove apple from linked list
llist.print_linked_list()