-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.py
More file actions
96 lines (72 loc) · 2.31 KB
/
list.py
File metadata and controls
96 lines (72 loc) · 2.31 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
#Implement an unordered list, using the node
#First, we construct the node class.
class Node:
def __init__(self, initdata):
self.data = initdata
self.next = None
def getData(self):
return self.data
def getNext(self):
return self.next
def setData(self, newdata):
self.data = newdata
def setNext(self, newnext):
self.next = newnext
#self.next refers to another node
#Then, we construct the unordered list class, using nodes.
#Note that a list itself doesn't contain nodes as its elements. It contains only its head.
class UnorderedList:
def __init__(self):
#head is a reference to the first node.
self.head = None
def isEmpty(self):
return self.head == None
#Note that the new item is to be referred by the head
def add(self,item):
temp = Node(item)
temp.setNext(self.head)
self.head = temp
def size(self):
current = self.head
count = 0
#implement traversal
while current != None:
count += 1
current = current.getNext()
return count
def search(self,item):
current = self.head
found = False
while current != None and not found:
if current.getData() == item:
found = True
else:
current = current.getNext()
return found
def remove(self,item):
current = self.head
previous = None
found = False
while not found:
if current.getData() == item:
found = True
else:
previous = current
current = current.getNext()
if previous == None:
#In case the first element corresponds to the item to be removed
self.head = current.getNext()
else:
previous.setNext(current.getNext())
def append(self, item):
#add the item to the end of the list cf:add is to add the item to the beginning of the list
#item is a node
if self.size == 0:
self.add(item)
else:
current = self.head
previous = None
while current != None:
previous = current
current = current.getNext()
previous.next = item