Join Regular Classroom : Visit ClassroomTech

Basic Stack Operation using Queue – Codewindow

Hot Topics

Output: Displaying the elements in stack 1 4 2 3 Top element of the stack is: 1. Deleted element is: 1 Displaying the elements in stack 4 2 3 Top element of stack after removing one element is: 4

Data Structure

Stack using Queue

Stack is data structure which follows Last In First Out principle that means the most recent inserted element in stack will be the first element to be removed from stack.Queue is a data structure that follows First In First Out principle that means the element which is inserted at first in the queue will be the first one to remove from queue.So we have to arrange elements in queue in such an order that the element inserted last will be the first element to get remove.

Algorithm:

  1. Take a queue.

  2. Push element in queue

  3. Use a loop to iterate till the queue size()-1,remove the elements and again push it into the queue.This way you will get the last element inserted in queue in the first position.

  4. Pop the first element in queue.

  5. Top will give the first element in queue.

Push operation in Stack using Queue

Here is the code :

class Stack
{
    queue q;

public:
    void Push(int x)
    {
        // getting the size of queue
        int size = q.size();
        // pushing our element into the queue
        q.push(x);
        // using a for loop to iterate through the elements in queue
        for (int i = 0; i <= size - 1; i++)
        {
            /*for size one lesser than the size of queue we are
getting the elements from the front of queue(FIFO) and pushing it back into the same queue.*/
            q.push(q.front());
            // removing the front element from queue
            q.pop();
        }
    }

    int Pop()
    {
        /*getting element from front of queue as we know queue
        follows FIFO principle*/
        int element = q.front();
        // removing the front element from queue
        q.pop();
        // giving back the element that we removed from queue
        return element;
    }

    int Top()
    {
        /*the top element in stack will be the first or front element
        of queue*/
        return q.front();
    }
    int Display()
    {
        queue p = q;
        while (!p.empty())
        {
            int n = p.front();
            cout << n << endl;
            p.pop();
        }
        return 0;
    }
}; // end of class

int main()
{
    // s is an object of class Stack on which we will perform our operations
    Stack s;
    // pushing element into the stack
    s.Push(3);
    s.Push(2);
    s.Push(4);
    s.Push(1);

    cout <<”Displaying the elements in stack” << endl;
    s.Display();
    cout << "Top element of the stack is: " << s.Top() << endl;
    cout << "Deleted element is: " << s.Pop() << endl;
    cout <<”Displaying the elements in stack” << endl;
    s.Display();
    cout << "Top element of the stack after removing element is: " << s.Top() << endl;
}
Output:
	Displaying the elements in stack
	1
    4
    2
    3
    Top element of the stack is: 1.
	Deleted element is: 1
	Displaying the elements in stack
	4
    2
    3
	Top element of stack after removing one element is: 4

Nagarro Solved

Automata Fixing

      

We Love to Support you

Go through our study material. Your Job is awaiting.

Recent Posts
Categories