Skip to content

Commit 6baccff

Browse files
committed
Added linked list
0 parents  commit 6baccff

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed

dataStructures.py

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#!/usr/bin/env python
2+
3+
4+
class Linked_List_Node:
5+
6+
def __init__(self, payload, parent = None, child = None):
7+
self.payload = payload
8+
self.parent = parent
9+
self.child = child
10+
11+
def has_child(self):
12+
return not self.child == None
13+
14+
def has_parent(self):
15+
return not self.parent == None
16+
17+
def get_child(self):
18+
if self.has_child():
19+
return self.child
20+
else: return None
21+
22+
def set_child(self, child):
23+
self.child = child
24+
25+
def get_parent(self):
26+
if self.has_parent():
27+
return self.parent
28+
else: return None
29+
30+
def set_parent(self, parent):
31+
self.parent = parent
32+
33+
def get_payload(self):
34+
return self.payload
35+
36+
def set_payload(self, payload):
37+
self.payload = payload
38+
39+
class Linked_List:
40+
41+
def __init__(self):
42+
self.head = None
43+
44+
def __len__(self):
45+
toReturn = 0
46+
current_node = self.head
47+
while not current_node == None:
48+
toReturn += 1
49+
current_node = current_node.get_child()
50+
return toReturn
51+
52+
def add(self, data):
53+
54+
# make new node
55+
newNode = Linked_List_Node(data)
56+
# case - List is empty
57+
if self.head == None:
58+
self.head = newNode
59+
# case - List is not empty
60+
else:
61+
current_node = self.head
62+
63+
# traverse through list to end
64+
while current_node.has_child():
65+
current_node = current_node.get_child()
66+
67+
# insert into list
68+
current_node.set_child(newNode)
69+
newNode.set_parent(current_node)
70+
71+
def find_in_list(self,term):
72+
current_node = self.head
73+
# traverse the list until end or match
74+
while (not current_node.get_payload() == term) and current_node.has_child():
75+
print "checking {} vs {}".format(current_node.get_payload(),term)
76+
current_node = current_node.get_child()
77+
print current_node.get_payload()
78+
if current_node.get_payload() == term:
79+
return current_node
80+
else: return None
81+
82+
def delete(self, term):
83+
84+
# find the node in the list
85+
node = self.find_in_list(term)
86+
if not node == None:
87+
parent = node.get_parent()
88+
child = node.get_child()
89+
90+
# top of list
91+
if parent == None:
92+
93+
# last in list
94+
if child == None:
95+
self.head = None
96+
else:
97+
self.head = child
98+
else:
99+
100+
# last in list
101+
if child == None:
102+
parent.set_child(None)
103+
else:
104+
child.set_parent(parent)
105+
parent.set_child(child)
106+
else: print "Item not in list"
107+
108+
def get_item(self,term):
109+
node = self.find_in_list(term)
110+
111+
if node == None:
112+
return None
113+
else: return node.get_payload()
114+
115+
def print_list(self):
116+
current_node = self.head
117+
if not current_node == None:
118+
print current_node.get_payload(),
119+
current_node = current_node.get_child()
120+
while not current_node == None:
121+
print ", ",
122+
print current_node.get_payload(),
123+
124+
current_node = current_node.get_child()
125+
126+
def pop(self):
127+
if not self.head == None:
128+
toReturn = self.head.get_payload()
129+
self.head = self.head.get_child()
130+
return toReturn
131+
else: return None
132+
133+

0 commit comments

Comments
 (0)