Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def push(STACK,item):
- STACK.append(item)
- def is_empty(STACK):
- if len(STACK)==0:
- return True
- else:
- return False
- def pop(STACK):
- if is_empty(STACK)==True:
- ret="STACK UNDERFLOW"
- print("STACK UNDERFLOW")
- else:
- ret=STACK.pop()
- return ret
- def display(STACK):
- if is_empty(STACK)==True:
- print("STACK IS EMPTY")
- else:
- print("The elements of the stack are")
- for i in range(len(STACK)-1,-1,-1):
- print(STACK[i])
- def peek(STACK):
- if is_empty(STACK)==True:
- ret="STACK UNDERFLOW"
- else:
- ret=STACK[len(STACK)-1]
- return ret
- # Stack is initially empty
- STACK = []
- while True:
- print("\nStack Options")
- print("1. Push")
- print("2. Pop")
- print("3. Display Stack")
- print("4. Peek")
- print("5. Exit")
- choice = int(input("Enter your choice (1-5): "))
- if choice == 1:
- item = int(input("Enter the item: "))
- push(STACK, item)
- elif choice == 2:
- item = pop(STACK)
- if item != "STACK UNDERFLOW":
- print(item, "was deleted from the stack")
- else:
- print("STACK UNDERFLOW")
- elif choice == 3:
- display(STACK)
- elif choice == 4:
- item = peek(STACK)
- print(item, "is present at the top of the stack")
- elif choice == 5:
- break
- else:
- print("Invalid choice")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement