0
0
Data Structures Theoryknowledge~6 mins

Stack operations (push, pop, peek) in Data Structures Theory - Full Explanation

Choose your learning style9 modes available
Introduction
Imagine you have a stack of plates. You can only add a new plate on top or take the top plate off. This problem of managing items in a last-in, first-out order is solved by stack operations.
Explanation
Push Operation
Push means adding a new item to the top of the stack. This operation increases the stack size by one and places the new item above all others. It is like placing a new plate on the top of a pile.
Push adds an item to the top of the stack.
Pop Operation
Pop removes the item from the top of the stack and returns it. This decreases the stack size by one. You can only pop the most recently added item, following the last-in, first-out rule.
Pop removes and returns the top item from the stack.
Peek Operation
Peek lets you look at the top item of the stack without removing it. This helps to check what is on top without changing the stack. It is like glancing at the top plate without taking it off.
Peek shows the top item without removing it.
Real World Analogy

Think of a stack of books on a table. You add a new book on top (push), take the top book off to read (pop), or just look at the title of the top book without moving it (peek).

Push Operation → Adding a new book on top of the stack
Pop Operation → Removing the top book to read it
Peek Operation → Looking at the title of the top book without removing it
Diagram
Diagram
Stack (top)
┌─────┐
│  3  │  <-- Peek shows this
├─────┤
│  2  │
├─────┤
│  1  │
└─────┘

Operations:
Push: Add new item on top
Pop: Remove top item
Peek: View top item
This diagram shows a stack with three items and illustrates push, pop, and peek operations on the top item.
Key Facts
StackA collection that follows last-in, first-out order.
PushOperation to add an item to the top of the stack.
PopOperation to remove and return the top item from the stack.
PeekOperation to view the top item without removing it.
Last-In, First-Out (LIFO)The order where the last added item is the first to be removed.
Code Example
Data Structures Theory
class Stack:
    def __init__(self):
        self.items = []

    def push(self, item):
        self.items.append(item)

    def pop(self):
        if not self.items:
            return None
        return self.items.pop()

    def peek(self):
        if not self.items:
            return None
        return self.items[-1]

stack = Stack()
stack.push(1)
stack.push(2)
stack.push(3)
print(stack.peek())  # Should print 3
print(stack.pop())   # Should print 3
print(stack.peek())  # Should print 2
OutputSuccess
Common Confusions
Thinking pop removes any item from the stack, not just the top one.
Thinking pop removes any item from the stack, not just the top one. Pop always removes the most recently added item at the top, following the LIFO rule.
Believing peek removes the item it shows.
Believing peek removes the item it shows. Peek only shows the top item without changing the stack.
Summary
Stack operations manage items in a last-in, first-out order.
Push adds an item on top, pop removes the top item, and peek shows the top item without removing it.
These operations help organize data where only the most recent item is accessible.