Stacks

Stacks in C++ are part of the Standard Template Library (STL). A stack is a linear data structure that follows the LIFO (Last In, First Out) principle.

What is a Stack?

A stack is a container where:

  • The last element added is the first one removed
  • Elements are inserted and removed from the top only

Why Use Stacks?

Stacks are useful because they:

  • Manage function calls in programs
  • Support undo and redo operations
  • Help in expression evaluation
  • Are used in DFS (Depth First Search)

Real-Life Example

Think of a stack of plates:

  • You add plates on top
  • You remove the top plate first
  • You cannot remove a plate from the middle

This is exactly how a stack works.

Stack Header File

To use stacks in C++, include the <stack> library.

#include <stack>

Declaring a Stack

stack<int> s;

Adding Elements (push)

Elements are added to the top of the stack.

s.push(10);
s.push(20);
s.push(30);

Removing Elements (pop)

Elements are removed from the top.

s.pop();

Accessing Top Element

cout << s.top();

Example Program of Stack

#include <iostream>
#include <stack>
using namespace std;

int main() {

stack<int> s;

s.push(1);
s.push(2);
s.push(3);

cout << "Top: " << s.top() << endl;

s.pop();

cout << "After pop, Top: " << s.top() << endl;

return 0;
}

Output

Top: 3
After pop, Top: 2

Common Stack Functions

FunctionPurpose
push()Add element on top
pop()Remove top element
top()Access top element
size()Get number of elements
empty()Check if stack is empty

How Stack Works

  1. Elements are added on top
  2. Last element added is first removed
  3. Only top element is accessible
  4. Works in reverse order

Difference Between Stack and Queue

StackQueue
LIFO (Last In First Out)FIFO (First In First Out)
Insert/remove from topInsert rear, remove front
Used in recursionUsed in scheduling

Important Points About Stacks

  • Follows LIFO principle
  • Only top element is accessible
  • No random access allowed
  • Simple and efficient structure

Advantages of Stacks

  • Easy to implement
  • Efficient memory usage
  • Useful in backtracking
  • Helps manage program execution

Common Mistakes with Stacks

  • Trying to access middle elements
  • Calling top() on empty stack
  • Forgetting to check if stack is empty
  • Confusing stack with queue

Real-Life Applications

Stacks are used in:

  • Function call management (call stack)
  • Undo/redo systems
  • Expression evaluation
  • Browser history navigation
  • DFS algorithm in graphs

Why Stacks are Important

Stacks are important because they:

  • Control program execution flow
  • Help in algorithm design
  • Manage temporary data efficiently
  • Are widely used in system operations

Conclusion

Stacks in C++ are powerful STL containers that follow the LIFO principle. They are widely used in programming, system design, and algorithms where the last inserted element needs to be processed first.

Home » Professional C++ > STL (Standard Template Library) > Stacks