|  | 
|  | 1 | +import os | 
|  | 2 | + | 
|  | 3 | +class Stacks: | 
|  | 4 | + def __init__(self): | 
|  | 5 | + ''' | 
|  | 6 | + Initialises a empty list which will be used as Stack array. | 
|  | 7 | + ''' | 
|  | 8 | + self._data = [] | 
|  | 9 | + | 
|  | 10 | + def __len__(self): | 
|  | 11 | + ''' | 
|  | 12 | + Returns length of Stack> | 
|  | 13 | + ''' | 
|  | 14 | + return len(self._data) | 
|  | 15 | + | 
|  | 16 | + def isempty(self): | 
|  | 17 | + ''' | 
|  | 18 | + Returns True if stack is empty, else False. | 
|  | 19 | + ''' | 
|  | 20 | + return len(self._data) == 0 | 
|  | 21 | + | 
|  | 22 | + def push(self, e): | 
|  | 23 | + ''' | 
|  | 24 | + Pushes the passed element(e) on the stack. | 
|  | 25 | + ''' | 
|  | 26 | + self._data.append(e) | 
|  | 27 | + | 
|  | 28 | + def pop(self): | 
|  | 29 | + ''' | 
|  | 30 | + Removes the element on the top and returns it. | 
|  | 31 | + ''' | 
|  | 32 | + if self.isempty(): | 
|  | 33 | + print("Stack is Empty") | 
|  | 34 | + return | 
|  | 35 | + | 
|  | 36 | + return self._data.pop() | 
|  | 37 | + | 
|  | 38 | + def top(self): | 
|  | 39 | + ''' | 
|  | 40 | + Peeks at the element on the top of the stack. | 
|  | 41 | + ''' | 
|  | 42 | + if self.isempty(): | 
|  | 43 | + print("Stack is Empty") | 
|  | 44 | + return | 
|  | 45 | + | 
|  | 46 | + return self._data[-1] | 
|  | 47 | + | 
|  | 48 | + def display(self): | 
|  | 49 | + ''' | 
|  | 50 | + Utility function to display the stack. | 
|  | 51 | + ''' | 
|  | 52 | + if self.isempty(): | 
|  | 53 | + print("Stack is Empty") | 
|  | 54 | + return | 
|  | 55 | + print("Stack:") | 
|  | 56 | + for item in reversed(self._data): | 
|  | 57 | + print(item) | 
|  | 58 | + | 
|  | 59 | +############################################################################### | 
|  | 60 | + | 
|  | 61 | +def options(): | 
|  | 62 | + ''' | 
|  | 63 | + Prints Menu for operations | 
|  | 64 | + ''' | 
|  | 65 | + options_list = ['Push', 'Pop', 'Top',  | 
|  | 66 | + 'Display Stack', 'Exit'] | 
|  | 67 | + | 
|  | 68 | + print("MENU") | 
|  | 69 | + for i, option in enumerate(options_list): | 
|  | 70 | + print(f'{i + 1}. {option}') | 
|  | 71 | + | 
|  | 72 | + choice = int(input("Enter choice: ")) | 
|  | 73 | + return choice | 
|  | 74 | + | 
|  | 75 | +def switch_case(choice): | 
|  | 76 | + ''' | 
|  | 77 | + Switch Case for operations | 
|  | 78 | + ''' | 
|  | 79 | + os.system('cls') | 
|  | 80 | + if choice == 1: | 
|  | 81 | + elem = int(input("Enter Item: ")) | 
|  | 82 | + S.push(elem) | 
|  | 83 | + | 
|  | 84 | + elif choice == 2: | 
|  | 85 | + print('Popped item is: ', S.pop()) | 
|  | 86 | + | 
|  | 87 | + elif choice == 3: | 
|  | 88 | + print("Item on top is: ", S.top()) | 
|  | 89 | + | 
|  | 90 | + elif choice == 4: | 
|  | 91 | + print("Stack: ", end='') | 
|  | 92 | + S.display() | 
|  | 93 | + print("\n") | 
|  | 94 | + | 
|  | 95 | + elif choice == 5: | 
|  | 96 | + import sys | 
|  | 97 | + sys.exit() | 
|  | 98 | + | 
|  | 99 | +############################################################################### | 
|  | 100 | + | 
|  | 101 | +S = Stacks() | 
|  | 102 | +while True: | 
|  | 103 | + choice = options() | 
|  | 104 | + switch_case(choice) | 
0 commit comments