Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def push(stack, item):
- stack[0] = item
- def pop(stack):
- if bool(len(stack)):
- print("Item popped : ", stack.pop(0))
- else : print("[WARNING] Underflow!!!")
- def peek(stack):
- stk_len = len(stack)
- if bool(stk_len):
- for i in range(stk_len):
- if i == 0:
- print(stack[i], " <== TOP\n")
- else: print(stack[i], end="\n")
- else: print("[WARNING] Stack is EMPTY!!!")
- def main():
- stack = []
- print("The Stack is Empty now..\nPlease Enter 5 items into it...")
- for i in range(5):
- inp = input("Enter an Item : ")
- stack.append(inp)
- print("[SUCCESS] 5 Items Successfully added to it.")
- print("\nPlease Enter the Operation that you want to do on it...")
- while True:
- print("\n1. Push\n2. Pop\n3. Peek\n4. Display Stack\n5. Exit")
- choice = int(input("Enter the Choice (Number only) : "))
- if choice == 1:
- item = input("Enter the Item to be pushed : ")
- push(stack, item)
- print("[SUCCESS] Item Successfully Pushed into the stack.")
- elif choice == 2 : pop(stack)
- elif choice == 3 : peek(stack)
- elif choice == 4 :
- for i in stack : print(i)
- elif choice == 5 :
- print("Exiting...")
- break
- else : print("[ERROR] Invalid Choice")
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement