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;
    }
}
相关推荐
不要葱花29 分钟前
接下来我将复现 10 篇强化学习算法:第 3 篇,一杯喜茶,搞定 Search-R1
算法·面试
十月的皮皮38 分钟前
C语言学习笔记20260717-预处理机制
c语言·笔记·学习
geovindu42 分钟前
CSharp: Recursion Algorithm
开发语言·后端·算法·c#·递归算法
z小猫不吃鱼1 小时前
ResRep: Lossless CNN Pruning via Decoupling Remembering and Forgetting
算法·cnn·剪枝
卡提西亚1 小时前
leetcode-239. 滑动窗口最大值
算法·leetcode·职场和发展
ShallWeL1 小时前
【机器学习】(20)—— 类别不平衡
人工智能·算法·机器学习
明志数科1 小时前
具身智能数据标准化:从碎片化接口到统一工作组的技术路径
人工智能·算法·机器学习
旖-旎1 小时前
LeetCode 494:目标和(动态规划/01背包问题)—— 题解
c++·算法·leetcode·动态规划·01背包
spssau2 小时前
一文学会结构方程模型:从模型搭建、路径图绘制到不达标调整,实操全流程
人工智能·python·算法
GeekArch2 小时前
第8讲:裸机单片机项目——Vibe快速迭代的最优边界
c语言·stm32·单片机·嵌入式硬件·mcu·物联网·ai编程