链队列实现病人排队看病

现在到医院看病基本都得预约排队,那如何用数据结构的形式表示呢?下面就用链队列来表示:

进本过程就是:

1. 病人到达医院,将病历交到护士手中,进行排队。

2. 护士按照序号,一次安排病人就诊。

3. 滚动条上可以看到就诊情况。

4. 医生下班或无就诊病人时退出。


其中“病人到达”用命令'a'表示,“护士让患者就诊”用命名'n'表示,命令'b'表示查看排队情况,命令'm'退出。

注:这里采用一个队列,有“病人到达”命令时即入队,有“护士让患者就诊”命令是即出队,命令'b'即队列所有元素出队,命令'm'即终止运行。

用链队列实现上述功能,每次N命令前需要判断是否还有病人,没有病人终止运行;


代码实现:

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<conio.h>
#define INIT_SIZE 100
#define INCREASE_SIZE 10
#define status int
#define OK 1
#define ERROR 0

typedef struct QNode     //定义队列结构体
{
int data;
struct QNode *next;
}QNode, *QueuePtr;


typedef struct
{
QueuePtr front;   //队头指针

        QueuePtr rear;    //队尾指针
}LinkQueue;


status InitQueue(LinkQueue &Q)  //初始化队列
{
Q.front = Q.rear = (QNode*)malloc(sizeof(QNode));
if (!Q.front)
return ERROR;
Q.front->next = NULL;
return OK;
}


status DestoryQueue(LinkQueue &Q) //销毁队列
{
while (Q.front)
{
Q.rear = Q.front;
free(Q.front);
Q.front = Q.rear->next;
}
return OK;
}


status EnQueue(LinkQueue &Q, int e)   //队列尾插入元素
{
QueuePtr p = (QueuePtr)malloc(sizeof(QNode));
if (!p)
return ERROR;
p->next = NULL;
p->data = e;
Q.rear->next = p;
Q.rear = p;
return OK;
}


status DeQueue(LinkQueue &Q, int &e)     //取出对头元素
{
if (Q.front == Q.rear)
return ERROR;
QueuePtr p = Q.front->next;
Q.front->next = p->next;
e = p->data;
if (Q.rear == p)
Q.rear = Q.front;
free(p);
return OK;
}


char menu()   //选择菜单
{
char i;
printf("请选择相应操作:\n");
printf(" a.病人到达,进行排队 \n");
printf(" n.就诊\n");
printf(" b.查看排队情况 \n");
printf(" m.退出\n");
i = _getch();
return i;
}


void  Insert(LinkQueue &Q)   //预先插入一些数据到队列中
{
int i, a;
for (i = 1; i < 4; i++)
{
a = EnQueue(Q, i);
if (a == 0)
exit(0);
}
}


void Display(LinkQueue Q)   //展示Q中的数据
{
QueuePtr p;
p = Q.front->next;
printf("目前排队情况,请排队病人耐心等候:");
while (p != NULL)
{
printf("%d号  ", p->data);
p = p->next;
}
printf("\n");
}


int main()
{
int a, b = 3, c, i = 1;
LinkQueue Q;
a = InitQueue(Q);    //初始化队列Q
if (a == 0)
return ERROR;
Insert(Q);         //预先插入一些元素备用
Display(Q);        //展示Q中现有元素
char ch;    //用户选择相应操作
while (1)
{
ch = menu();
switch (ch)
{
case 'a':
b++;
a = EnQueue(Q, b);
if (a == 0)
{
printf("出现错误!");
exit(0);
}
Display(Q);
break;
case 'n':
a = DeQueue(Q, c);
if (a == 0)
{
printf("出现错误!");
exit(0);
}
printf("现在%d号就诊\n", c);
//Display(Q);
break;
case 'b':
Display(Q);
break;
case 'm':
exit(0);
default:
printf("ERROR,请重新输入!\n");


}
}

return 0;
}


  • 8
    点赞
  • 57
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
链队列可以用来实现银行排队叫号问题。具体实现步骤如下: 1. 定义一个链队列结构体,包含队头指针和队尾指针。 2. 定义一个叫号函数,每当有客户来到银行,就生成一个新的结点,并将其插入队尾。 3. 定义一个服务函数,每当柜台空闲时,从队头取出一个结点,并将其删除。 4. 定义一个显示队列函数,用于显示当前队列中等待服务的客户的信息。 下面是一个示例代码: ```c #include <stdio.h> #include <stdlib.h> typedef struct Node { int num; // 客户号码 struct Node* next; } Node; typedef struct Queue { Node* front; // 队头指针 Node* rear; // 队尾指针 } Queue; // 初始化队列 void initQueue(Queue* q) { q->front = q->rear = NULL; } // 判断队列是否为空 int isQueueEmpty(Queue* q) { return q->front == NULL; } // 入队 void enQueue(Queue* q, int num) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->num = num; newNode->next = NULL; if (isQueueEmpty(q)) { q->front = q->rear = newNode; } else { q->rear->next = newNode; q->rear = newNode; } } // 出队 int deQueue(Queue* q) { if (isQueueEmpty(q)) { printf("Queue is empty!\n"); return -1; } Node* p = q->front; int num = p->num; q->front = q->front->next; free(p); if (q->front == NULL) { q->rear = NULL; } return num; } // 显示队列中的客户号码 void displayQueue(Queue* q) { Node* p = q->front; printf("Queue: "); while (p != NULL) { printf("%d ", p->num); p = p->next; } printf("\n"); } int main() { Queue q; initQueue(&q); int choice, num; do { printf("1. Generate a new customer number\n"); printf("2. Serve a customer\n"); printf("3. Display the queue\n"); printf("0. Exit\n"); printf("Enter your choice: "); scanf("%d", &choice); switch (choice) { case 1: num = rand() % 1000 + 1; // 生成一个随机的客户号码 enQueue(&q, num); printf("New customer number: %d\n", num); break; case 2: num = deQueue(&q); if (num != -1) { printf("Serving customer number: %d\n", num); } break; case 3: displayQueue(&q); break; case 0: printf("Exit!\n"); break; default: printf("Invalid choice!\n"); break; } } while (choice != 0); return 0; } ``` 这个程序可以通过菜单来模拟银行排队叫号的过程。每当选择 1 时,就会生成一个新的客户号码,并将其插入队尾;每当选择 2 时,就会从队头取出一个客户号码,并将其删除;每当选择 3 时,就会显示当前队列中等待服务的客户的信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值