-
Notifications
You must be signed in to change notification settings - Fork 0
/
unordered_list.py
141 lines (116 loc) · 3.05 KB
/
unordered_list.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
class UnorderedList:
# constructor
def __init__(self):
self.head = None
# check if the list is empty
def isEmpty(self):
return self.head == None
# insert item to be the first item of the list
def add(self, item):
temp = Node(item)
temp.setNext(self.head)
self.head = temp
# size of the list
def size(self):
current = self.head
count = 0
while current != None:
count += 1
current = current.getNext()
return count
# find if item is in the list
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
# remove item from the list
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:
self.head = current.getNext()
else:
previous.setNext(current.getNext())
# insert the item to be last element of list
def append(self, item):
current = self.head
temp = Node(item)
while current != None:
current = current.getNext()
current.setNext(temp)
# insert item in the pos-th of the list
def insert(self, pos, item):
current = self.head
previous = None
index = 0
temp = Node(item)
while current != None and index < pos:
previous = current
current = current.getNext()
index += 1
if pos == 0:
temp.setNext(self.head)
self.head = temp
else:
if current == None:
previous.setNext(temp)
else:
temp.setNext(current)
previous.setNext(temp)
# get index of item in the list
def index(self, item):
current = self.head
found = False
index = 0
while current != None and not found:
if current.getData() != item:
index +=1
current = current.getNext()
else:
found = True
if found:
return index
else:
return "Not Found"
# remove last item of the list
def pop(self):
current = self.head
previous = None
if current == None:
return "No item in list"
while current.getNext() != None:
previous = current
current = current.getNext()
previous.setNext(None)
return current.getData()
# remove item from the pos-th of the list
def delete(self, pos):
current = self.head
previous = None
index = 0
if current == None:
return "No item in list"
while index < pos and current != None:
previous = current
current = current.getNext()
index += 1
if current == None:
return "No item in list"
else:
if previous == None:
self.head = current.getNext()
else:
previous.setNext(current.getNext())
return current.getData()