-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list.py
More file actions
154 lines (117 loc) · 3.69 KB
/
Copy pathlinked_list.py
File metadata and controls
154 lines (117 loc) · 3.69 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
class Node:
def __init__(self, data):
self.data = data
self.next = None
def __repr__(self):
return "<Node data: %s>" % self.data
class Linked_List:
# Singly linked list
def __init__(self):
self.head = None
def isEmpty(self):
return self.head == None
def llSize(self):
current = self.head
size = 0
while current:
size += 1
current = current.next
return size
def addNodeToStart(self, data):
newNode = Node(data)
newNode.next = self.head
self.head = newNode
def findNodeData(self, key): #searches for node based on data contained in the Node
current = self.head
while current:
if current.data == key:
return current
else:
current = current.next
return None
def insertNewNode(self, data, index):
if index == 0:
self.addNodeToStart(data)
return
if index > 0:
newNode = Node(data)
position = index
current = self.head
while position > 1:
current = current.next
position -= 1
prevNode = current
nextNode = current.next
prevNode.next = newNode
newNode.next = nextNode
def removeNodeByKey(self, key):
current = self.head
prev = None
found = False
while current and not found:
# base case - node is the head node
if current.data == key and current is self.head:
found = True
self.head = current.next # reassign head to the next node
#del current #delete current
break
# reassign pointer from the previous node to the next node - essentially skips the node we want to remove
elif current.data == key:
found = True
prev.next = current.next
# if the current node is not the node we are looking for, keep traversing the ll until node is found
else:
prev = current
current = current.next
return current
def removeNodeByIndex(self, index):
current = self.head
prev = None
found = False
position = index
if index == 0:
found = True
self.head = current.next
return current
while position > 1:
current = current.next
position -= 1
prev = current
current = current.next
nextNode = current.next
prev.next = nextNode
return current
def __repr__(self): # aka print list
nodes =[]
current = self.head
while current:
if current is self.head:
nodes.append("[Head: %s]" % current.data)
elif current.next is None:
nodes.append("[Tail: %s]" % current.data)
else:
nodes.append("[%s]" % current.data)
current = current.next
return "->".join(nodes)
##################################################################################################################
# instantiate linked list
ll = Linked_List()
# add nodes to linked list
ll.addNodeToStart(1)
ll.addNodeToStart(2)
ll.addNodeToStart(3)
ll.addNodeToStart(55)
ll.addNodeToStart(7)
ll.insertNewNode(6665, 0)
ll.insertNewNode(74747474, 5)
ll.insertNewNode(45, 6)
print(ll.llSize())
print(ll)
print(ll.findNodeData(55))
print(ll.findNodeData(45))
ll.removeNodeByKey(55)
print(ll)
ll.removeNodeByIndex(3)
print(ll)
ll.removeNodeByIndex(0)
print(ll)