
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Implement Queue
Queue
The queue which is implemented as FIFO where insertions are done at one end (rear) and deletions are done from another end (front). The first element that entered is deleted first.
Queue operations are −
EnQueue (int data) − Insertion at rear end
int DeQueue()− Deletion from front end
This is a C++ program to implement queue using array.
Algorithm
Begin function Enqueue() to insert elements in queue: If queue is completely filled up then print “Overflow”. Otherwise insert element at rear. Update the value of rear End Begin function Dequeue() to delete elements from queue: If queue is completely empty then print “Underflow”. Otherwise insert element from the front. Update the value of rear. End
Example Code
#include <bits/stdc++.h> using namespace std; struct Q { int f, r, capacity; int* q; Q(int c) { f = r= 0; capacity = c; q = new int; } ~Q() { delete[] q; } void Enqueue(int d) { if (capacity == r) { //check if queue is empty or not printf("\nQueue is full\n"); return; } else { q[r] = d; //insert data r++; //update rear } return; } void Dequeue() { if (f == r) { printf("\nQueue is empty\n"); return; } else { for (int i = 0; i < r - 1; i++) { q[i] = q[i + 1]; } r--; //update rear } return; } void Display() //display the queue { int i; if (f == r) { printf("\nQueue is Empty\n"); return; } for (i = f; i < r; i++) { printf(" %d <-- ", q[i]); } return; } void Front() { if (f == r) { printf("\nQueue is Empty\n"); return; } printf("\nFront Element is: %d", q[f]); //print front element of queue return; } }; int main(void) { Q qu(3); qu.Display(); cout<<"after inserting elements"<<endl; qu.Enqueue(10); qu.Enqueue(20); qu.Enqueue(30); qu.Display(); qu.Dequeue(); qu.Dequeue(); printf("\n\nafter two node deletion\n\n"); qu.Display(); qu.Front(); return 0; }
Output
Queue is Empty 10 <-- 20 <-- 30 <-- after two node deletion 30 <-- Front Element is: 30
- Related Articles
- C++ Program to Implement Circular Queue
- C++ Program to Implement Priority Queue
- Program to Implement Queue in Python
- C++ Program to Implement Queue using Array
- C++ Program to Implement Queue in STL
- C++ Program to Implement Queue using Linked List
- C++ Program to Implement Queue Using Two Stacks
- Python Program to Implement Stack using One Queue
- Java Program to Implement the queue data structure
- Golang program to implement the Queue data structure
- Python Program to Implement Queue Data Structure using Linked List
- How to Implement Priority Queue in Python?
- How to implement Multithreaded queue With Python
- How to Implement a Queue in Lua Programming?
- Can we use Simple Queue instead of Priority queue to implement Dijkstra’s Algorithm?

Advertisements