×

Implementation of Queue

Implementation of queue: We can implement the queue through the array and linked list. An array is the easiest way to implement the queue.

When a queue is created with the help of an array, the number of its elements is declared before processing. The beginning of the array becomes its "front" end, and the end of the array becomes its "rear" end. The terms "front" and "rear" are used to describe the linear list when implemented as a queue.

The "Front" stores the index of the first element in the queue. The "rear" stores the index of the last element in the queue. At any given time, the number of elements in a queue can be calculated from the values ??of "front" and "rear".

if front = 0 then number of elements = 0 
else no of elements = front – rear + 1.

Enqueue operation

When you insert an element in the queue, that operation is called enqueue.

Algorithm of insert the element of the queue

Step 1: Before adding elements, it is checked whether the queue is full or not. 
Step 2: If the queue is full, it will print the "overflow error" message, and the program will terminate. 
Step 3: If the queue is not full, the value of the "rear" end will be increased by 1, and a new element will be added to the queue-array.  

Insertion in a queue implemented as an array

if rear = NULL then
 {      
  rear = front = 0        
  Queue [0] = ITEM 
} 
else if rear = N – 1 then
 {         
print “Queue full, overflow” 
} 
else 
{         
Queue [rear + 1] = ITEM         
rear = rear + 1
 } 
END  

Dequeue operation

When you remove an element from the queue, that operation is called dequeue.

Algorithm of insert the element of the queue

Step 1: Before removing elements, it is checked whether the queue is empty or not. 
Step 2: If the queue is empty, it will print the "underflow error" message, and the program will terminate. 
Step 3: If the queue is not empty, the value of the "front" end will be increased by 1, and an element will be removed in the queue-array.  

Deletion in a queue implemented as an array

if front = NULL then
 {       print “quere empty, underflow” 
} 
else 
{       
 ITME = Queue[front]        
 if front = rear then         
 {            
  front = rear = NULL        
  }       
 else        
  {            
  front = front + 1         
 } 
 } 
END  

Queue program in C language:

#include<stdio.h> 
#include<stdlib.h>
#define maxsize 5 
void insert();  
void delete();  
void display(); 
int front = -1, rear = -1;  
int queue[maxsize];   
void main ()  
 {      
 int choice;      
  while(choice != 4)       
 {            
  printf("\n1.insert of element\n2.Delete of element\n3.Display the queue\n4.Exit\n");         
 printf("\nenter your choice = ");          
 scanf("%d",&choice);         
  switch(choice)          
 {         
      case 1:             
  insert();              
 break;              
 case 2:             
  delete();          
     break;          
     case 3:         
      display();         
      break;             
  case 4:             
  exit(0);            
   break;            
   default:          
      printf("\nenter valid choice??\n");    
       }   
    }  
 }  
 void insert() 
  {  
int item;     
printf("\nenter the element\n");   
    scanf("\n%d",&item);        
   if(rear == maxsize-1)     
  {         
  printf("\n OVERFLOW \n");        
   return;    
   }   
    if(front == -1 && rear == -1)   
    {      
     front = 0;  
         rear = 0;      
 }      
 else      
  {        
   rear = rear+1;     
  }     
  queue[rear] = item;  
     printf("\n value inserted ");     
     }  
 void delete()  
 {    
   int item;    
    if (front == -1 || front > rear)  
     {   
        printf("\n UNDERFLOW \n");   
        return;    
                  }    
   else  
     {    
       item = queue[front];     
      if(front == rear)     
      {       
        front = -1;     
          rear = -1 ;    
       }        
   else         
   {            
   front = front + 1;     
      }       
    printf("\n value deleted ");    
   }     
            }   
       void display() 
  {      
 int i;  
     if(rear == -1)  
     {        
   printf("\n empty queue\n");   
    }    
   else    
   { 
  printf("\n printing values \n");  
         for(i=front;i<=rear;i++)   
        {          
     printf("\n%d\n",queue[i]);   
        }      
    }  
 }  

 Output:

1.insert of element 
2.Delete of element 
3.Display the queue 
4.Exit   

enter your choice = 1   
enter the element 1   
Value inserted   

1.insert of element 
2.Delete of element 
3.Display the queue 
4.Exit   

enter your choice = 1   
enter the element 2   
Value inserted   

1.insert of element 
2.Delete of element 
3.Display the queue 
4.Exit   

enter your choice = 2   
value deleted   

