×

Queue in C++

What is Queue?

As the name suggests, the queue is the type of data structure that follows the FIFO (First In - First Out) mechanism. In simple words, it is a linear data structure that resembles somewhere like a stack but has the difference of entry and exit in the elements i.e unlike stacks. It is open at both ends. One end is used to insert data (enqueue) and the other end is used to remove data (dequeue).

Queues are just like lines at a ticket counter or a cash-counter where the first entering person is the first exiting person as well.

There is a similar operation of the queue just like the stacks. Some of the operations include:

peek() ? Gets the first element of queue without

removing it.

isfull() ? Checks if it is full.

isempty() ? Checks if it is empty.

These 3 above operations are efficient enough to serve the major two operations of queues and they are:

Enqueue():  Adds or stores item in the queue.

Dequeue():  Removes or prevents access of items in the queue.

Let us now look at some pseudo-codes for understanding operations in queue.

  1. Peek()
 int peek_or_top() {
    return queue[front_element];
 } 
  • isFull()
 bool isfull()
 {
    if(rear == MaximumSize - 1)
       return true;
    else
       return false;
 } 
  • isEmpty()
 bool isempty() {
    if(front < 0 || front > rear)
       return true;
    else
       return false;
 } 

The above operational codes are just the minor steps to approach a problem involving implementations of queues. Let us look at a program to understand the flow control of queues better.

 #include<iostream>
 #include<conio.h>
 #include<stdlib.h>
 #define SIZE 5
 using namespace std;
 int q[SIZE],front=0,rear=0;
 int main()
 {
 int ch;
 void enqueue();
 void dequeue();
 void display();
 while(1)
 {
 cout<<"\n 1. add element";
 cout<<"\n 2. remove element";
 cout<<"\n 3.display";
 cout<<"\n 4.exit";
 cout<<"\n enter your choice:";
 cin>>ch;
 switch(ch)
 {
 case 1:
 enqueue();
 break;
 case 2:
 dequeue();
 break;
 case 3:
 display();
 break;
 case 4:
 exit(0);
 default:
 cout<<"\n invalid choice";
 }
 }
 }
 void enqueue()
 {
 int no;
 if (rear==SIZE && front==0)
 cout<<"queue is full";
 else
 {
 cout<<"enter the num:";
 cin>>no;
 q[rear]=no;
 }
 rear++;
 }
 void dequeue()
 {
 int no,i;
 if (front==rear)
 cout<<"queue is empty";
 else
 {
 no=q[front];
 front++;
 cout<<"\n"<<no<<" removed from the queue\n";
 }
 }
 void display()
 {
 int i,temp=front;
 if (front==rear)
 cout<<"the queue is empty";
 else
 {
 cout<<"\n element in the queue:";
 for(i=temp;i<rear;i++)
 {
 cout<<q[i]<<" ";
 }
 }
 } 

Output:

Queue in C++
Queue in C++

Explanation:

The above code depicts the flow for adding and removing elements in a queue. To understand how the enqueue and dequeue operations are performed, look at the following algorithm below.

START

1.Initialize a variable of any choice

  1. Read the variable
  2. If(top == 1) then
  3. call insert function 
  4. Else
  5. call delete function
  6. End

Algorithm for inserting in Array-Queue:

  1. If rear = NULL
  2. rear=front=0
  3. QUEUE[0]=ITEM }
  4. Else If rear= N-1 then
  5. Print "Queue Overflow!"
  6. Else
  7. QUEUE[rear+1]=ITEM
  8. rear=rear+1
  9. END.

Algorithm for deleting in Array-Queue:

1.If front==NULL then

2. Print "Queue is Empty" 

3. Else

4. ITEM=QUEUE[front]

5. If front=rear Then

6. front=rear=NULL

7. Else

8. front = front + 1

9. END

Note: The above algorithm works for mainly two operations namely enqueue and dequeue. Since these two operations are also associated with other sub-operations like peek(), isFull(), and isEmpty() we can also easily them from the above code.

Analysis of features and implementations

  1. Queues are widely used in sharing single resource serving requests like print, or task scheduling, etc.
  • Practically, queues are used in the same way the call-centers hold the calls until the representatives are free.
  • Queues can be used to handle real-time interrupts in the same order they arrive like first in first out.

Complexity Analysis

  1. Enqueue : O(1)
  2. Dequeue : O(1)
  3. Size : O(1)

