×

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

AVL tree in data structure c++

AVL tree is generally known as the self-sustained and most balanced tree in the field of a binary search tree. It was also widely known as the height-balanced binary tree....

6 minutes read.

Selection Sort

In each iteration of the selection sort algorithm, the smallest item from an unsorted list is chosen and placed at the top of the unsorted list. Algorithm of Selection Sorting In order...

3 minutes read.

Introduction to 1D-Arrays

One Dimensional Array Technical Definitions The simplest version of an Array is a One-Dimensional Array, in which the items are stored linearly and may be accessed individually by supplying the index value...

6 minutes read.

Recaman’s Sequence

Recamán's succession repeat connection in arithmetic and software engineering. Since its components are obviously connected with the past components, they are as often as possible characterized utilizing recursion. It takes its...

4 minutes read.

Count pairs from two linked lists whose sum is equal to a given value

Count pairs from two linked lists whose sum is equal to a given value In this problem, we have given two linked lists of size n1 and n2 with distinct elements...

4 minutes read.

Given a Binary Tree Check the Zig-Zag Traversal

Implementation // The C++ implementation of the zig-zag traversal method in the O(n) time.  #include <iostream> #include <stack> using namespace std; // creating a binary tree node. struct __nod { int record; struct __nod *Lft, *Rt; }; // creating a...

4 minutes read.

FLEX (Fast Lexical Analyzer Generator)

FLEX stands for Fast Lexical Analyzer Generator. Around 1987, Vern Paxson created Flex in C with a great deal of input and inspiration from Van Jacobson. Van Jacobson's approach is...

3 minutes read.

Serialize and Deserialize Binary Trees

In order to save a tree in a file that can later be restored, serialisation is used. The tree's structure must be preserved. Deserialization involves reading a tree from a...

4 minutes read.

Check if a Singly Linked List is Palindrome

Check if a Singly Linked List is Palindrome In this section, we have given a singly linked list, and we need to check whether the given list is a palindrome. Example:           1...

3 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.

Red Black Tree

Red Black Tree A red-black tree is referred as self-balancing binary search tree. The tree was invented by Rudolf Bayer in 1972. In red-black, each node stores an extra bit that...

8 minutes read.

Recursion in Fibonacci

Fibonacci heap is considered to be a particular execution of the heap data structure that ultimately helps in making use of not just any number but the Fibonacci numbers. It...

3 minutes read.

What are Forest Trees in Data Structure

Data structure A data model manages and optimizes computer resources, and a database stores and manages data. It's one of many uses for data structures to hold data. Data structures come...

5 minutes read.

Detect Loop in Linked List: Data Structure

Detect the Loop in Linked List: In this problem, we will be seeing some technique through which we can detect the loop in linked list. We will discuss each technique...

3 minutes read.

Given a Binary Tree Swap Nodes at K Height

Implementation // Writing a C++ program that will help us exchange the nodes.  #include<bits/stdc++.h> using namespace std; // Creating a binary tree node. struct __nod { int record; struct __nod *Lft, *Rt; }; // creating a function that will help...

8 minutes read.

Trim a binary search tree

Implementation //writing a C++ program will help us eliminate the keys that are out of the league.  #include<bits/stdc++.h> using namespace std; //we are now creating a binary search tree node consisting of key left...

8 minutes read.

Stack Data Structure

The stack is a non-primitive and linear data structure. It works on the principle of LIFO (Last In First Out). That is, the element that is added to the end...

3 minutes read.

What is a Threaded Binary Tree?

When we consider those binary trees that are interlinked with each other, we do come across the fact that the fields present in there do consist of NULL values that...

3 minutes read.

Serialize and Deserialize a Binary Tree

Implementation // Writing a C++ program to check the serialization and deserialization of binary tree.   #include <iosstream> /* A binary tree node contains a key and a pointer to the left and right...

4 minutes read.

Rotate a Singly Linked List

Rotate a Singly Linked List This article will explain how we can rotate the singly linked list. Here we have given a singly linked list, and we need to rotate this...

4 minutes read.