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;
    }
}
相关推荐
whoarethenext7 分钟前
c/c++的opencv的轮廓匹配初识
c语言·c++·opencv
爱吃涮毛肚的肥肥(暂时吃不了版)7 分钟前
项目班——0510——JSON网络封装
c++·算法·json
apocelipes19 分钟前
使用libdivide加速整数除法运算
c语言·c++·性能优化·linux编程
liang_202621 分钟前
【HT周赛】T3.二维平面 题解(分块:矩形chkmax,求矩形和)
数据结构·笔记·学习·算法·平面·总结
緈福的街口22 分钟前
【leetcode】2900. 最长相邻不相等子序列 I
算法·leetcode·职场和发展
易只轻松熊23 分钟前
C++(20): 文件输入输出库 —— <fstream>
开发语言·c++·算法
远瞻。44 分钟前
【论文阅读】人脸修复(face restoration ) 不同先验代表算法整理
论文阅读·算法
进击的小白菜1 小时前
LeetCode 153. 寻找旋转排序数组中的最小值:二分查找法详解及高频疑问解析
数据结构·算法·leetcode
青出于兰1 小时前
C语言| 指针变量的定义
c语言·开发语言
dog2501 小时前
BBR 的 buffer 动力学观感
人工智能·算法