C++ Standard Template Library (STL) – [Stack and Queue]

Stacks and queues are data structures that store elements of a specific data type. They have various operations (methods/functions) to input, output and manage the data.

Stack implements a lastin, first-out (LIFO) policy on its elements. So, the element removed from the stack is always the most recently inserted one. On the other hand, Queue implements first-in, first-out (FIFO) policy on its elements. Hence in a queue, the deleted element is always the element that has been inserted for the longest time.

Stack

These are the operations provided for a stack:

Stack-Empty (S)
1. if S.empty() == true
2.   return true
3. else
4.   return false

Push (S, x)
1. S.size() =  S.size() + 1
2. S.push(x)

Pop (S)
1. if Stack-Empty (S)
2.   return NULL
3  else
4.   x = S.top()
5    S.size() =  S.size() - 1
6.   S.pop()  
7.   return x

Code Implementation

//
//  main.cpp
//  Stack
//
//  Created by Himanshu on 03/10/21.
//

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

int main () {
    
    stack<int> st;
    
    cout<<"Push(x) {10, 20, 30, 40, 50}"<<endl;
    st.push(10);
    st.push(20);
    st.push(30);
    st.push(40);
    st.push(50);

    cout<<"Stack-Empty(): ";
    if (st.empty()) {
        cout<<"Stack is empty"<<endl;
    } else {
        cout<<"Stack is not empty"<<endl;
    }
    
    cout<<"Pop elements..."<<endl;
    while (!st.empty()) {
        cout<< st.top()<<" ";
        st.pop();
    }
    cout<<endl;
    
    cout<<"Stack-Empty(): ";
    if (st.empty()) {
        cout<<"Stack is empty"<<endl;
    } else {
        cout<<"Stack is not empty"<<endl;
    }
    
  return 0;
}

Output

Push(x) {10, 20, 30, 40, 50}
Stack-Empty(): Stack is not empty
Pop elements...
50 40 30 20 10 
Stack-Empty(): Stack is empty

Here’s a working example:

https://ideone.com/uyVJVN

Queue

These are the operations provided for a queue:

Queue-Empty (Q)
1. if Q.empty() == true
2.   return true
3. else
4.   return false

Enqueue (Q, x)
1. Q.size() =  Q.size() + 1
2. Q.push(x)

Dequeue (Q)
1. if Queue-Empty (Q)
2.   return NULL
3  else
4.   x = Q.front()
5    Q.size() =  Q.size() - 1
6.   Q.pop()  
7.   return x

Code Implementation

//
//  main.cpp
//  Queue
//
//  Created by Himanshu on 03/10/21.
//


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

int main () {
    
    queue<int> qu;
    
    cout<<"Enqueue(x) {10, 20, 30, 40, 50}"<<endl;
    qu.push(10);
    qu.push(20);
    qu.push(30);
    qu.push(40);
    qu.push(50);

    cout<<"Queue-Empty(): ";
    if (qu.empty()) {
        cout<<"Queue is empty"<<endl;
    } else {
        cout<<"Queue is not empty"<<endl;
    }
    
    cout<<"Dequeue elements..."<<endl;
    while (!qu.empty()) {
        cout<<qu.front()<<" ";
        qu.pop();
    }
    cout<<endl;
    
    cout<<"Queue-Empty(): ";
    if (qu.empty()) {
        cout<<"Queue is empty"<<endl;
    } else {
        cout<<"Queue is not empty"<<endl;
    }
    
  return 0;
}

Output

Enqueue(x) {10, 20, 30, 40, 50}
Queue-Empty(): Queue is not empty
Dequeue elements...
10 20 30 40 50 
Queue-Empty(): Queue is empty

Here’s a working example:

https://ideone.com/g6wg54

Leave a Reply

Your email address will not be published.