-
Notifications
You must be signed in to change notification settings - Fork 3
/
linkedList.py
180 lines (155 loc) · 5.14 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
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
class Node:
# Each node has its data and a pointer that points to next node in the Linked List
def __init__(self, data, next=None):
self.data = data
self.next = next
# function to set data
def setData(self, data):
self.data = data
# function to get data of a particular node
def getData(self):
return self.data
# function to set next node
def setNext(self, next):
self.next = next
# function to get the next node
def getNext(self):
return self.next
class LinkedList:
# Defining the head of the linked list
def __init__(self):
self.head = None
# printing the data in the linked list
def printLinkedList(self):
temp = self.head
while temp:
print(temp.data, end=" ")
temp = temp.next
# inserting the node at the beginning
def insertAtStart(self, data):
if self.head is None:
newNode = Node(data)
self.head = newNode
else:
newNode = Node(data)
newNode.next = self.head
self.head = newNode
# inserting the node in between the linked list (after a specific node)
@staticmethod
def insertBetween(previousNode, data):
if previousNode.next is None:
print("Previous node should have next node!")
else:
newNode = Node(data)
newNode.next = previousNode.next
previousNode.next = newNode
# inserting at the end of linked list
def insertAtEnd(self, data):
newNode = Node(data)
temp = self.head
while temp.next is not None: # get last node
temp = temp.next
temp.next = newNode
# deleting an item based on data(or key)
def delete(self, data):
temp = self.head
# if data/key is found in head node itself
if temp.next is not None:
if temp.data == data:
self.head = temp.next
temp = None
return
# else search all the nodes
while temp.next is not None:
if temp.data == data:
break
prev = temp # save current node as previous so that we can go on to next node
temp = temp.next
# node not found
if temp is None:
return
prev.next = temp.next
return
# iterative search
def search(self, node, data):
if node is None:
return False
if node.data == data:
return True
return self.search(node.getNext(), data)
class Node:
# Each node has its data and a pointer that points to next node in the Linked List
def __init__(self, data, next=None, previous=None):
self.data = data
self.next = next
self.previous = previous
class DoublyLinkedList:
def __init__(self):
self.head = None
# for inserting at beginning of linked list
def insertAtStart(self, data):
if self.head is None:
newNode = Node(data)
self.head = newNode
else:
newNode = Node(data)
self.head.previous = newNode
newNode.next = self.head
self.head = newNode
# for inserting at end of linked list
def insertAtEnd(self, data):
newNode = Node(data)
temp = self.head
while temp.next is not None:
temp = temp.next
temp.next = newNode
newNode.previous = temp
# deleting a node from linked list
def delete(self, data):
temp = self.head
if temp.next is not None:
# if head node is to be deleted
if temp.data == data:
temp.next.previous = None
self.head = temp.next
temp.next = None
return
while temp.next is not None:
if temp.data == data:
break
temp = temp.next
if temp.next:
# if element to be deleted is in between
temp.previous.next = temp.next
temp.next.previous = temp.previous
temp.next = None
temp.previous = None
else:
# if element to be deleted is the last element
temp.previous.next = None
temp.previous = None
return
if temp is None:
return
# for printing the contents of linked lists
def printdll(self):
temp = self.head
while temp is not None:
print(temp.data, end=" ")
temp = temp.next
if __name__ == "__main__":
List = LinkedList()
List.head = Node(1) # create the head node
node2 = Node(2)
List.head.setNext(node2) # head node's next --> node2
node3 = Node(3)
node2.setNext(node3) # node2's next --> node3
List.insertAtStart(4) # node4's next --> head-node --> node2 --> node3
List.insertBetween(node2, 5) # node2's next --> node5
List.insertAtEnd(6)
List.printLinkedList()
print()
List.delete(3)
List.printLinkedList()
print()
print(List.search(List.head, 1))