1.insert of element 
2.Delete of element 
3.Display the queue 
4.Exit   

enter your choice = 3   
printing values   2   
1.insert of element 
2.Delete of element 
3.Display the queue 
4.Exit   

enter your choice = 4

Related Topics

Binary Tree in Data Structures

What is a Binary Tree in Data Structures? The term binary itself means bi, which implies two of anything. So very clearly, we know we present the trees in the form...

6 minutes read.

Collision Resolution Techniques

Collision Resolution Techniques Collision in hashing In this, the hash function is used to compute the index of the array.The hash value is used to store the key in the hash table,...

2 minutes read.

Data Structures Algorithms

What is an Algorithm? An algorithm is a sequence of steps used to complete a job or get a desired result. It is similar to programming building elements that let cell...

4 minutes read.

Queue operations in Data Structure

Queue - Queue is a linear data structure or first in first out data structure means the first element added in the queue will be removed first and the last...

7 minutes read.

Binary Tree vs Binary Search Tree: Data Structure

Difference Between Binary Tree and Binary Search Tree What is Binary Tree? A tree which each node can have utmost two children called binary tree. These children are referred as the ‘left...

3 minutes read.

Find out the area between two concentric circles

You have given two values of the radius of two circles. You have to find out the area between these two circles. Let's take an example - For the above diagram,...

3 minutes read.

Merge Sort

Merge Sort is one of the most widely used sorting algorithms, and it is based on the Divide and Conquer principle. A problem is subdivided into multiple sub-problems in this method....

8 minutes read.

Operations on 1D-Arrays

One Dimensional Array Operations Basic Methods The fundamental operations enabled by an array are listed below. Traverse prints each element of the array one by one.Insert a new element at the specified index.Delete...

8 minutes read.

Convert a Binary Tree into a Binary Search Tree

Implementation #include <stdio.h>   #include <stdlib.h>       //creating a node of the binary tree.  struct __nod{       int record;       struct __nod *Lft;       struct __nod *Rt;   };       // presenting the root of the binary tree.   struct...

5 minutes read.

Operations of B++ tree

Insertion When we discuss the insertion operation in the B++ tree, this operation helps us in pushing a new element in the tree at any given place. In this case, the...

17 minutes read.

Linear Queue VS Circular Queue

What is Queue? A queue is one of the important linear data structures extensively used in various computer applications. It is based on the FIFO (First In First Out) principle. It...

9 minutes read.

Insertion Sort in Data Structures

Insertion Sort in C++ Insertion sort is a sorting algorithm that, in each iteration, installs an unsorted element in its proper position Insertion sort operates in a similar way to how we...

3 minutes read.

Depth of binary tree

We all know that a binary tree is a kind of tree that helps us maintain the order and balance of the tree. It is a type of tree in...

4 minutes read.

Data Structure Prefix to Postfix Conversion

Prefix to Postfix Conversion Prefix: As the name suggests if the operator placed before the operands called the prefix expression.  The form of prefix expression is (operator, operand1, operand2). Example:  *+EF-GH (Infix:...

2 minutes read.

Treap data structure

In this article, we will discuss the treap data structure. The word treap is a combination of 'tree' and 'heap'. So, treap data structure is a combination of a heap...

8 minutes read.

Delete a Node without head pointer from the linked list

Delete a Node without head pointer from the linked list This article will explain how to delete a node without a head pointer from the linked list. We have given a...

2 minutes read.

Binary Search Tree

Binary Search Tree: A binary search tree is a type of tree in which every node is organized in the sorted order. It is also called an ordered binary tree. Properties...

4 minutes read.

Tree vs Graph: Data Structure

Difference Between Tree and Graph What is Tree? A tree is a non-linear data structure and finite collection of elements called node. A tree, in which the data items are arranged in...

3 minutes read.

Finding Rank in a Binary Search Tree

Implementation // writing a C++ program to find out the rank and element in the program.  #include <bits/stdc++.h> using namespace std; struct __nod { int record; __nod *Lft, *Rt; int LftSize; }; __nod* new__nod(int record) { __nod *temp = new __nod; temp->record...

6 minutes read.

Function to Insert a Node in a Binary Search Tree

Implementation // writing C++ code that will help us in implementing the insertion operation in a binary search tree. #include <bits/stdc++.h> using namespace std; // creating a new binary search tree node struct __nod { int...

8 minutes read.