C语言第九天--------一个标准且最简单的队

队列:是一种FIFO(先进先出)结构

应用场景:排队,叫号
队列物理结构:顺序队列,链式队列; 

操作:
队尾:只允许进队
队头:只允许出队

循环顺序队列:可以解决假满假空现象
操作:
1、初始化队列:构造空的队列
InitQueue
2、清空队列:清空队列元素
ClearnQueue
3、队列长度:求队列的长度
QueueLength
4、进队:
EnQueue
5、出队:
DeQueue

#include<stdio.h>
struct Queue
{
    int buf[10];
    int rear;
    int front;
};
void InitQueue(struct Queue* pcs)
{
	    pcs->rear=0;
    	pcs->front=0;
}
short IsEmpty(struct Queue* pcs)
{
    if(pcs->front == pcs->rear)
            return 1;
    else
            return 0;
}
short IsFull(struct Queue* pcs)
{
    if( (pcs->rear+1)%10==pcs->front)
            return 1;
    else
            return 0;
}
unsigned short QueueLength(struct Queue* pcs)
{
    return (pcs->rear - pcs->front+10)%10;
}
short EnQueue(struct Queue* pcs,int key)
{
    if(IsFull(pcs)==1)
            return 0;
    pcs->buf[(pcs->rear)%10]=key;//进队
    pcs->rear=(pcs->rear+1)%10;//移动队尾
    return 1;
}
short DeQueue(struct Queue* pcs,int* value)
{
     if(IsEmpty(pcs)==1)
            return 0;
    *value=pcs->buf[pcs->front];//出队
    pcs->front=(pcs->front+1)%10;//对头加一
    return 1;
}




int main()
{
    struct Queue cs;
    InitQueue(&cs);
    //获取队长
    int ilen=QueueLength(&cs);
    printf("当前排队人数为:%d\n",ilen);
    EnQueue(&cs,1);
    EnQueue(&cs,2);
    EnQueue(&cs,3);
    EnQueue(&cs,4);
    EnQueue(&cs,5);
    EnQueue(&cs,6);
    EnQueue(&cs,7);
    EnQueue(&cs,8);
    EnQueue(&cs,9);
    ilen=QueueLength(&cs);
    printf("当前排队人数为:%d\n",ilen);

    int value;
    while(DeQueue(&cs,&value)!=0)
    {
            ilen=QueueLength(&cs);
            printf("请%d号到窗口处理业务,剩余%d人在排队\n",value,ilen);
            sleep(3);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值