-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedList.py
181 lines (171 loc) · 5.62 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
181
#! /user/env/bin python
"""
Author : Mikias Berhaun
Date : Aug 29 2019
Desc: Simple Linked List implementation
"""
#class Node is the main class for the linked list
class Node:
"""
the constructor of the class contains five class variables
data holds the data for the node , head is Node for the list which is the starting point
, next points to the next node , tail is Node at the end of the list and length is number
for counting the nodes in the list
"""
def __init__(self):
self.data = None
self.head = None
self.next = None
self.tail = None
self.length = 0
#set and get methods for the data
def setData(self , data):
self.data = data
def getData(self):
return self.data
#set and get methods for the next pointer
def setNext(self , next):
self.next = next
def getNext(self):
return self.next
#get method for the length
def getLength(self):
return self.length
#return True if the node has next node not null or None
def hasNext(self):
return self.next != None
#returns count for the list nodes almost similar with the length
def ListLength(self):
count = 0
current = self.head
while current != None:
count += 1
current = current.getNext()
return count
#display the list
def display(self):
current = self.head
while current != None:
print("{} -> ".format( current.getData() ) , end="")
current = current.getNext()
print("\n")
#returns data which is found at a certain valid position
def getDataPos(self , pos):
if pos < 0 or pos > self.length:
raise ValueError("invalid position used")
else:
current = self.head
count = 0
while count != pos and current.getNext() != None:
current = current.getNext()
count += 1
data = current.getData()
return data
"""
below are main list method , for inserting and delete data
for inserting data we have three cases
# insert at the beginning
# insert at the end
# insert at random position
as well for deleting data we have three cases
# deleting at the beginning
# deleting at the end
# deleting at random position
"""
#inserting at the beginning
def insertHead(self , data):
newNode = Node() #creates a new Node
newNode.setData(data)
if self.length == 0: #if the list is empty just set the head to the new node
self.head = newNode
self.length += 1
else:
newNode.setNext(self.head)
self.head = newNode
self.length += 1
#inserting at the end
def insertTail(self , data):
newNode = Node()
newNode.setData(data)
if self.length == 0:
self.head = self.tail = newNode
self.length += 1
else:
current = self.head
while current.getNext() != None:
current = current.getNext()
current.setNext(newNode)
newNode.setNext(None)
self.length += 1
#insert at random position
def insertRandom(self , pos , data):
if pos < 0 or pos > self.length:
raise ValueError("Invalid position used")
else:
newNode = Node()
newNode.setData(data)
if pos == 0:
self.insertHead(data)
elif pos == self.length:
self.insertTail(data)
else:
count = 0
current = self.head
while count != pos:
current = current.getNext()
count += 1
newNode.setNext(current.getNext())
current.setNext(newNode)
self.length += 1
#delete at the beginning or head
def deleteHead(self):
if self.length == 0:
raise ValueError("List is Empty")
elif self.length == 1:
self.head = None
self.tail = None
self.length = 0
else:
self.head = self.head.getNext()
self.length -= 1
#delete at the end of the list or tail
def deleteTail(self):
if self.length == 0:
raise ValueError("List is empty")
elif self.length == 1:
self.head = None
self.tail = None
self.length -= 1
else:
current = self.head
prev = self.head
while current.getNext() != None:
prev = current
current = current.getNext()
prev.setNext(None)
self.length -= 1
#delete at random position given by the user / valid position
def deleteRandom(self , pos):
if pos < 0 or pos > self.length:
raise ValueError("invalid position")
if pos == 0:
self.deleteHead()
elif pos == self.length:
self.deleteTail
else:
current = self.head
prev = self.head
count = 0
while count < pos and current.getNext() != None:
count += 1
if count == pos:
prev.setNext(current.getNext())
self.length -= 1
return
else:
prev = current
current = current.getNext()
#set the list to empty
def clearList(self):
self.head = None
self.length = 0