Insertion in a Queue in C

Enqueue operation in C

How to insert an element in a Queue in C programming?

Insertion in a Queue in C is also known as enqueuing so to enqueue an element certain steps are followed which will be discussed in this article. Inserting an element in a queue is one the simplest operation that is performed on a queue which is FIFO – First in First out structure, i.e. the element added first (Enqueued) will go out of the queue first (Dequeued).

Learn more about Queue in C programming by clicking the button below:-

for inserting an element in a Queue in C

Steps for enqueuing in C

To insert an element in a queue that is for enqueuing we proceed using following algorithm:-

  1. Define the maximum size of queue and initialize front and rear as -1.
  2. In the main function we will initialize two variables that will store the data and the size of the queue.
  3. Accept the data that we want to enter in a queue using a for loop.
  4. After accepting the data use enqueue() function to insert the data in a queue.
  5. In this function return queue id full , if the value of rear is equal to max-1.
  6. Else increase the value of rear by 1.
  7. After this insert the data that we have accepted earlier.
  8. Now display the data of the queue using a disp() function.
  9. In this function if the value of front is equal to -1 then it means list is empty.
  10. Else initialize a for loop from front to rear and print the data that queue is holding.
Insertion in a queue in C

for insertion in a Queue in C

Algorithm for enqueuing in C

As we know that Queue is a FIFO type data structure so we will be using following algorithm for insertion of an element in a Queue :-

  • IF (REAR==MAX-1)
  • RETURN QUEUE FULL
  • ELSE
  • REAR+REAR+1
  • QUEUE[REAR]=DATA
  • IF(FRONT== -1)
  • FRONT = 0

for inserting an element in a queue in C (Enqueuing)

#include 
#include 
#define MAX 50//defining the size of queue
int queue[MAX],front=-1,rear=-1;

void enqueue(int data) //function to enqueue data
{
	if(rear==MAX-1)
		printf("\nQueue is Full!");
	else
	{
		rear=rear+1;
		queue[rear]=data;
		if(front==-1)
		front=0;
	}
	
}
void disp() //function to display the relents of the queue
{
	printf("\nThe elements in the queue are:");
	int i;
	if(front==-1)
		printf("\nQueue is Empty");
	else
		for(i=front;i<=rear;i++)
		{
		   
			printf("%d  ",queue[i]);
		}
		printf("\n");
}
int main() //main function for all input and output statements
{
	int data,size;
	printf("Enter the size of queue: ");
	scanf("%d",&size);
    for(int i=0;i<size;i++)
    {
      printf("\nEnter Data to Enqueue:\n");
      scanf("%d",&data);
      printf("Enqueuing %d\n",data);
      enqueue(data);
      disp();
    }
    
}
Output:
Enter the size of queue: 5

Enter Data to Enqueue:
11
Enqueuing 11

The elements in the queue are:11  

Enter Data to Enqueue:
12
Enqueuing 12

The elements in the queue are:11  12  

Enter Data to Enqueue:
13
Enqueuing 13

The elements in the queue are:11  12  13  

Enter Data to Enqueue:
14
Enqueuing 14

The elements in the queue are:11  12  13  14  

Enter Data to Enqueue:
15
Enqueuing 15

The elements in the queue are:11  12  13  14  15