-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.py
executable file
·46 lines (34 loc) · 983 Bytes
/
stack.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
#!/usr/bin/env python
class Stack_Node:
def __init__(self,payload = None, child = None):
self.payload = payload
self.child = child
def get_child(self):
return self.child
def get_payload(self):
return self.payload
def set_child(self, data):
self.child = data
def set_payload(self, payload):
self.payload = payload
class Stack:
def __init__(self):
self.root = None
def push(self, data):
newNode = Stack_Node(data)
if self.root == None:
self.root = newNode
else:
newNode.set_child(self.root)
self.root = newNode
def pop(self):
if self.root == None:
return None
else:
toReturn = self.root
self.root = self.root.get_child()
return toReturn.get_payload()
def is_empty(self):
return self.root == None
while not stack.is_empty():
print(stack.pop())