Note: Queues are used where processing is not required. Processing may not need any priority since the priority queue follows a different approach. Queues follow the same approach as Breadth-First-Search (BFS).

Points to Remember:

  1. The point of entry and exit are different in a queue.
  2. Tw0 stacks can be used to make a queue.
  3. Random access is not allowed in the queue.
  4. We cannot simply add or remove elements from the middle of any queue.
  5. Inserting operation using an array is costly in the dequeue. This is because all the elements at a specific position need to be shifted by one. It is the same concept of similar people sitting on a bench and one person from one end pushes down the other to accommodate himself.

Related Topics

C++ Aggregation

C++ Aggregation Definition: In C++, aggregation is a process in which one class (as an entity reference) defines another class. It provides another way to reuse the class. It represents...

4 minutes read.

C++ Break

In this article, we will discuss the C++ Break statement with its syntax, algorithm, pseudocode, and examples. The C++ break statement also terminates the currently active loop or switch statement immediately....

4 minutes read.

C++ Tricks for Competitive Programming

If you are interested in Computer science or Information technology, you must have heard about competitive programming. Competitive programming is a way to improve your problem-solving skills. There are various...

7 minutes read.

C++ Algorithms

There are plenty of programming paradigms that are closely associated with the implementations of code and simulate them into a proper functional one. This is done with the help of...

5 minutes read.

Pattern programs in C++

In this article, we are going to discuss different pattern programs in C++. Program for Printing * patterns: Right Angle Triangle* pattern #include <iostream> using namespace std; int main() {     int rows;     cout <<...

3 minutes read.

Smart pointers in C++

What are Pointers ? Pointers are often used to keep track of a variable's address. A null value can be assigned to a pointer. Pass by reference can be used to...

6 minutes read.

Array of sets in C++

Instead of defining distinct variables for each item, arrays are used to hold numerous values in a single variable. A collection of data objects stored in a continuous way is...

6 minutes read.

Functors in C++

Functors are not a very popular thing among beginner or intermediate-level programmers. But this thing is very useful and helpful. The name functor suggests us some similarities with function. It...

3 minutes read.

Star pattern in C++ using For Loops

Star patterns are one of the most extensively utilized patterns in any programming language since they help to increase logical thinking and flow control understanding.In the C++ programming language, you...

3 minutes read.

Hospital Management Project in C++

The following capabilities are required to build a hospital management project: These are as follows: hospitals' names, contact information, and lists of doctors and patients. Activities Supported Hospital Data Print Patients' data to...

4 minutes read.

Virtual class in C++

Introduction to Virtual Base class Virtual base classes can be utilized in virtual inheritance as a mechanism for examining many "instances" of a certain class while searching through multiple inheritances in...

6 minutes read.

Loops in C++

A loop statement in most programming languages allows us to execute a statement or a collection of statements numerous times. Control structures of programming languages vary, allowing for more complex...

6 minutes read.

Structure Sorting (By Multiple Rules) in C++

To understand the concept of Structure Sorting (By Multiple Rules) in C++, it is recommended to know the Structures concept in the C++ programming language. Here the scenario is pretty...

3 minutes read.

Add two numbers represented by two arrays in C++

The array stores a number in such a way that each digit of the number is represented by an array element. As an example, The array number 147 is 1,4,7. To add...

3 minutes read.

How to improve programming skills in C++

Before getting started, one should know why to improve their programming skills. To become a good software developer or programmer, one must be skilled in at least one programming language. Many...

4 minutes read.

Decltype type Specifier in C++

The primary use of C++ decltype is to inspect the declaration type of an entity in an expression. The auto keyword can declare a particular type of variable, whereas the...

4 minutes read.

C++ array of Pointers

Array of Pointers: In high-level programming languages like C++, the array's name is its pointer. The name of an array contains an address which is the address of an element. In...

4 minutes read.

Factory Method for Designing Pattern in C++

In C++, the factory method is a type of conditional design pattern. The factory method is related to creating a new object in C++. With the help of a factory...

3 minutes read.

Roadmap to C++ Programming

Introduction There are so many programming languages available in the market, but among them, C++ is something that never lost its charm. It has a powerful impact on the programming world....

4 minutes read.

Dynamic Memory Allocation in C++

In some programming situations, the number of data items changes as the program is running, which is known as dynamic data or input. Consider a real-world situation where a program...

3 minutes read.