Advertisement
sol4r

STACK DATASTRUCTURE

Oct 3rd, 2020
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.64 KB | None | 0 0
  1. stack = []
  2. MAX_SIZE = 5
  3.  
  4. def push(p):
  5.     if len(stack)==MAX_SIZE:
  6.         print("overflow")
  7.     else:
  8.         stack.append(p)
  9.     return stack
  10.  
  11.  
  12. def pop():
  13.     if len(stack) != 0:
  14.         del stack[-1]
  15.     else:
  16.         print("underflow")
  17.     return (stack)
  18.  
  19.  
  20. while True:
  21.  
  22.     print("1 for push ")
  23.     print("2 for pop ")
  24.     print("3 for exit")
  25.     choice = int(input())
  26.     if choice == 1:
  27.         n = int(input("enter input: "))
  28.         result = push(n)
  29.         print(result)
  30.     elif choice == 2:
  31.         result = pop()
  32.         print(result)
  33.     elif choice == 3:
  34.         break
  35.     else:
  36.         print("wrong choice")
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement