-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy_linked_list.py
More file actions
79 lines (63 loc) · 1.83 KB
/
my_linked_list.py
File metadata and controls
79 lines (63 loc) · 1.83 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
from node import Node
class UnorderedList:
# Create the Head for the list
def __init__(self):
self.head = None
def isEmpty(self):
return self.head == None
# Adding to the UnorderedList
def add(self, item):
temp = Node(item)
temp.setNext(self.head)
self.head = temp
# Adding script to calculate size of the linked list
def size(self):
current = self.head
count = 0
# Start traversal
while not current == None:
count = count + 1
current = current.getNext()
return count
# Add script to search for item
def search(self, item):
current = self.head
found = False
while not current == None and found == False:
if current.getData() == item:
found = True
else:
current = current.getNext()
return found
# Add method to remove item
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())
# Add method to append to the list
def append(self, item):
temp = Node(item)
temp.getNext() = None
current = self.head
while not current.getNext() == None:
temp = current
current = current.getNext()
if current.getNext() == None:
mylist = UnorderedList()
mylist.add(23)
mylist.add(22)
mylist.add(22)
mylist.add("Hola")
print(mylist.size())
# print(mylist.search(24))
print(mylist.remove(23))