0% found this document useful (0 votes)
3 views3 pages

Question 4

This document contains a Python code for a menu-driven stack implementation that supports PUSH, POP, and DISPLAY operations. It defines functions to check if the stack is empty, push an item onto the stack, pop an item from the stack, and display the current stack. The user is prompted to choose an operation in a loop until they decide to exit.

Uploaded by

tyagi.devanshpro
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Question 4

This document contains a Python code for a menu-driven stack implementation that supports PUSH, POP, and DISPLAY operations. It defines functions to check if the stack is empty, push an item onto the stack, pop an item from the stack, and display the current stack. The user is prompted to choose an operation in a loop until they decide to exit.

Uploaded by

tyagi.devanshpro
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#Write a python menu driven code to perform PUSH,POP and DISPLAY operations

def IsEmpty(stk):
if stk==[]:
return True
else:
return False
def Push(stk,item):
[Link](item)
def Pop(stk):
if IsEmpty(stk):
return 'Underflow'
else:
item=[Link]()
return item
def Display(stk):
if IsEmpty(stk):
print('Empty Stack')
else:
top=len(stk)-1
print(stk[top],'<-- Top')
for i in range(top-1,-1,-1):
print(i)
stack=[]
while True:
print('STACK OPERATIONS')
print('1. Push')
print('2. Pop')
print('3. Display')
print('4. Exit')
ch=int(input('Enter your choice:'))
if ch==1:
item=int(input('Enter item:'))
Push(stack,item)
elif ch==2:
item=Pop(stack)
if item=='Underflow':
print('Underflow')
else:
print('Popped item is',item)
elif ch==3:
Display(stack)
elif ch==4:
break
else:
print('Invalid choice')

OUTPUT:

You might also like