Queue in c

Queue in c  is a simple c program to implement queue data structure.

In this tutorial, we will see the implementation of queue in c.

The queue is a linear type of data structure.

It is an unordered group of the element in which elements is 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:-

for ex: 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 queue from a front end.

Queue algorithm :

  • Write a function to enqueue element in queue
  • Next write function to dequeue element from queue
  • Write function to display elements in queue

C program to implement queue using array

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586#include<conio.h>#include<stdio.h>#define max 5int 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 in c
queue in c

Explanation :

  •  First  we write a main function
  •  Write a function to enqueue element in queue.
  •  Next we write a function to dequeue element from queue.
  •  Same we write the function to display the elements in the queue.
  •  We take an input choice from the user
  • And make a call to function user want to execute.