the art of
Algorithm
Notes on Analysis and Design



Stack using Array

Implementing stack using arrays in python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#function to create stack
def createstack():
	stack=[]
	return stack
#Function to push an item on to stack
def push(stack,item):
	if len(stack) !=0:
		return 0
	stack.append(item)
	print ("item appended is" ,item)
#Function to pop an item from stack
def pop(item):
	if len(stack)==0:
		return 0
	return stack.pop()
#Main program to test above function
stack=createstack()
push(stack, str(10))
push(stack, str(20))
push(stack, str(30))
print(pop(stack) + " popped from stack")