-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
48 lines (40 loc) · 980 Bytes
/
Copy pathstack.py
File metadata and controls
48 lines (40 loc) · 980 Bytes
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
class Node:
def __init__(self, data):
self.data = data
self.nextn = None
class Stack:
def __init__(self):
self.top = None
def pop(self):
if self.top:
value = self.top.data
self.top = self.top.nextn
return value
else:
raise IndexError('The stack is empty')
def push(self, data):
newtop = Node(data)
if self.top:
newtop.nextn = self.top
self.top = newtop
def __str__(self):
outstr = ''
node = self.top
while node:
outstr += '\n'
outstr += node.data
node = node.nextn
return outstr
if __name__ == '__main__':
pile = Stack()
pile.push('a')
pile.push('b')
pile.push('c')
try:
print pile.pop()
print pile.pop()
print pile.pop()
print pile.pop()
except IndexError:
print 'Stack is empty'
print pile