西电数据结构上机题——循环队列入队出队

循环队列入队出队,之前看到的百度文库的参考答案有误,重新写了下,经过测试没问题。

//循环队列入队出队
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
//循环队列的结构类型定义
const int m=5;
typedef int datatype;
typedef struct
{   datatype sequ[m];
    int  rear, quelen;
}qu;
void setnull(qu*);
void enqueue(qu*, datatype);
datatype *dequeue(qu*);

int main()
{  qu *sq;
   datatype x, *p;
   int key;
   sq=(qu*)malloc(sizeof(qu));
   setnull(sq);
   do
   {  printf("1.Enter Queue   2.Delete Queue   -1.Quit:");
      scanf("%d",&key);
      switch(key)
      {  case 1:  printf("Enter the Data:"); scanf("%d",&x);
		          enqueue(sq,x);  break;
	     case 2:  p=dequeue(sq);
		          if(p!=NULL) printf("%d\n",*p);
		          break;
	     case -1: exit(0);
      }
   }while(1);
}

//置空队
void setnull(qu *sq)
{  sq->rear=m-1;
   sq->quelen=0;
}

//添加入队算法
void enqueue(qu *sq,datatype x)
{
	if (sq->quelen ==m)
	{
		printf("队列已满");
	}
	else
	{
		sq->rear=(sq->rear+1)%m;
		sq->sequ[sq->rear]=x;
		sq->quelen++;
	}
}
//添加出队算法
datatype *dequeue(qu *sq)
{
	datatype *temp;
	if (sq->quelen ==0)
	{
		printf("队列已空");
		return NULL;
	}
	else{
		temp=(datatype*)malloc(sizeof(datatype));
		*temp=sq->sequ[(sq->rear-sq->quelen+m+1)%m];
		sq->quelen--;
		return(temp);
	} 
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zstar-_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值