函数题
6-1 排队叫号系统
编写程序实现银行排队叫号系统,采用链队列作为存储结构。
函数接口定义:
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=(QNode *)malloc(sizeof(QNode)); if(Q.front!=NULL) { Q.rear=Q.front; Q.front->next=NULL; return(TRUE); } else return(FALSE); } Status EnLinkQueue(LinkQueue &Q,QElemType e) { QNode *newNode; newNode=(QNode *)malloc(sizeof(QNode)); if(newNode!=NULL) { newNode->data=e; newNode->next=NULL; Q.rear->next=newNode; Q.rear=newNode; return(TRUE); } else return ERROR; } Status DeLinkQueue(LinkQueue &Q,QElemType &e) { QNode *p; if(Q.front==Q.


1416

被折叠的 条评论
为什么被折叠?



