forked from mckrd/py_repo_beginner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.py
45 lines (43 loc) · 803 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
#Implement stack using a list
stack = []
top = -1
max_size = int(input('Enter max size of stack'))
def push(num):
global top
global stack
if top==max_size-1:
print("Stack full.Pop to insert any more element")
return
top = top + 1
stack.append(num)
def pop():
global top
global stack
if top==-1:
print("Stack empty")
return
top = top - 1
element = stack.pop(len(stack)-1)
def print_stack():
for i in reversed(stack):
print(i)
#Stack is initially empty. Lets push few elements
push(30)
push(60)
push(120)
push(240)
pop()
print_stack()
#Empty the stack by popping all elements
pop()
pop()
pop()
print_stack()
#check for underflow
pop()
globals()
for i in range(0,max_size):
push(12*2*i)
print_stack()
#check for overflow
push(200)