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

stack

The document outlines a Python program for stack operations including push, pop, peek, and display functionalities. It prompts the user to enter a maximum number of elements and provides a menu for various stack operations. The program handles stack overflow and underflow conditions appropriately.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

stack

The document outlines a Python program for stack operations including push, pop, peek, and display functionalities. It prompts the user to enter a maximum number of elements and provides a menu for various stack operations. The program handles stack overflow and underflow conditions appropriately.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

stack = []

max_val = int(input("Enter the maximum number or elements:"))

while True:
print("STACK OPERATIONS...")
print("1.Push")
print("2.Pop")
print("3.Peek")
print("4.Display Stack")
print("5.Exit")

choice = int(input("Enter your choice:"))


if choice == 1:
if len(stack) == max_val:
print("Stack Overflow!")
else:
val = int(input("Enter the element to be pushed:"))
stack.append(val)
elif choice == 2:
if len(stack) == 0:
print("Stack Underflow!")
else:
val = stack.pop()
print(val, "popped out of the Stack...")
elif choice == 3:
val = stack[-1]
print("Peek is", val)
elif choice == 4:
print("Displaying all Stack elements...")
print(stack)
elif choice == 5:
break
else:
print("Invalid choice!")

You might also like