-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.py
44 lines (35 loc) · 876 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
# Stack Module
class Node:
def __init__(self, item, link):
self.item = item
self.next = link
class StackLink:
def __init__(self):
self.top = None
self.size = 0
def push(self, item):
self.top = Node(item, self.top)
self.size += 1
def pop(self):
if self.size != 0:
temp = self.top.item
self.top = self.top.next
self.size -= 1
return temp
def print_stack(self):
p = self.top
while p:
if p.next != None:
print(p.item, '-->', end = '')
else:
print(p.item, end = '')
p = p.next
print()
if __name__ == "__main__":
top = StackLink()
top.push("apple")
top.push("mango")
top.push("cherry")
top.push("banana")
top.pop()
top.print_stack()