Queue implementation in C++
In this post, you will learn how to implement a queue using C++ programming language.
A queue is a linear data structure where insertion of elements is done from one end and deletion from the other end. The queue is like a line of people at a polling booth where the first person in the line gets the first vote. This is called First-In-First-Out (FIFO). Insertion of the element is done from the one end, called the FRONT end and deletion is done from the other end called the REAR.
Queue Operations
These are the operations performed by a queue.
- Enqueue - The element to add in a queue. If the queue is full, then it is said to be an overflow condition.
- Dequeue - The element to delete in a queue. If the queue is empty, then it is said to be an underflow condition.
- Peek - Retrieve the element from the front end of the queue without removing it from the queue.
- IsEmpty - Check if the queue is empty or not.
- Size - Return the total number of elements present in the queue.
- Front - Get the front item from the queue.
- Rear - Get the last item from the queue.
Algorithm to Insert Element in a Queue
Step 1: IF REAR = MAX-1
print('Queue Overflow')
return
[END OF IF]
Step 2: IF FRONT = -1 and REAR = -1
SET FRONT = REAR = 0
ELSE
SET REAR = REAR + 1
[END OF IF]
Step 3: SET QUEUE[REAR] = NUM
Step 4: EXIT
In the above algorithm, we have checked the existence of space in the queue. If there is no space then print 'Queue Overflow' and the code is exited and if not then, we check the element at the FRONT and REAR, if both are empty then set both to 0 else increase REAR with one and insert element from the REAR end and return.
Algorithm to Delete Element in a Queue
Step 1: IF FRONT = -1 OR FRONT > REAR
print('Queue Underflow')
return
ELSE
SET FRONT = FRONT + 1
[END OF IF]
Step 2: EXIT
In the above algorithm, we have checked the existence of elements in the queue. If there is no element, then print 'Queue Underflow' and the code is exited; if not, then FRONT is increased by one and points to the next element in the queue.
Queue Implementation in C++
In the given example, we have asked the user for operations like insert, delete, display, and exit. As indicated by the choice entered by the user, access its respective function using the switch statement. Utilize the variables front and rare to address the first and last elements of the queue.
Check if the queue is full first in the function enqueue(). If it is, then print the result as "Queue Overflow". If not, take the number to be inserted as input and store it in the variable queue. Increment the variable front and rear by 1.
Check if the queue is empty before calling dequeue(). If it is, then print the result as "Queue Underflow". Otherwise, print the first element of the array queue[] and decrement the variable front by 1.
In the function show(), using a for loop, prints all the elements of the array starting from front to rear.
#include<iostream>
using namespace std;
#define max 10
int queue[max],front=-1,rear=-1;
// Function to display elements of Queue
void enqueue(int num)
{
// check for queue overflow
if(rear==max)
{
cout<<"QUEUE OVERFLOW!";
}
else if(front==-1 && rear==-1)
{
front++;
rear++;
queue[rear]=num;
}
else
{
rear++;
queue[rear]=num;
}
}
int dequeue()
{
// check for queue underflow
if(front==-1 || front>rear)
{
cout<<"QUEUE UNDERFLOW!";
return -1;
}
else
{
cout<<"Element deleted from queue : "<<queue[front++];
return queue[front-1];
}
}
void show()
{
int i=front;
// check for queue underflow
if(front==-1 || front>rear)
{
cout<<"QUEUE UNDERFLOW!";
}
else
{
while(i<=rear)
{
cout<<"\t"<<queue[i];
i++;
}
cout<<endl;
}
}
int main()
{
int choice,val;
//Menu for Queue operations
cout<<" :::MENU:::";
cout<<"\n1.Enqueue\n2.Dequeue\n3.Show\n4.Exit";
while(1)
{
printf("\nEnter the choice:");
scanf("%d",&choice);
switch(choice)
{
case 1: cout<<"Enter value to inset in queue : ";
cin>>val;
enqueue(val);
break;
case 2: dequeue();
break;
case 3: cout<<"Displaying Queue : ";
show();
break;
case 4: exit(0);
default: printf("\nError! Invalid choice!...");
}
}
return 0;
}
Output of the above code:
:::MENU:::
1.Enqueue
2.Dequeue
3.Show
4.Exit
Enter the choice:1
Enter value to inset in queue : 90
Enter the choice:1
Enter value to inset in queue : 22
Enter the choice:1
Enter value to inset in queue : 89
Enter the choice:3
Displaying Queue : 90 22 89
Enter the choice:2
Element deleted from queue : 90
Enter the choice:3
Displaying Queue : 22 89
Enter the choice:
Related Articles
Implementation of queue using array in CQueue implementation in Python
Queue Implementation in C
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert array to list Python
Python take screenshot of specific window
Web scraping Python BeautifulSoup
Check if two strings are anagrams Python
Python program to add two numbers
Print new line python
Prime factors of a number in c
Armstrong number program in c
Write a program to check leap year in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program to convert Decimal to Octal
C program to convert decimal to binary
C program to check whether a number is even or odd