Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Insertion in Queue

Last Updated on May 9, 2023 by Prepbytes

A queue is a data structure that follows the First-In-First-Out (FIFO) principle, meaning the element that is added first to the queue is the first one to be removed. In a queue, elements are inserted at one end called the rear or tail, and removed from the other end called the front or head.

What is Insertion in Queue?

Insertion in a queue is the process of adding an element to the rear end of the queue. The process of insertion can be done using various methods, including:

  1. Enqueue: The enqueue operation adds an element to the rear end of the queue. In this operation, the element is added to the end of the queue and the rear pointer is incremented by one.
  2. Circular Queue: A circular queue is a modified version of a queue in which the rear end is connected to the front end. In this type of queue, when the rear end reaches the end of the queue, it wraps around to the beginning of the queue. This allows for efficient use of memory and can help prevent overflow errors.
  3. Priority Queue: A priority queue is a queue in which each element is associated with a priority. The elements with higher priority are dequeued before those with lower priority. In this type of queue, the insertion is done based on the priority of the element.

Insertion in a queue is an important operation that enables adding new elements to the queue in a proper and organized way. This operation is commonly used in computer science and programming, particularly in data processing and algorithms.

Basic Operations of Queue

An object called a queue (an ADT, or abstract data structure) enables the following operations:

  • Enqueue: Add a component to the queue’s end.
  • Dequeue: Put a component at the end of the queue.
  • IsFull: A complete queue should be checked.
  • IsEmpty: Verify that the queue is empty.
  • Peek: Without deleting it, obtain the value at the head of the queue.

Insertion in Queue: Enqueue()

The enqueue operation adds an element to the rear end of the queue. Following is the algorithm:

  • Step1: Begin
  • Step 2: A complete queue should be checked.
  • Step 3: Produce an overflow error and quit if the queue is full.
  • Step 4: Place a data element where the rear is pointing in the queue.
  • Step 5: End

Implementation of Queue for Insertion

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 4
int intArray[MAX];
int front = 0;
int rear = -1;
int itemCount = 0;
bool isFull(){
   return itemCount == MAX;
}
bool isEmpty(){
   return itemCount == 0;
}
int removeData(){
   int data = intArray[front++];
   if(front == MAX) {
      front = 0;
   }
   itemCount--;
   return data;
}
void insert(int data){
   if(!isFull()) {
      if(rear == MAX-1) {
         rear = -1;
      }
      intArray[++rear] = data;
      itemCount++;
   }
}
int main(){
   insert(13);
   insert(15);
   insert(19);
   insert(1);
   printf("After insertion in Queue: ");
   while(!isEmpty()) {
      int n = removeData();
      printf("%d ",n);
   }
}

Output

After Insertion in Queue: 13 15 19 1 

Deletion in Queue: Dequeue()

The dequeue operation deletes an element from the queue. Following is the algorithm:

  • Step1: Begin
  • Step 2: Verify if the queue is empty or not
  • Step 3: Produce an overflow error and quit if the queue is empty.
  • Step 4: Access the information where the front is pointing if the queue is not empty.
  • Step 5: To point to the following accessible data element, increment the front pointer.
  • Step 6: End

Implementation of Queue for Deletion

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 4
int intArray[MAX];
int front = 0;
int rear = -1;
int itemCount = 0;
bool isFull(){
   return itemCount == MAX;
}
bool isEmpty(){
   return itemCount == 0;
}
void insert(int data){
   if(!isFull()) {
      if(rear == MAX-1) {
         rear = -1;
      }
      intArray[++rear] = data;
      itemCount++;
   }
}
int removeData(){
   int data = intArray[front++];
   if(front == MAX) {
      front = 0;
   }
   itemCount--;
   return data;
}
int main(){
   int i;
   
   
   insert(13);
   insert(15);
   insert(19);
   insert(1);
   
   int num = removeData();
   printf("\nElement removed: %d\n",num);
   printf("Queue After Deletion : ");
   while(!isEmpty()) {
      int n = removeData();
      printf("%d ",n);
   }
}

Output

Element removed: 13
Queue After Deletion: 15 19 1 

isFull() Operation

The isFull() is used to verify whether the queue is completely filled or not.
Following is the Algorithm:

  • Step 1: Begin
  • Step 2: Return true if the number of queue entries matches the number of queue members.
  • Step 3: otherwise return false.
  • Step 4: End

