C program to implement circular queue.
#include<stdio.h>
#define MAX 10
int front=0;
int rear=-1;
int isfull()
{
if((front==0&&rear==MAX-1)||(front>0&&rear==front-1)) return 1;
else return 0;
}
int isempty()
{
if(rear==-1&&front==0) return 1;
else return 0;
}
void push(int q[],int item)
{
if(isfull()) printf("queue is full \n");
else
{
rear=(rear+1)%10;
q[rear]=item;
}
}
void pop(int q[])
{
if(isempty()) printf("queue is empty \n");
else
{
front=(front+1)%10;
}
}
void display(int q[])
{
int i;
if(isempty()) printf("queue is empty \n");
else{
if(front>=0&&rear>=front)
{
printf("REAR==== %d \n\n",rear);
for(i=front;i<=rear;i++)
printf("%d \n",q[i]);
}
else
{
for(i=0;i<=rear;i++)
printf("%d \n",q[i]);
for(i=front;i<=MAX-1;i++)
printf("%d \n",q[i]);
}
}
}
void main()
{
int q[MAX],n,item;
for(;;)
{
printf("1.PUSH 2.POP 3.DISPLAY 4.EXIT \n");
scanf("%d",&n);
switch(n)
{
case 1: printf(" \n ================================================ \n");
printf("enter the element to be inserted : \n");
scanf("%d",&item);
push(q,item);
printf(" \n ================================================ \n");
break;
case 2: printf(" \n ================================================ \n");
printf("YOU HAVE SELECTED POP \n");
pop(q);
printf(" \n ================================================ \n");
break;
case 3: printf(" \n ================================================ \n");
printf(" content of the queue are \n");
display(q);
printf(" \n ================================================ \n");
break;
case 4: exit(0);
break;
}
}
}
#include<stdio.h>
#define MAX 10
int front=0;
int rear=-1;
int isfull()
{
if((front==0&&rear==MAX-1)||(front>0&&rear==front-1)) return 1;
else return 0;
}
int isempty()
{
if(rear==-1&&front==0) return 1;
else return 0;
}
void push(int q[],int item)
{
if(isfull()) printf("queue is full \n");
else
{
rear=(rear+1)%10;
q[rear]=item;
}
}
void pop(int q[])
{
if(isempty()) printf("queue is empty \n");
else
{
front=(front+1)%10;
}
}
void display(int q[])
{
int i;
if(isempty()) printf("queue is empty \n");
else{
if(front>=0&&rear>=front)
{
printf("REAR==== %d \n\n",rear);
for(i=front;i<=rear;i++)
printf("%d \n",q[i]);
}
else
{
for(i=0;i<=rear;i++)
printf("%d \n",q[i]);
for(i=front;i<=MAX-1;i++)
printf("%d \n",q[i]);
}
}
}
void main()
{
int q[MAX],n,item;
for(;;)
{
printf("1.PUSH 2.POP 3.DISPLAY 4.EXIT \n");
scanf("%d",&n);
switch(n)
{
case 1: printf(" \n ================================================ \n");
printf("enter the element to be inserted : \n");
scanf("%d",&item);
push(q,item);
printf(" \n ================================================ \n");
break;
case 2: printf(" \n ================================================ \n");
printf("YOU HAVE SELECTED POP \n");
pop(q);
printf(" \n ================================================ \n");
break;
case 3: printf(" \n ================================================ \n");
printf(" content of the queue are \n");
display(q);
printf(" \n ================================================ \n");
break;
case 4: exit(0);
break;
}
}
}
No comments:
Post a Comment