前言
用顺序表队列操作,会遇到假溢出的现象(当队尾=所限定元素个数),因此为了解决这种假溢出的
现象,因此就引进了循环队列(将队列的头和尾连接起来,构成环形)这种结构,便可以实现对队列
的重复使用。
循环队列——银行排号
详细代码
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define SIZE 4 //最大用户数量
typedef struct
{
int num; //顾客编号
long time;
}DATE;
typedef struct
{
DATE date[SIZE];
int head;
int tail;
}cycqueue;
int num;
cycqueue *cycqueueinit (); //循环队列初始化
int cycqueuefree(cycqueue *q); //释放队列
int cycqueuepush(cycqueue *q,DATE date); //入队列
DATE *cycqueuepop(cycqueue *q); //出队列
int add(cycqueue *q); //添加排队人
int next(cycqueue *q); //为下一个顾客服务
DATE *cycqueuepeek(cycqueue *q); //取队列的头元素
int cycqueuelen(cycqueue *q); //计算有多少人排队
int main()
{
cycqueue *queue;
int select=-1;
queue=cycqueueinit();
if(queue==NULL)
{
printf("创建队列失败");
getch();
return 0;
}
while(select!=0)
{
printf("\t\t 选择操作n");
printf("\t\t 1.新到用户n");
printf("\t\t 2.下一个用户\n");
printf("\t\t 0.退出\t\t\n");
printf("select:");
scanf("%d",&select);
switch(select)
{
case 0:
break;
case 1:
add(queue);
printf("\n现在有%d用户等待\n",cycqueuelen(queue));
break;
case 2:
next(queue);
printf("\n现在有%d用户等待\n",cycqueuelen(queue));
break;
}
}
cycqueuefree(queue);
getch();
return 0;
}
cycqueue *cycqueueinit ()
{
cycqueue *q;
if(q=(cycqueue *)malloc(sizeof(cycqueue)))
{
q->head=0;
q->tail=0;
return q;
}
else
return NULL;
}
int cycqueuefree(cycqueue *q)
{
if(q)
{
free(q);
}
}
int cycqueuepush(cycqueue *q,DATE date)
{
if((q->tail+1)%SIZE==q->head)
{
printf("栈已满");
return 0;
}
else
{
q->tail=(q->tail+1)%SIZE;
q->date[q->tail]=date;
return 1;
}
}
DATE *cycqueuepop(cycqueue *q)
{
if(q->tail==q->head)
{
printf("队为空\n");
exit(0);
}
else
{
q->head=(q->head+1)%SIZE;
return &(q->date[q->head]);
}
}
int add(cycqueue *q)
{
DATE date;
if((q->tail+1)%SIZE!=q->head)
{
date.num=++num;
date.time=time(NULL);
cycqueuepush(q, date);
}
else
printf("排队人太多\n");
}
int next(cycqueue *q)
{
DATE *date;
if(q->tail!=q->head)
{
date=cycqueuepop(q);
printf("现在为%d服务\n",date->num );
}
if(q->tail!=q->head)
{
date=cycqueuepeek(q);
printf("请编号为%d顾客准备\n",date->num);
}
}
DATE *cycqueuepeek(cycqueue *q)
{
if(q->tail==q->head)
{
printf("队为空");
return NULL ;
}
else
{
return &(q->date[(q->head+1)%SIZE]);
}
}
int cycqueuelen(cycqueue *q)
{
int n;
n=q->tail-q->head;
if(n<0)
n=SIZE+n;
return n;
}
最重要的是判断队满的条件,当构成循环队列时,判断队列为空(p->head=p->tail),当循环队列充满时也是
(p->head == p->tail)这个条件,因此为了避免这种情况,就让队列差一个元素充满时,就判断队列以满,
所以((p->head+1)%size == p->tail)判断队列已满。