C Programming language

adplus-dvertising
C - Circular Queue, Insertion and Deletion
Previous Home Next

Code for Insertion in a queue using a simple linear array :

#include <stdio.h>
#include<conio.h>
#define<stdlib.h>
#define MAXSIZE 17
void enqueue();
void dequeue();
void display();
int queue[MAXSIZE];
int front=-1;
int rear=-1;
void main()
{
int choice;
char ch;
clrscr();
printf("Program for various operations on Queue using arrays");
do
{
 printf("\n1. Enqueue");
 printf("\n2. Dequeue");
 printf("\n3. Display");
 printf("Enter your choice :");
 scanf("%d",&choice);
 switch(choice)
{
     case 1: enqueue();
         break;
     case 2: dequeue();
         break;
     case 3: display();
         break;
 default : printf("\nwrong choice made");
    }
printf("\ndo you want to continue y/n");
fflush(stdin);
scanf("%c",&ch);
}
while ((char=='y')||(char=='Y'));
getch();
}
void enqueue()
{
int info;
if((rear+1)%MAXSIZE==front)
{
    printf("\nQueue Overflow");
break;
}
else
{
     printf("\nEnter the element you want to insert to queue");
 scanf("\n%d", &info);
 rear=(rear+1)%MAXSIZE; /*increasing rear to accommodate new element and making a circular queue*/
 queue[rear]=info;                      /*placing the new element to the rear */
    }
}
void dequeue()
{
int info;
if((front==rear)||(front==-1))                  /*checking the underflow condition */
{
     printf("\n the queue is empty");
 break;
}
else
{
     info=queue[front];                         /*placing front element to info */
 front=(front+1)%MAXSIZE;               /*incrementing the front value */
 printf("\n the deleted item is %d", info);    /* returning the deleted value*/
    }
}
void display()
{
 int i;
 if((front==rear)||(front==-1))                 /*checking the underflow condition */
 printf("\n queue is empty");
 else
{
     printf("\nthe queue is diplayed as :")
 for(i=front;i!=rear;i=(i+1)%MAXSIZE)   /*traversing the queue */
{
     printf("%d",queue[i]);
}
    }
}
Previous Home Next