Stacks & LIFO
One open end, one rule: the last element in is the first element out. Build the same contract twice — once on a fixed array, once on a chain of nodes.
- 01Cover
- 02What Is a Stack?
- 03The LIFO Sequence
- 04std::stack
- 05Array Structure
- 06Push
- 07Pop & Peek
- 08Display & Run
- 09Overflow & Underflow
- 10Linked Structure
- 11Pointer Push & Pop
- 12Peek & Display
- 13Integer Stack Run
- 14String Stack Run
- 15Array vs. Linked
- 16Key Takeaways
What Is a Stack?
A linear collection with one open end: everything goes in and comes out at the top, so the newest element leaves first.
The LIFO Sequence
Three pushes fill the stack; three pops empty it. The exit order is the entry order, reversed.
Each value lands on the one before it. After three, 30 is on top.
30 leaves first. 20 becomes the new top.
20 leaves; the element beneath it is exposed.
10 leaves last, and the stack is empty.
30 is the newest element, so 30 is the top. Three plates, one open end.
The Built-in Stack
The program pushes 10, 20 and 30, reads the top, then drains the stack with top() and pop().
Import the STL container.
Start with an empty stack of ints.
Each call lands on the current top.
Returns 30 without removing it.
Print, pop, repeat: 30 20 10.
s.top() reads arr[top] — 30 — without changing the stack. Reading is not removing.
A Stack in an Array
Five cells, one integer: top is the index of the highest written cell, and -1 means the stack is empty.
arr[0].Push — One Cell Higher
push() guards capacity, moves top up, and writes into arr[top]. Nothing else moves.
10 lands in cell 0; top becomes 0.
20 is written above 10; top becomes 1.
30 is written above 20; top becomes 2.
Pop and Peek
Both guard with isEmpty() and read arr[top]. Then pop() decrements top; peek() stops.
isEmpty() before reading.arr[top]; only pop() runs top--.pop() shrinks the stack; peek() changes nothing.Display and the First Run
display() walks from top down to 0, newest first. The first run pushes three values, displays, peeks, then pops twice.
Empty stack: print a message and stop.
Print the heading once.
Walk from the top down to index 0.
Print each cell: 30, 20, 10.
Peek reads 30; two pops leave 10.
display() starts at i = top and walks down to 0: 30, then 20, then 10.
Overflow and Underflow
Five cells means the sixth push must be refused; an empty stack must refuse pop(). Both guards print and change nothing.
push(80) asks for a sixth cell in a five-cell array. isFull() refuses it and nothing is written.
A Stack of Nodes
No array, no MAX. Each node holds a value and a pointer; the stack is one pointer to the first node.
A value plus the address below it.
One pointer is the whole stack.
The last node marks the base.
Push and Pop
push() allocates a node at the head; pop() steps top forward and deletes the node behind it.
top = nullptr;top == nullptrnew Node<T>(value)newNode->next = top;top = newNode;temp = top; top = top->next;delete temp;while (!isEmpty()) pop();pop() prints top->data (30), then saves the same pointer in temp. Top has not moved yet.
Peek and Display
peek() reads top->data in one line. display() walks a temporary pointer from the top to nullptr.
Both methods start with the same guard.
peek() prints the top value; top never moves.
display() copies top into temp.
Walk until the pointer falls off the end.
Print, then advance. The chain is only read.
The while condition is true (temp != nullptr), so temp->data is printed: 30.
The Integer Stack
The first half of main() runs the integer stack end to end, in source order.
| Call | Stack after | Output |
|---|---|---|
| Stack<int> s | (empty) | — |
| push(10) | 10 | 10 pushed into stack. |
| push(20) | 20 10 | 20 pushed into stack. |
| push(30) | 30 20 10 | 30 pushed into stack. |
| display() | 30 20 10 | Stack elements (top to bottom): 30 20 10 |
| peek() | unchanged | Top element is: 30 |
| pop() | 20 10 | 30 popped from stack. |
| display() | 20 10 | Stack elements (top to bottom): 20 10 |
main() moves on; the destructor drains them.display() walks temp from 30 to 10 and prints each data field: 30 20 10.
The String Stack
Nothing in Stack<T> mentions int except the type argument. The same code compiles for strings.
int becomes string.top, push, pop, peek and display are shared.~Stack() pops until the chain is empty. The transcript stops early; the code does not.display() walks the same top-to-bottom path and prints Carol Bob Alice.
Two Ways to Be a Stack
int arr[MAX]Nodes on the heap, Node<T>*int topNode pointer, Node<T>* toptop == -1top == nullptrtop == MAX - 1No full state exists~Stack() pops and deletes each nodeintAny T — int, string, and so onKey Takeaways
int top over arr[MAX]; guard -1 and MAX - 1.Node<T>* top; push and pop at the head.~Stack() drains the chain.stl-stack.cppstack-array.cppstack-linked-list.cpp