Circular Queue in C language (static)

 C program for circular queue (static) :


#include<stdio.h>
#define MAXSIZE 10
typedef struct queue
{
int data[MAXSIZE];
int front, rear;
}QUEUE;

void initqueue(QUEUE *pq)
{
pq->front=pq->rear=MAXSIZE-1;
}
int isempty(QUEUE *pq)
{
return(pq->front==pq->rear);
}

int isfull(QUEUE *pq)
{
if((pq->rear+1)%MAXSIZE==pq->front)
return 1;
return 0;
}


void addq(QUEUE *pq, int num)
{
pq->rear=(pq->rear+1)%MAXSIZE; //circular increment
pq->data[pq->rear]=num;
}


int removeq(QUEUE *pq)
{
pq->front=(pq->front+1)%MAXSIZE; //circular increment
return pq->data[pq->front];
}

int main()
{
QUEUE q;//create
int choice, num;
initqueue(&q);
do
{
printf("\n1: ADD\n2: REMOVE\n3: EXIT\n");
printf("Enter your choice :");
scanf("%d",&choice);
switch(choice)
{
case 1:
if(isfull(&q))
printf("Overflow");
else
{
printf("Enter the element to add :");
scanf("%d",&num);
addq(&q,num);
}
break;
case 2:
if(isempty(&q))
printf("Underflow");
else
{
num=removeq(&q);
printf("The removed element is %d", num);
}
break;
}
}while(choice!=3);
return 0;
}

Output:

1: ADD
2: REMOVE
3: EXIT
Enter your choice :1
Enter the element to add :23

1: ADD
2: REMOVE
3: EXIT
Enter your choice :1
Enter the element to add :45

1: ADD
2: REMOVE
3: EXIT
Enter your choice :1
Enter the element to add :54

1: ADD
2: REMOVE
3: EXIT
Enter your choice :2
The removed element is 23
1: ADD
2: REMOVE
3: EXIT
Enter your choice :2
The removed element is 45
1: ADD
2: REMOVE
3: EXIT
Enter your choice :2
The removed element is 54
1: ADD
2: REMOVE
3: EXIT
Enter your choice :3

Process returned 0 (0x0)   execution time : 16.389 s
Press any key to continue.

Post a Comment

2 Comments

Thanks,To visit this blog.