Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

C++ STL: stack (Complete Guide)

Divya Jain
Divya Jain

Stacks in STL are dynamic sequential containers that are implemented as stack data structures in memory. They follow the LIFO (Last In First Out) arrangement i.e the last element that is inserted will be the first one to be popped out. Elements are stored in a contiguous manner in a stack.

Insertion and deletion only happen at the top of the stack. Stacks are present in #include<stack> header file.

Syntax:

stack<data_type> name;

Example:

stack < int > s; //initializes a stack of size 0 which stores integer values 

Functions on queues

  • push(element): It pushes the value at the top of the stack.
    Time Complexity: O(1)
    Parameters: the element to be inserted
    Return value: void
     
  • pop(): It deletes the topmost element of the stack.
    Time Complexity: O(1) 
    Parameters: None
    Return value: void
     
  • size(): It tells us the size of the stack. 
    Time Complexity: O(1)
    Parameters: None
    Return value: integer, the size of the stack
     
  • top(): It returns the topmost element of the stack. 
    Time Complexity: O(1)
    Parameters: None 
    Return value: the topmost element of the stack
     
  • swap(stack_name): It swaps the elements of the two stacks. 
    Parameters: stack name to be swapped with
    Return value: void
     
  • empty(): It tells us whether the stack is empty or not. 
    Time Complexity: O(1)
    Parameters: None
    Return value: boolean, true if the stack is empty else false
#include <iostream>
#include <stack>
using namespace std;

int main() {
  stack < int > s;
  for (int i = 0; i < 5; i++) {
    s.push(i + 1);
  }
  cout << "The size of stack is " << s.size() << '\n';
  cout << "The topmost element of the stack is " << s.top() << '\n';
  s.pop();
  s.pop();
  cout << "The elements of the stack after pop operation ";
  while (!s.empty()) {
    cout << s.top() << ' ';
    s.pop();
  }
  return 0;
}

Output

The size of stack is 5
The topmost element of the stack is 5
The elements of the stack after pop operation 3 2 1 

Copying one stack to another

stack < int > stack2 = stack1; //copies stack1 into stack2	

Any change in stack2 will not affect stack1. The time complexity of this operation is O(N) where N is the size of the stack1. The same can be done using reference but it will not create a copy, it will contain a reference to the original stack.

stack < int > & stack2 = stack1; //stack2 references stack1
Divya Jain
Divya Jain
Divya is an incoming SDE at Atlassian. She loves solving problems and web development. She loves to explore new things and is up for interesting conversations about tech.
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet