-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked_list.py
148 lines (110 loc) · 3.17 KB
/
linked_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
142
143
144
145
146
147
148
class Node(object):
def __init__(self, data):
self.data = data
self.next = None
def get_data(self):
return self.data
def get_next(self):
return self.next
def set_data(self, data):
self.data = data
def set_next(self, next_node):
self.next = next_node
class UnorderedList(object):
def __init__(self):
self.head = None
def is_empty(self):
return self.head == None
def add(self, item):
new_node = Node(item)
new_node.set_next(self.head)
self.head = new_node
def size(self):
current = self.head
count = 0
while current is not None:
count += 1
current = current.get_next()
return count
def search(self, item):
current = self.head
found = False
while current is not None and not found:
if current.get_data() == item:
found = True
else:
current = current.get_next()
return found
def remove(self, item):
current = self.head
prev = None
found = False
while not found:
if current.get_data == item:
found = True
else:
prev = current
current = current.get_next()
if prev is None:
self.head == current.get_next()
else:
prev.set_next(current.get_next())
def append(self, item):
current = self.head
end_item = False
new_node = Node(item)
while not end_item:
if current.get_next() is None:
end_item = True
current.set_next(new_node)
else:
current = current.get_next()
my_list = UnorderedList()
my_list.add(4)
my_list.add(9)
my_list.add("asd")
my_list.append("appended item")
# print(my_list.search(9))
print(my_list.size())
class OrderedList(object):
def __init__(self):
self.head = None
def is_empty(self):
return self.head == None
def size(self):
current = self.head
count = 0
while current is not None:
count += 1
current = current.get_next()
return count
def search(self, item):
current = self.head
found = False
stop = False
while current is not None and not found and not stop :
if current.get_data() == item:
found = True
else:
if current.get_data() > item:
stop = True
else:
current = current.get_next()
return found
def add(self, item):
current = self.head
prev = None
stop = False
while current is not None and not stop:
if current.get_data() > item:
stop = True
else:
current = current.get_next()
prev = current
new_item = Node(item)
if current == self.head:
self.head = new_item
new_item.set_next(current)
else:
prev.set_next(new_item)
new_item.set_next(current)