0% found this document useful (0 votes)
4 views

Stack_Program

The document provides a Python program that implements a stack using a list with a menu-driven interface. It allows users to perform operations such as push, pop, display, peek, and exit. The program handles empty stack conditions and prompts the user for input choices.

Uploaded by

Harmeet Dodia
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Stack_Program

The document provides a Python program that implements a stack using a list with a menu-driven interface. It allows users to perform operations such as push, pop, display, peek, and exit. The program handles empty stack conditions and prompts the user for input choices.

Uploaded by

Harmeet Dodia
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

'''

Write a menu driven program to python to implement stack using a list


and perform following functions:
1. Push
2. Pop
3. Display
4. Peek
5. Exit

'''
stack=[]
while True:
print("1. PUSH")
print("2. POP")
print("3. DISPLAY")
print("4. PEEK")
print("5. EXIT")
ch=int(input("Enter your choice[1/2/3/4/5]:"))
if ch==1:
a=input("Enter element : ")
stack.append(a)
print("Element",a,"has been inserted into stack")
elif ch==2:
if stack==[]:
print("Stack is empty...")
else:
print("deleted element is : ",stack.pop())
elif ch==3:
if stack==[]:
print("Stack is empty...")
else:
for i in range(len(stack)-1,-1,-1):
print(stack[i])
elif ch==4:
if stack==[]:
print("Stack is empty...")
else:
a=stack[len(stack)-1]
print("Last element of stack : ",a)
elif ch==5:
break
else:
print("Enter valid choice...!")

You might also like