forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stacks.py
60 lines (45 loc) · 1.11 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
58
59
60
class Stacks:
def __init__(self):
self.size = 10
self.tos = -1
self.array = [0] * self.size
def __init__(self, size):
self.size = size
self.tos = -1
self.array = [0] * size
def push(self, value):
if self.tos == self.size - 1:
print("Stack is full")
else:
self.tos += 1
self.array[self.tos] = value
def pop(self):
if self.tos == -1:
print("Stack is empty")
else:
self.tos -= 1
def traverse(self):
temp = self.tos - 1
if self.tos == -1:
print("Stack is empty")
else:
print("Top of stack is : " + str(self.array[self.tos]))
if temp > -1:
print("Other :", end = " ")
while temp > -1:
print(self.array[temp], end = " ")
temp -= 1
print()
st = Stacks(5)
for i in range(0, 6):
st.push(i)
st.traverse()
for i in range(0, 6):
st.pop()
'''
Output
Stack is full
Top of stack is : 4
Other : 3 2 1 0
Stack is empty
'''