Implementation of isFull() Operation

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 4
int intArray[MAX];
int front = 0;
int rear = -1;
int itemCount = 0;
bool isFull(){
   return itemCount == MAX;
}
void insert(int data){
   if(!isFull()) {
      if(rear == MAX-1) {
         rear = -1;
      }
      intArray[++rear] = data;
      itemCount++;
   }
}
int main(){
   int i;
 
   insert(13);
   insert(15);
   insert(19);
   insert(1);
 
   printf("Queue: ");
   for(i = 0; i < MAX; i++)
      printf("%d ", intArray[i]);
   printf("\n");
   if(isFull()) {
      printf("Queue is completely filled!\n");
   }
}

Output

Queue: 13 15 19 1 
Queue is completely filled!

Peek() Operation

Without removing it, the front-most piece in the queue can be retrieved using the peek() method.
Following is the Algorithm:

  • Step 1: Begin
  • Step 2: Return the element at the front of the queue.
  • Step 3: End

Implementation of Peek() Operation

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 4
int intArray[MAX];
int front = 0;
int rear = -1;
int itemCount = 0;
int peek(){
   return   intArray[front];
}
bool isFull(){
   return itemCount == MAX;
}
void insert(int data){
   if(!isFull()) {
      if(rear == MAX-1) {
         rear = -1;
      }
      intArray[++rear] = data;
      itemCount++;
   }
}
int main(){
   int i;
   
   insert(13);
   insert(15);
   insert(19);
   insert(1);
   printf("Queue: ");
   for(i = 0; i < MAX; i++)
      printf("%d ", intArray[i]);
   printf("\nElement at front: %d\n",peek());
}

Output

Queue: 13 15 19 1 
Element at front: 13

Implementation of Queue

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 6
int intArray[MAX];
int front = 0;
int rear = -1;
int itemCount = 0;
int peek(){
   return intArray[front];
}
bool isEmpty(){
   return itemCount == 0;
}
bool isFull(){
   return itemCount == MAX;
}
int size(){
   return itemCount;
}
void insert(int data){
   if(!isFull()) {
      if(rear == MAX-1) {
         rear = -1;
      }
      intArray[++rear] = data;
      itemCount++;
   }
}
int removeData(){
   int data = intArray[front++];
   if(front == MAX) {
      front = 0;
   }
   itemCount--;
   return data;
}
int main(){
   
   /* insert 5 items */
   insert(3);
   insert(5);
   insert(9);
   insert(1);
   insert(12);


   insert(15);

   if(isFull()) {
      printf("Queue is full!\n");
   }

 
   int num = removeData();
   printf("Element removed: %d\n",num);

   
   insert(16);
   
  
   insert(17);
   insert(18);

  
   printf("Element at front of the Queue: %d\n",peek());
  
   printf("Queue: ");
   while(!isEmpty()) {
      int n = removeData();
      printf("%d ",n);
   }
}

Output

Queue is full!
Element removed: 3
Element at front of the Queue: 5
Queue: 5 9 1 12 15 16 

Conclusion
In conclusion, insertion in a queue is a fundamental operation in queue data structure that involves adding an element to the rear end of the queue. This operation ensures that the element added is placed at the end of the queue, following the FIFO (First-In-First-Out) principle, where the element added first will be the first to be removed. The insertion process typically involves checking if the queue is full, and if not, adding the element to the end of the queue. It is a simple and efficient operation that is used in various applications such as process scheduling, network traffic management, and data buffering.

Frequently Asked Questions

Q1. What is the purpose of insertion in a queue?
Ans. The purpose of insertion in a queue is to add a new element to the end of the queue in order to maintain the order of elements in the queue, following the FIFO (First-In-First-Out) principle.

Q2. What happens if you try to insert an element in a full queue?
Ans. If you try to insert an element in a full queue, it will result in an error or an exception, depending on the programming language or implementation.

Q3. How do you insert an element in a queue?
Ans. To insert an element in a queue, you need to first check if the queue is full. If the queue is not full, you can add the element to the end of the queue by incrementing the rear pointer of the queue and storing the new element at that position.

Q4. Can you insert multiple elements in a queue at once?
Ans. No, you cannot insert multiple elements in a queue at once. Each element must be inserted one at a time, following the order of insertion.

Q5. Is insertion in a queue an efficient operation?
Ans. Yes, insertion in a queue is generally an efficient operation that takes constant time, O(1), as long as the queue is not full.

Leave a Reply

Your email address will not be published. Required fields are marked *