forked from OmkarPathak/Data-Structures-using-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SinglyLinkedList.py
113 lines (97 loc) · 3.37 KB
/
SinglyLinkedList.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
# Author: OMKAR PATHAK
# Linked List and Node can be accomodated in separate classes for convenience
class Node(object):
# 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(object):
# 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 == 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)
def insertBetween(self, 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 != 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:
# else search all the nodes
while(temp.next != 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 == None:
return
prev.next = temp.next
return
# iterative search
def search(self, node, data):
if node == None:
return False
if node.data == data:
return True
return self.search(node.getNext(), data)
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))