Write a Python program to implement a stack using a list
Expt. No.15
Page No.30
Stack using a list
Aim
To write a menu-driven Python program that implements a stack using a list, with push, pop and display operations and underflow checking.
Concepts used
Algorithm
- Create an empty list stack.
- push(item): append the item to the end of the list (the top).
- pop(): if the stack is empty, report underflow; otherwise remove and display the last item.
- display(): show the items from the top down to the bottom.
- Show a menu and repeat until the user chooses Exit.
Program
# Stack implemented using a list stack = [] def push(item): stack.append(item) print(item, "pushed") def pop(): if stack == []: print("Stack is empty (underflow)") else: print(stack.pop(), "popped") def display(): if stack == []: print("Stack is empty") else: print("Stack (top to bottom):") for i in range(len(stack) - 1, -1, -1): print(stack[i]) while True: print("1. Push 2. Pop 3. Display 4. Exit") choice = input("Enter your choice: ") if choice == "1": push(int(input("Enter the element: "))) elif choice == "2": pop() elif choice == "3": display() elif choice == "4": break else: print("Invalid choice")
A marked line continues the line above it. In your file, write it on the same line.
Output
$ python stack_using_list.py
1. Push 2. Pop 3. Display 4. Exit
Enter your choice: 1
Enter the element: 10
10 pushed
1. Push 2. Pop 3. Display 4. Exit
Enter your choice: 1
Enter the element: 20
20 pushed
1. Push 2. Pop 3. Display 4. Exit
Enter your choice: 1
Enter the element: 30
30 pushed
1. Push 2. Pop 3. Display 4. Exit
Enter your choice: 3
Stack (top to bottom):
30
20
10
1. Push 2. Pop 3. Display 4. Exit
Enter your choice: 2
30 popped
1. Push 2. Pop 3. Display 4. Exit
Enter your choice: 3
Stack (top to bottom):
20
10
1. Push 2. Pop 3. Display 4. Exit
Enter your choice: 2
20 popped
1. Push 2. Pop 3. Display 4. Exit
Enter your choice: 2
10 popped
1. Push 2. Pop 3. Display 4. Exit
Enter your choice: 2
Stack is empty (underflow)
1. Push 2. Pop 3. Display 4. Exit
Enter your choice: 4
✓ Printed by running this program, not typed by hand.
Result
10, 20 and 30 were pushed and displayed top to bottom as 30, 20, 10. Pops removed 30, then 20 and 10 (last in, first out), and the fourth pop reported underflow because the stack was empty.
Viva voce: what the examiner may ask
01What is a stack?
A linear data structure in which items are added and removed at one end, the top. It follows LIFO: last in, first out.
02Which list methods implement push and pop?
append() adds to the end (top) and pop() with no argument removes from the end.
03What is underflow?
Trying to pop from an empty stack.
04Why is global not needed in push() and pop()?
The functions change the list in place with methods; they never assign a new value to the name stack.
Variations for the lab test
- Add a peek option that shows the top element without removing it.
- Push only even numbers.
- Use the stack to reverse a string.
! Common mistake Using
stack.pop(0) removes the bottom element, turning the stack into a queue (first in, first out).