Javatpoint Logo
Javatpoint Logo

Queue using stack in C++

In this article, you will learn about the queue using stack in C++ with its implementation.

Implementing a stack data structure as a queue, with the low-level data structure being the push (adding an element) and pop (removing an element) operations.

A stack is a Last In First Out (LIFO) data structure, whereas a queue is a FIFO (First In First Out) data structure. A stack pops the element at the top and pushes a new element to the top of the stack. On the other hand, a queue enqueues (inserts) an element at the bottom while dequeuing (removes) an element from the top.

Implementation of Queue:

Using the stack data structure, there are two ways to implement a queue. While one approach uses one stack, the other uses two.

Using two stacks:

This method ensures that the oldest inserted element is always at the top of stack1 for the deQueue operation to simply pop from stack1. Stack 2 is used to position the element at the top of stack 1.

enqueue(x):

  • While stack1 is not empty, push everything from stack1 to stack2.
  • Push x to stack1.
  • Push everything back to stack1.

dequeue(x):

  • If stack1 is empty, display an error message.
  • Pop an item from stack1 and return it.

Example:

Let us take an example to illustrate the queue using two stacks in C++.

Output:

Queue using stack in C++

Using one stack:

A queue can also be implemented using only one user stack, and it uses recursion.

enqueue(x):

  • Push x to stack_.

dequeue(x):

  • If stack_ is empty, display an error message.
  • If stack_ has only one element, return it.
  • Pop every item off the stack_ recursively, put it in a variable called ret, push it back into stack_, and then return ret.

Dequeue()'s third step ensures that the final item to be popped is always returned. When only one item remains in the stack_, the recursion stops, returning the final element from stack_ in dequeue() and pushing the remaining items back into stack_.

Example:

Let us take an example to illustrate the queue using one stack in C++.

Output:

Queue using stack in C++

Benefits of Queue using stack in C++

There are several advantages to implementing a queue in C++ with two stacks, especially when it comes to simplicity and efficiency in specific situations. Here are a few benefits:

  • Simplicity: It is not too difficult to implement. There are two stacks: stack 1 is used for enqueue operations, and stack 2 is used for dequeue operations. It may facilitate understanding and maintenance of the code.
  • Effectiveness of Dequeue Functions: In the amortized sense, dequeue operations have a constant time complexity of O(1). The reason for this is that the components are only transferred from stack1 to stack2 when stack2 is empty. Once transferred, stack2 can be efficiently dequeued multiple times until it becomes empty once more, at which point another transfer is initiated.
  • Space Efficiency: O(n) is the space complexity, where n is the number of elements in the queue. It is because each element is only ever stored in stack1 or stack2. Higher space complexity could be achieved by using more data structures in other queue implementations.
  • Flexibility: This method allows you to select your choice's underlying data structure (stacks). It can be beneficial in scenarios where a stack makes more sense or is more effective than other data structures.
  • Enqueue Operations: Enqueue operations have a time complexity of O(1) because they directly push elements onto stack1. However, other data structures like a linked list or a dynamic array might be more efficient in scenarios where enqueue operations are frequent and dequeue operations are infrequent.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA