
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Implement Queue Using Two Stacks in C++
Stack
The stack is a linear data structure that follows the Last-In-First-Out (LIFO) operation. Where the element will be added and removed from the top.
Following are the stack operations:
- push (int data): Insertion at top
- int pop(): Deletion from top
Queue
The queue is also a linear data structure that follows the First-In-First-Out (FIFO) operation. Where insertions are done at one end (rear) and deletions are done from another end (front). The first element that is entered is deleted first.
Following are the stack operations:
- EnQueue (int data): Insertion at rear end
- int DeQueue(): Deletion from front end
Function Description
Before diving into the program, let's see and describe the functions and how these functions help to implement Queue ?
- function enQueue() to enqueue an item to queue:
- Push m to s1.
- function deQueue() to dequeue items from queue:
- If both the stacks empty then print queue is empty.
- If s2 is empty then move elements from s1 to s2.
- Pop the elements from s2 and return it.
- function push() to push item in the stack.
- function pop() to pop out an item from the stack.
C++ Program to Implement Queue Using Two Stacks
This C++ Example implements a queue using two stacks (linked lists) where elements are pushed onto one stack and dequeued by transferring elements to another stack when needed:
#include <iostream> #include <cstdlib> using namespace std; struct nod { int d; struct nod * n; }; void push(struct nod ** top_ref, int n_d); int pop(struct nod ** top_ref); struct queue { struct nod * s1; struct nod * s2; }; void enQueue(struct queue * q, int m) { push( & q -> s1, m); } int deQueue(struct queue * q) { int m; if (q -> s1 == NULL && q -> s2 == NULL) { cout << "Queue is empty" << endl; exit(0); } if (q -> s2 == NULL) { while (q -> s1 != NULL) { m = pop( & q -> s1); push( & q -> s2, m); } } m = pop( & q -> s2); return m; } void push(struct nod ** top_ref, int n_d) { struct nod * new_node = (struct nod * ) malloc(sizeof(struct nod)); if (new_node == NULL) { cout << "Stack overflow\n"; exit(0); } new_node -> d = n_d; new_node -> n = ( * top_ref); ( * top_ref) = new_node; } int pop(struct nod ** top_ref) { if ( * top_ref == NULL) { // if stack is empty cout << "Stack underflow\n"; exit(0); } struct nod * top = * top_ref; int res = top -> d; * top_ref = top -> n; free(top); return res; } int main() { struct queue * q = (struct queue * ) malloc(sizeof(struct queue)); q -> s1 = NULL; q -> s2 = NULL; cout << "Enqueuing..7" << endl; enQueue(q, 7); cout << "Enqueuing..6" << endl; enQueue(q, 6); cout << "Enqueuing..2" << endl; enQueue(q, 2); cout << "Enqueuing..3" << endl; enQueue(q, 3); cout << "Dequeuing..." << deQueue(q) << endl; cout << "Dequeuing..." << deQueue(q) << endl; cout << "Dequeuing..." << deQueue(q) << endl; free(q); return 0; }
The above code generated the following output:
Enqueuing..7 Enqueuing..6 Enqueuing..2 Enqueuing..3 Dequeuing...7 Dequeuing...6 Dequeuing...2