-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.py
144 lines (117 loc) · 4.52 KB
/
LinkedList.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
"""
Linked Lists:
Single Linked Lists:
|data|LinkToNextData|--->|data|LinkToNextData|--->|data|null|
(disadvantage: can't go back to previous data after one data point)
Double Linked List:
|Null|data|LinkToNextData|<--->|LinkToPreviousData|data|LinkToNextData|<--->|LinkToPreviousData|data|null|
Advantage of Linked List over array:
> No need to pre-allocate the space
> Insertion is easier
BigO of Linked List:
Indexing -----> O(n)
Insert/Delete element at Start -----> O(1)
Insert/Delete element at End -----> O(n)
Insert element at Middle -----> O(n)
Linked List Traversal(Reverse) -----> O(n)
Accessing Elements by Value -----> O(n)
"""
class Node:
'''
Working of Class Node:
whenever this class is called, a data will be passed which will get saved in LinkedList as a node which has 2 parts: data & next
'''
def __init__(self, data=None, next=None): # initialised data to None
self.data = data
self.next = next
class LinkedList():
def __init__(self):
self.head = Node() # Head is starting point of a Linked List and points to no other value
# def accept_data(self, data):
def accept_data(self, *data): # args to get data at single go
new_node = Node(data) # created a new node to add data
cur = self.head # pointer set for the next data
while cur.next != None:
# let linked list have 5 elements; new node will be added as 6th node in linkedlistl;
# so we will have to search for the last pointer (ptr of 5th ele)
# so we will iterate until ptr of of curent ele becomes none; as it gets None we will come to know that this is the last node
cur = cur.next
cur.next = new_node # pointer set to the new node
# method to print each element of Linked List
def display(self):
disp = []
cur = self.head
disp.append(cur.data) # to display head element
while cur.next != None: # will iterate until next val of current pointer gets None
cur = cur.next # accessing next element of the node
disp.append(cur.data) # appended the data of current element to the list disp[]
print(disp)
# Method to get length of elements in Linked List
def getLength(self):
count = 0
itr = self.head
while itr:
count += 1
itr = itr.next
return count
def removeAtIndex(self, index):
# check if index is valid
if index < 0 or index >= self.getLength():
raise Exception("Invalid Index")
if index == 0: # removing head
self.head = self.head.next
return
count = 0
itr = self.head
while itr:
# removing element at given index
if count == index - 1: # we need to stop at the element prior to element we are removing
# After element(e1) is removed, address of e2 is linked to e0
itr.next = itr.next.next
break
itr = itr.next
count += 1
def insertAtIndex(self, index, data):
# check if index is valid
if index < 0 or index >= self.getLength():
raise Exception("Invalid Index")
if index == 0: # value is inserted at index[0]
self.accept_data(data)
return
count = 0
itr = self.head
while itr:
if count == index - 1:
node = Node(data, itr.next)
itr.next = node
itr = itr.next
count+=1
l = LinkedList() # created an object 'l' to call class LinkedList
# adding elements to linked list one-by-one
l.head = Node(1) # Head of the Linked List
element2 = Node(2)
element3 = Node(3)
element4 = Node(4)
# Linking Nodes of the Linked List to each other
# if not linked, only value at head will be printed
l.head.next = element2 # head is pointing to element2
element2.next = element3 # element2 is pointing to element3
element3.next = element4 # element3 is pointing to element4
# display Linked List
l.display()
print("Length: ", l.getLength())
l.removeAtIndex(2)
l.display()
l.insertAtIndex(2, 3) # (index, value)
l.display()
# adding elements of LinkedList at once
# l = LinkedList()
# l.accept_data(1,2,3,4,5)
# if function accept_data is used without (*data) as parameter
# adding values one-by-one
# l.accept_data(1)
# l.accept_data(2)
# l.accept_data(3)
# l.accept_data(4)
# l.accept_data(5)
# l.display()