c语言循环队列程序_使用数组实现循环队列的C程序

c语言循环队列程序

#include<stdio.h>
#define MAX 10

typedef struct Q
{
int R,F;
int data[MAX];
}Q;

void initialise(Q *P);
int empty(Q *P);
int full(Q *P);
void enqueue(Q *P,int x);
int dequeue(Q *P);
void print(Q *P);

void main()
{
Q q;
int op,x;
initialise(&q);

do
  {
printf(“nn1)Insertn2)Deleten3)Printn4)Quit”);
printf(“nEnter Your Choice:”);
scanf(“%d”,&op);
switch(op)
  {
case 1: printf(“nEnter a value:”);
scanf(“%d”,&x);
if(!full(&q))
enqueue(&q,x);
else
printf(“nQueue is full !!!!”);
break;
case 2: if(!empty(&q))
  {
x=dequeue(&q);
printf(“Deleted Data=%d”,x);
  }
else
printf(“nQueue is empty !!!!”);
break;
case 3: print(&q);break;
  }
      }while(op!=4);
}

void initialise(Q *P)
{
P->R=-1;
P->F=-1;
}

int empty(Q *P)
{
if(P->R==-1)
return(1);
return(0);
}

int full(Q *P)
{
if((P->R+1)%MAX==P->F)
return(1);
return(0);
}

void enqueue(Q *P,int x)
{
if(P->R==-1)
{
P->R=P->F=0;
P->data[P->R]=x;
}
else
{
P->R=(P->R+1)%MAX;
P->data[P->R]=x;
}
}

int dequeue(Q *P)
{
int x;
x=P->data[P->F];
if(P->R==P->F)
{
P->R=-1;
P->F=-1;
}
else
P->F=(P->F+1)%MAX;
return(x);
}

void print(Q *P)
{
int i;
if(!empty(P))
{
printf(“n”);
for(i=P->F;i!=P->R;i=(i+1)%MAX)
printf(“%dt”,P->data[i]);
printf(“%dt”,P->data[i]);
}
}

#include<stdio.h>
#define MAX 10

typedef struct Q
{
int R,F;
int data[MAX];
}Q;

void initialise(Q *P);
int empty(Q *P);
int full(Q *P);
void enqueue(Q *P,int x);
int dequeue (Q *P);
void print(Q *P);

void main()
{
Q q;
int op,x;
initialise(&q);

do
  {
printf(“nn1)Insertn2)Deleten3)Printn4)Quit”);
printf(“nEnter Your Choice:”);
scanf(“%d”,&op);
switch(op)
  {
case 1: printf(“nEnter a value:”);
scanf(“%d”,&x);
if(!full(&q))
enqueue(&q,x);
else
printf(“nQueue is full !!!!”);
break;
case 2: if(!empty(&q))
  {
x=dequeue(&q);
printf(“Deleted Data=%d”,x);
  }
else
printf(“nQueue is empty !!!!”);
break;
case 3: print(&q);break;
  }
      }while(op!=4);
}

void initialise(Q *P)
{
P->R=-1;
P->F=-1;
}

int empty(Q *P)
{
if(P->R==-1)
return(1);
return(0);
}

int full(Q *P)
{
if((P->R+1)%MAX==P->F)
return(1);
return(0);
}

void enqueue(Q *P,int x)
{
if(P->R==-1)
{
P->R=P->F=0;
P->data[P->R]=x;
}
else
{
P->R=(P->R+1)%MAX;
P->data[P->R]=x;
}
}

int dequeue(Q *P)
{
int x;
x=P->data[P->F];
if(P->R==P->F)
{
P->R=-1;
P->F=-1;
}
else
P->F=(P->F+1)%MAX;
return(x);
}

void print(Q *P)
{
int i;
if(!empty(P))
{
printf(“n”);
for(i=P->F;i!=P->R;i=(i+1)%MAX)
printf(“%dt”,P->data[i]);
printf(“%dt”,P->data[i]);
}
}

翻译自: https://www.thecrazyprogrammer.com/2014/03/c-program-for-implementation-of-circular-queue-using-array.html

c语言循环队列程序

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值