Stacks in C++

The basic data structure which is known as stack is used to store elements in a linear fashion.

The order or method by which operations are carried out in a stack is known as LIFO (last in, first out). This implies that the final element added to the stack would be the first one to be deleted.

The Last In First Out (LIFO) principle governs how the data structure stack works. Stack is applied to solving a number of different issues.

The STL template in C++ offers a built-in implementation of the stack data structure (Standard Template Library).

  1. push() - It is used to add an element into the stack.
  2. pop() - It is used to remove an element from the stack.
  3. top() - It returns the element at the top of the stack.
  4. size() - It returns the number of elements in the stack.
  5. empty() - It returns true if the stack is empty.

In order to create a stack in C++, we first need to include the stack header file.

#include <stack>

One can use following syntax to create a stack

stack<data-type> stack-name;

Let's create the stack of integer

stack<int> st;

1. Insert an element into the stack :-

One can use push() function to add an element into a stack.

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

int main() {
  // create a stack of strings
  stack<string> names;

  // push elements into the stack
  names.push("Sagar");
  names.push("Vaishnavi");
  names.push("Atharv");
  names.push("Komal");

  // print elements of stack
     while(!names.empty()) {
    cout << names.top() << endl;
    names.pop();
  }
  return 0;
}
//Output
Komal
Atharv
Vaishnavi
Sagar

2. Remove an elements from the stack :-

One can remove an element from the stack using the pop() function.

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

int main() {
  // create a stack of strings
  stack<string> names;

  // push elements into the stack
  names.push("Sagar");
  names.push("Vaishnavi");
  names.push("Atharv");
  names.push("Komal");

  // removes "Komal" as it was inserted last
   names.pop();

    while(!names.empty()) {
    cout << names.top() << endl;
    names.pop();
  }
   return 0;
}
//Output
Atharv
Vaishnavi
Sagar

3. Access an elements from the stack :-

One can access the element at the top of the stack using the top() function.

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

int main() {
  // create a stack of strings
  stack<string> colors;

  // push elements into the stack
  colors.push("Pink");
  colors.push("Blue");
  colors.push("Green");
  colors.push("Red");

  // get top element
  string top = colors.top();

  cout << "Top Element: " << top;

  return 0;
}
//Output
Top Element: Red
  1. Get the size of the stack :- One can use the size() function to get the number of elements in the stack.
#include <iostream>
#include <stack>
using namespace std;

int main() {
  // create a stack of strings
  stack<double> num;

  // push elements into the stack
  num.push(22.7);
  num.push(75.8);
  num.push(85.36);
  num.push(9.2);
  num.push(6.12);

  // get the size of the stack
  int size = num.size();
  cout << "Size of the stack: " << size;

  return 0;
}
//Output
Size of the stack: 5

5. Check if the stack Is empty :-

One can use the empty() function to check if the stack is empty or not.

-> 1 (true) - if the stack is empty.

-> 0 (false) - if the stack is not empty.

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

int main() {
  // create a stack of int
  stack<int> num;

  cout << "Is the stack empty? ";

  // check if the stack is empty  
  if (num.empty()) {
    cout << "Yes" << endl;
  }
  else {
    cout << "No" << endl;
  }

  // pushing the element into the stack
  num.push(7);
  num.push(5);

  cout << "Is the stack empty? ";

  // check if the stack is empty  
  if (num.empty()) {
    cout << "Yes";
  }
  else {
    cout << "No";
  }
  return 0;
}
//Output
Is the stack empty? Yes
Is the stack empty? No