-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_list.py
75 lines (56 loc) · 1.39 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
# Implementing a singly linked list.
class Node(object):
def __init__(self, data=None):
self.data = data
self.next_node = None
def get_data(self):
return self.data
def set_data(self, data):
self.data = data
def get_next(self):
return self.next_node
def set_next(self, new_next):
self.next_node = new_next
class UnorderedList(object):
def __init__(self, head=None):
self.head = head
def isEmpty(self):
return self.head is None
def add_node(self, data):
node = Node(data)
node.next_node = self.head
self.head = node
def size(self):
current = self.head
count = 0
while current:
count += 1
current = current.get_next()
return count
def search(self, data):
current = self.head
while current:
if current.data == data:
return current
current = current.get_next()
return None
def remove(self, data):
if self.isEmpty():
return
previous = None
current = self.head
found = False
while not found:
if current.data == data:
found = True
if previous is None:
self.head = current.get_next()
else:
previous.set_next(current.get_next)
def show_items(self):
if self.head is None:
print 'Linked List is empty'
current = self.head
while current:
print current.get_data()
current = current.get_next()