forked from The-Open-Source-Society/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Stacks.py
57 lines (41 loc) · 1.08 KB
/
Stacks.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
class node:
def __init__(self, value):
self.data = value
self.next = None
class Stacks:
def __init__(self):
self.TOS = None
def Push(self, value):
new_node = node(value)
new_node.next = self.TOS
self.TOS = new_node
def Pop(self):
if self.TOS == None:
print("Stack is empty")
else:
self.TOS = self.TOS.next
def Print_Stack(self):
if self.TOS == None:
print("Stack is empty")
else:
print("Top of stack is " + str(self.TOS.data))
if self.TOS.next != None:
print("Other elements are :", end = " ")
current = self.TOS.next
while current != None:
print(current.data, end = " ")
current = current.next
print()
Stack = Stacks()
for i in range(0, 5):
Stack.Push(i)
Stack.Print_Stack()
for i in range(0, 6):
Stack.Pop()
Stack.Print_Stack()
''' Output
Top of stack is 4
Other elements are : 3 2 1 0
Stack is empty
Stack is empty
'''