Advertisement
Anonymous0069

Stack_functions

Jul 14th, 2023 (edited)
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.30 KB | Source Code | 0 0
  1. stack = []
  2. top = None
  3.  
  4. def listbox():
  5.     print("1. Insert Data")
  6.     print("2. Delete data")
  7.     print("3. Display data")
  8.     print("4. Exit")
  9.  
  10. def insert(stack,top):
  11.     insele = int(input("Enter the number of element you want to enter:"))
  12.     for i in range(insele):
  13.         ele = input("Enter the element:")
  14.         stack.append(ele)
  15.     top = len(stack) - 1
  16.    
  17.  
  18. def delete(stack, top):
  19.    
  20.     if stack == []:
  21.         print("Underflow!")
  22.  
  23.     else:
  24.         delele = int(input("Enter the number of elements you want to delete(LIFO):"))
  25.         for i in range(delele):
  26.             stack.pop()
  27.         top = len(stack) - 1
  28.  
  29.         print("Operation accomplished!")
  30.  
  31. def display(stack,top):
  32.     lst = []
  33.     top = len(stack) - 1
  34.  
  35.     if stack == []:
  36.         print("Underflow!")
  37.        
  38.    
  39.     else:
  40.         print(stack[top],"<---top")
  41.         for i in range(0,top):
  42.             lst.append(stack[i])
  43.         lst.reverse()
  44.         for i in lst:
  45.             print(i)
  46.         print("Operation accomplished!")
  47.  
  48.  
  49. while True:
  50.     listbox()
  51.     choice = int(input("Enter your choice:"))
  52.  
  53.     if choice  == 1:
  54.         insert(stack, top)
  55.  
  56.     if choice == 2:
  57.         delete(stack, top)
  58.  
  59.     if choice == 3:
  60.         display(stack,top)
  61.  
  62.     if choice == 4:
  63.         break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement