This Python program implements a stack to store country and capital name pairs. It provides functions to insert, delete, display and peek at elements in the stack. The main menu allows the user to choose these options and provides an example run where elements are inserted and deleted from the stack and its contents are displayed.
This Python program implements a stack to store country and capital name pairs. It provides functions to insert, delete, display and peek at elements in the stack. The main menu allows the user to choose these options and provides an example run where elements are inserted and deleted from the stack and its contents are displayed.
This Python program implements a stack to store country and capital name pairs. It provides functions to insert, delete, display and peek at elements in the stack. The main menu allows the user to choose these options and provides an example run where elements are inserted and deleted from the stack and its contents are displayed.
def insert(): co=input('Enter the country name : ') ca=input('Enter the capital name : ') stack.append([co,ca]) top=stack[-1] def delete(): if stack == []: print('Underflow') else: print('Deleted element is ',stack.pop()) if len(stack)==0: top=None else: top=stack[-1] def display(): if stack == []: print('Underflow') return print('Content of the stack : ') for i in range(len(stack)-1,-1,-1): print(stack[i]) def peek(): if stack == []: print('Underflow') return top=stack[-1] print('The topmost element is : ',top) # Main Menu stack=[] print(''' MENU 1. Insert 2. Delete 3. Display stack 4. Peek 5. Exit''') while True: ch=int(input('Enter your choice : ')) if ch==1:insert() elif ch==2:delete() elif ch==3:display() elif ch==4:peek() elif ch==5: print('Exiting') break else: print('Wrong choice') OUTPUT MENU 1. Insert 2. Delete 3. Display stack 4. Peek 5. Exit Enter your choice : 1 Enter the country name : India Enter the capital name : new Delhi Enter your choice : 1 Enter the country name : Japan Enter the capital name : Tokyo Enter your choice : 2 Deleted element is ['Japan', 'Tokyo'] Enter your choice : 3 Content of the stack : ['India', 'new Delhi'] Enter your choice : 4 The topmost element is : ['India', 'new Delhi'] Enter your choice : 5 Exiting >>>