Queue program in c: To implement queue data structure in c programming.

What is the Queue?
The queue is a linear type of data structure.
It is an unordered group of the element in which elements are added at one end (rear end) and removed from one end (front end).
This data structure is commonly known as FIRST IN FIRST OUT (FIFO).
Queue Example:-
Example: Queue in a ticket counter or line at a bank.

Operation on queue
1. Create (Q): Create an empty queue.
2. ENQ (i): It is used to add element i to queue at the rear end.
3. DEQ (Q): It is used to remove the element from a queue from a front end.

Algorithm for queue program in c

  1. Write a function to enqueue element in queue
  2. Next write function to dequeue element from queue
  3. Write function to display elements in queue

Queue program in c using array

#include<conio.h>
#include<stdio.h>
#define max 5
int queue[max],front=0,rear=0;
int menu();
void enqueue();
void dequeue();
void display();
void main()
{
int ch;
clrscr();
printf("\n Queues using Arrays\n");
do
{
ch=menu();
switch(ch)
{
case 1: enqueue();
break;
case 2: dequeue();
break;
case 3: display();
break;
case 4: exit(0);
break;
default:printf("\n Please enter a valid choice!!");
}
}while(1);
}

int menu()
{
int ch;
printf("\n1.ENQUEUE \n2.DEQUEUE \n3.DISPLAY \n4.EXIT");
printf("\nEnter your Choice:");
scanf("%d",&ch);
return ch;
}

void enqueue()
{
int element;
if(rear==max)
{
printf("\nOverflow!!");
}
else
{
printf("\nEnter Element:");
scanf("%d",&element);
queue[rear++]=element;
printf("\n %d Enqueued at %d",element,rear);
}

}

void dequeue()
{
if(rear==front)
{
printf("\nUnderflow!!");
}
else
{
front++;
printf("\nElement is Dequeued from %d",front);
}
}
void display()
{
int i;
if(front==rear)
{
printf("\nQueue is Empty!!!");
}
else
{
printf(" \n");
for(i=front;i<max;i++)
{
printf(" | %d ",queue[i]);
}
printf("|");
}
}

Output :

Queue program in c

Write A Comment