Tuesday, July 2, 2013

C PROGRAM FOR CIRCULAR QUEUE

/*PROGRAM TO TEST INSERTION & DELETION OPERATION IN CIRCULAR QUEUE*/
#include<stdio.h>
#include<conio.h>
#define max 5
int front,rear,q[max];
void inqueue();
void delqueue();
void qdisplay();
int main() {
int c;
clrscr();
front=rear=-1;
do {
printf("\n1:INSERTION \n2:DELETION\n3:DISPLAY ");
printf("\n4:EXIT\nEnter Your Choice:");
scanf("%d",&c);
switch(c) {
case 1: {inqueue ();
break; }
case 2: {delqueue ();
break; }
case 3: {qdisplay ();
break; }
}
}
while(c!=4);
}

void inqueue() {
int x;
if ((front==0&&rear==max-1)|| (front==rear+1))
{ printf("\nQUEUE OVERFLOW\n");
return; }
if(front==-1)
{ front=rear=0; }
else
{ if(rear==max-1)
{ rear=0; }
else { rear++; } }
printf("\nEnter The Number:");
scanf("%d",&x);
q[rear]=x;
return; }

void delqueue() {
int y;
if(front==-1) {
printf("\nQUEUE IS UNDERFLOW \n");
return; }
y=q[front];
if(front==rear)
{ front=rear=-1; }
else
{ if(front==max-1)
{ front=0; }
else
{ front++; } }
printf("\n%d SUCESSFULLY DELETED \n",y);
return; }

void qdisplay() {
int i,j;
if(front==rear==-1)
{ printf("\nQUEUE IS EMPTY \n");
return; }
printf("\nITEMS ARE:");
for(i=front;i!=rear;i=(i +1)%max)
{ printf(" %d ",q[i]); }
printf(" %d ",q[rear]);
return; }





/************************************************************/
/*OUTPUT:-\

1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:1

Enter The Number:1

1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:1

Enter The Number:2

1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:1

Enter The Number:3

1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:1

Enter The Number:4

1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:1

Enter The Number:5

1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:1

QUEUE OVERFLOW

1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:3

ITEMS ARE: 1  2  3  4  5
1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:2

1 SUCESSFULLY DELETED

1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:3

ITEMS ARE: 2  3  4  5
1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:1

Enter The Number:7

1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:3

ITEMS ARE: 2  3  4  5  7
1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:4 */

No comments:

Post a Comment