4.1.2.2 Stack Operations
Computational Structures
There are two basic operations in a stack:
- Push Operation: Push means adding an item to the top of the stack. In Python, we use list data structure to implement the stack. Therefore, we use the list function append () to add an element to the top of the stack.
- Pop Operation: Pop means removing the item from the top of the stack. We use list pop() function to remove the element from the top of the stack.
Example: Imagine a stack of books shown in Figure 4.2, on a desk, you can only add a book on top of the stack or take the top book off. Here is a Python implementation of the book stack described above.
python
# Create an empty stack of books