Stacks And Queues In STL

By Sruthy

By Sruthy

Sruthy, with her 10+ years of experience, is a dynamic professional who seamlessly blends her creative soul with technical prowess. With a Technical Degree in Graphics Design and Communications and a Bachelor’s Degree in Electronics and Communication, she brings a unique combination of artistic flair…

Learn about our editorial policies.
Updated June 24, 2023

Learn The Implementation Of Stacks And Queues In STL With Examples.

Stacks and queues are two containers in STL which are very basic in nature. They are the simplest containers which have wide applications in software programming.

In this tutorial, we will see a detailed implementation of both these containers in STL. We will also go through the various operations supported by stack and queue with examples.

=> Watch Out The Simple C++ Training Series Here.

STACKS AND QUEUES in STL

Stacks

Stack container in STL is a type of container adaptors. It is used to replicate a stack data structure in C++. Stack container is a set of elements in which the elements are inserted at one end and are also deleted at the same end.

This common point of addition and deletion is known as “Top of the stack”.

The pictorial representation of stack is shown below.

stack

As shown in the above representation, the stack is a container in which elements are added and deleted from the same end called Top of the stack.

Since addition and deletion happen at the same end, we can say that stack container is LIFO (last in, first out) type of work. This means, that the element added first will be the last one to be deleted.

In order to implement stack container, we need to include the header <stack> in our program.

#include<stack>

The general declaration syntax for stack container is:

stack<objectType> stackName;

Stack Operations

Next, let us discuss the various operations that stack container in STL supports.

  • push: push operation is used to insert an element in the stack. This operation always adds elements at the top of the stack.

Consider an empty stack mystack of type integer.

mystack

Next, let’s add element 1 to the stack.

add element 1 to the stack

Then, we add element 3 to the stack.

add element 3 to the stack

According to the representation, as a result of a push operation, an element is added at the top of the stack. After every push operation, the size of the stack is increased by 1.

  • pop: pop operation is used to remove an element from the stack. The element removed is the one that is pointed to by the top of the stack. As a result of the pop operation, the stack size is reduced by 1.

Let us see how the pop operation looks like:

Consider the stack mystack as above in which we have already pushed 2 elements.

mystack2

Now let us call the function pop(). When this call is executed, the element at the top of the stack is removed and the ‘Top’ points to the next element as shown below.

element ‘3’ is removed pop operation

If we again call pop(), then the next element (in this case 1) will be removed thereby resulting in an empty stack.

element ‘1’ is removed - stack operation

  • top: Returns the topmost element of the stack.
  • empty: Checks if the stack is empty or not.
  • size: Returns the size of the stack i.e. the number of elements in the stack.

Further Reading => Frequently Asked Stack Developer Interview Questions

Given below is an example of Stack implementation to better understand the operations.

#include &lt;iostream&gt;
#include &lt;stack&gt;
using namespace std;
void printStack(stack &lt;int&gt; stk)
{
   while (!stk.empty())
      {
         cout &lt;&lt; '\t' &lt;&lt; stk.top();
         stk.pop();
      }
   cout &lt;&lt; '\n';
}
 
int main ()
{
   stack &lt;int&gt; oddstk;
   oddstk.push(1);
   oddstk.push(3);
   oddstk.push(5);
   oddstk.push(7);
   oddstk.push(9);
 
   cout &lt;&lt; &quot;The stack is : &quot;;
   printStack(oddstk);
 
   cout &lt;&lt; &quot;\nSize of stack: &quot; &lt;&lt; oddstk.size();
   cout &lt;&lt; &quot;\nTop of stack: &quot; &lt;&lt; oddstk.top();
   cout &lt;&lt; &quot;\noddstk.pop() : &quot;;
   oddstk.pop();
   printStack(oddstk);
   cout&lt;&lt;&quot;\nAnother pop(): &quot;;
   oddstk.pop();
   printStack(oddstk);
   return 0;
}

stack implementation output

The above example clearly shows the push operation that generates a stack. It also shows the stack after two consecutive pop operations.

Thus we have seen stack and its operations in STL. Further, in this tutorial, we will see the detailed implementation of yet another simple STL container which is “Queue”.

Queue

The queue is yet another container in STL which is very simple and useful too. Queue container is a replica of the queue data structure in C++. Unlike stack, in the queue container, there are two ends, i.e. front, and back.

Elements are added to the queue at the back while deleted from the front of the queue. In general, queue uses FIFO (First in, First Out) type of arrangement.

To implement a queue container in a program, we have to include a header <queue> in the code.

#include <queue>

The general syntax for declaration of the queue is:

queue<objectType> queue_name;

We declare the queue container as follows:

Queue<int> myqueue;

Queue Operations

Now, we will see the various operations supported by the queue.

  • push: Function ‘push’ adds the element at the end of the queue i.e. at the back of the queue.
  • pop: Function ‘pop’ removes the first element of the queue i.e. the element at the front of the queue.

Let us understand the push and pop functions of the queue.

Consider an empty queue declared above myqueue. Now we push an even number 2 in the queue with the operation

myqueue.push(2);

Now the queue will look like:

myqueue.pop();

Next, we add ‘4’ to the queue with call ‘myqueue.push(4)’.

Now the queue looks as shown below:

myqueue.push(4)

As seen above, the elements are pushed into the queue from the rear end or back.

Now let us pop operation on myqueue.

myqueue.pop();

myqueue.pop();

So as we see, when pop() is called, element in the front of the queue is removed. This means that the first element that is entered the queue is the first element to be out of the queue.

  • front: This function returns a reference to the first element of the queue.
  • back: Back returns a reference to the last element in the queue.
  • empty: Checks if the queue is empty.
  • size: Returns the size of the queue i.e. the number of elements in the queue.

Given below is an Example program that demonstrates the operations used by the queue container.

#include &lt;iostream&gt;
#include &lt;queue&gt;
using namespace std;
void printQueue(queue &lt;int&gt; myqueue)
{
   queue &lt;int&gt; secqueue = myqueue;
   while (!secqueue.empty())
   {
      cout &lt;&lt; '\t' &lt;&lt; secqueue.front();
      secqueue.pop();
   }
   cout &lt;&lt; '\n';
}
int main()
   {
      queue &lt;int&gt; myqueue;
      myqueue.push(2);
      myqueue.push(4);
      myqueue.push(6);
      myqueue.push(8);
      cout &lt;&lt; &quot;The queue myqueue is : &quot;;
      printQueue(myqueue);
      cout &lt;&lt; &quot;\nmyqueue.size() : &quot; &lt;&lt; myqueue.size();
      cout &lt;&lt; &quot;\nmyqueue.front() : &quot; &lt;&lt; myqueue.front();
      cout &lt;&lt; &quot;\nmyqueue.back() : &quot; &lt;&lt; myqueue.back();
      cout &lt;&lt; &quot;\nmyqueue.pop() : &quot;;
      myqueue.pop();
      printQueue(myqueue);
      return 0;
   }

Output:

The queue myqueue is :            2           4             6            8

myqueue.size() : 4

myqueue.front() : 2

myqueue.back() : 8

myqueue.pop() :        4            6            8

As shown above, we declare a queue container first. Then using the push operation we add the first four even numbers into it. After that we pop the element from the queue and display the changed queue.

queue container

Conclusion

With this, we have come to the end of this tutorial on stacks and queues. As already mentioned these are the simplest containers that we have in STL. Another variation of the queue container is known as “Priority Queue”.

In our upcoming tutorial, we will discuss more on Priority Queue in STL!!

=> Visit Here To Learn C++ From Scratch.

Was this helpful?

Thanks for your feedback!