PTA 6-5 排队叫号系统

编写程序实现银行排队叫号系统,采用链队列作为存储结构。

函数接口定义:

复制代码
Status InitLinkQueue(LinkQueue &Q);//对链队列进行初始化
Status EnLinkQueue(LinkQueue &Q,QElemType e);//入队
Status DeLinkQueue(LinkQueue &Q,QElemType &e);//出队
Status QueueEmpty(LinkQueue Q);//判断队空

裁判测试程序样例:

复制代码
在这里给出函数被调用进行测试的例子。例如:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define TRUE 1
#define FALSE 0
#define MAX_QSIZE 30
typedef int Status;
typedef int QElemType;
typedef struct Qnode
{
    QElemType data;
    struct Qnode * next;
}QNode, *QueuePtr;//创建节点类型
typedef  struct Queue{
  QueuePtr   front ; //队首指针
  QueuePtr   rear ; //队尾指针
  }LinkQueue;
Status InitLinkQueue(LinkQueue &Q);//对链队列进行初始化
Status EnLinkQueue(LinkQueue &Q,QElemType e);//入队
Status DeLinkQueue(LinkQueue &Q,QElemType &e);//出队
Status QueueEmpty(LinkQueue Q);//判断队空

int main()
{
    QElemType no=1;
    QElemType callno;
    int select,flag=1;
    LinkQueue qu;
    InitLinkQueue(qu);
    while(flag==1)
    {
        //printf("1:排队2:叫号0:退出 请选择:");
        scanf("%d",&select);
        switch(select)
        {
        case 1: printf("您的序号为:%d\n",no);
                EnLinkQueue(qu,no);  //no入队
                no++;
                break;
        case 2: if(DeLinkQueue(qu,callno)==ERROR)
                    printf(">>没有排队的客户!\n");
            else//队不空
                printf(">>请%d号办理业务\n",callno);
                break;
        case 0:flag=0;
        }
    }
    return 0;
}

/* 请在这里填写答案 */

输入样例:

复制代码
1
1
1
2
2
2
2
0

输出样例:

复制代码
您的序号为:1
您的序号为:2
您的序号为:3
>>请1号办理业务
>>请2号办理业务
>>请3号办理业务
>>没有排队的客户!

正确答案:

复制代码
Status InitLinkQueue(LinkQueue &Q)
{
    Q.front=Q.rear=NULL;
    return 1;
}
Status EnLinkQueue(LinkQueue &Q,QElemType e)
{
    if(Q.front==NULL&&Q.rear==NULL)
    {
        Q.rear=(QueuePtr)malloc(sizeof(QNode));
        Q.front=Q.rear;
    }
    else
    {
        Q.rear->next=(QueuePtr)malloc(sizeof(QNode));
        Q.rear=Q.rear->next;
    }
    Q.rear->next=NULL;
    Q.rear->data=e;
    //printf("成功排队\n");
    //printf("%p\n",Q.rear);
    return 1;
}

Status DeLinkQueue(LinkQueue &Q,QElemType &e)
{
    if(Q.front==NULL)
    {
        return 0;
    }
    else
    {
        QueuePtr p=Q.front;
        //printf("%p\n",Q.front);
        e=p->data;
        Q.front=Q.front->next;
        //printf("%p\n",Q.front);
        free(p);
        return 1;
    }
}
Status QueueEmpty(LinkQueue Q)
{
    if(!Q.front&&!Q.rear)
    {
        return FALSE;
    }
    else
    {
        return TRUE;
    }
}
相关推荐
凌冰_2 小时前
数据结构 - 时间空间复杂度(一)
数据结构·算法·软件工程
Pniubi3 小时前
力扣2 . 两数相加
算法·leetcode·职场和发展
戴·菲瑜3 小时前
为什么PPO难以做出顶级棋类引擎
算法·博弈论·rl·ppo
在所不辞兄5 小时前
为什么数字孪生驱动的产线标定需要在线更新机制,而不能一次标定后长期沿用?
人工智能·神经网络·其他·算法·机器学习
无忧.芙桃7 小时前
C++语言原理与实践(二十三):哈希表的实现
c++·链表·哈希算法·散列表·哈希表
郝学胜-神的一滴9 小时前
Effective Python 条款36:优雅拼装迭代器与生成器
开发语言·数据结构·人工智能·python·numpy·scikit-learn
AvocadoZeroOneOI10 小时前
B4500 [GESP202603 三级] 凯撒密码 详细解题报告
c++·算法
cjz389912 小时前
双指针技巧:覆盖式双指针(原地覆盖)
算法
console.log('npc')14 小时前
Python 学习教程 · 第4课:数据结构
数据结构·python·学习
橘子1315 小时前
【高级数据库】关系代数基本概念
算法