C++ stacks


Introduction

In C++, a stack is a container adaptor that operates on the Last In, First Out (LIFO) principle, meaning the last element added is the first one to be removed. It is implemented using the <stack> header in the C++ Standard Library and provides a simple interface for adding (pushing) and removing (popping) elements. The stack container is widely used in algorithms and problems involving backtracking, parsing, and maintaining a sequence of operations.

A stack supports the following basic operations:

  • push(): Adds an element to the top of the stack.
  • pop(): Removes the element from the top of the stack.
  • top(): Returns the element at the top of the stack without removing it.
  • size(): Returns the number of elements in the stack.
  • empty(): Checks whether the stack is empty.

Syntax

To declare a stack:

std::stack<T> stack_name;

Where:

  • T: The type of elements in the stack (e.g., int, std::string).

You can also use custom containers as the underlying container for the stack (default is std::deque).

std::stack<T, Container> stack_name;

Where:

  • Container: The underlying container, such as std::vector or std::deque.

Declaring and Initializing a Stack

Example 1: Basic Stack Declaration

#include <iostream>
#include <stack>

int main() {
    // Declare a stack of integers
    std::stack<int> stack;

    // Pushing elements into the stack
    stack.push(10);
    stack.push(20);
    stack.push(30);

    // Displaying the top element
    std::cout << "Top element: " << stack.top() << std::endl;  // Output: 30

    return 0;
}

Explanation:

  • push(10), push(20), and push(30) add the elements to the stack.
  • top() gives the top element of the stack without removing it.

Basic Operations of std::stack

1. push()

The push() function adds an element to the top of the stack.

stack.push(40);  // Adds 40 to the top of the stack

2. pop()

The pop() function removes the element from the top of the stack. It does not return the removed element.

stack.pop();  // Removes the top element

3. top()

The top() function returns the element at the top of the stack without removing it.

std::cout << "Top element: " << stack.top() << std::endl;  // Displays the top element

4. size()

The size() function returns the number of elements currently in the stack.

std::cout << "Stack size: " << stack.size() << std::endl;

5. empty()

The empty() function checks whether the stack is empty.

if (stack.empty()) {
    std::cout << "Stack is empty!" << std::endl;
} else {
    std::cout << "Stack is not empty!" << std::endl;
}

Iterating Through a Stack

Unlike other containers like vectors or lists, a stack does not allow direct iteration over its elements. To process the elements, you must pop them off one by one. Here's an example:

Example 1: Popping Elements from the Stack

#include <iostream>
#include <stack>

int main() {
    std::stack<int> stack;
    stack.push(10);
    stack.push(20);
    stack.push(30);

    // Popping elements from the stack
    while (!stack.empty()) {
        std::cout << stack.top() << " ";  // Display the top element
        stack.pop();  // Remove the top element
    }

    return 0;
}

Output:

30 20 10

Explanation:

  • top() returns the element at the top of the stack.
  • pop() removes the element from the stack, allowing the next element to become the new top.

Stack with Custom Containers

The std::stack container adaptor uses std::deque by default as its underlying container, but you can choose to use other containers like std::vector or std::list as the base container.

Example 2: Stack with std::vector as the Underlying Container

#include <iostream>
#include <stack>
#include <vector>

int main() {
    // Declare a stack using std::vector as the underlying container
    std::stack<int, std::vector<int>> stack;

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

    std::cout << "Top element: " << stack.top() << std::endl;  // Output: 30

    return 0;
}