Page 1 - DATA STRUCTURE-STACKS & QUEUES
P. 1
DATA STRUCTURE
(STACKS & QUEUES)
A STACK is a container of objects that are inserted and removed according to the last-in first-out
(LIFO) principle.
In the stacks only two operations are allowed: push the item into the stack, and pop the item out of
the stack. Elements can be added and removed from the stack only at the top.
#STACK implementation using LIST
top=-1 #list empty
stk=[]
def isEmpty():
if stk==[]:
return True
else:
return False
def PUSH():
x=int(input("Enter the next element: "))
stk.append(x)
global top
top=top+1
def POP():
if isEmpty():
print("Stack is empty")
return
a=stk.pop()
global top
top=top-1
print("Element removed is: ", a)
def Display():
if isEmpty():
print("Stack is empty")
return
print("Stack elements are:\n")
for i in range(top, -1, -1):
print(stk[i])
while True:
print("\n\nStack Operations")
print("1.Push")
print("2.Pop")
print("3.Display")