采用循环队列或链队列实现病人看病的模拟程序

采用循环队列或链队列实现病人看病的模拟程序

1)定义队列的存储结构;

2)实现队列的初始化、判断是否为空、入队、出队等基本操作;

3)调用队列的基本操作实现病人看病模拟程序包括排队、就诊、查询、退出等功能;


代码:

#include <iostream>
#include<stdio.h>
#include<stdlib.h>
#include <malloc.h>
using namespace std;
typedef int QElemType;

//存储结构
typedef struct QNode {
	QElemType data;
	struct QNode *next;
}Qnode, *QueuePtr;

//对头队尾指针
typedef struct {
	QueuePtr front;//对头指针
	QueuePtr rear; //队尾指针
}LinkQueue;

LinkQueue Q;
int n;

//构造一个空队列
void InitQueue(LinkQueue &Q) {
	Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode));
	Q.front->next = NULL;
}

//判断是否为空
bool KoQueue(LinkQueue &Q) {
	if (Q.front == Q.rear) return true;
	return false;
}

//入队
void EnQueue(LinkQueue &Q, QElemType e) {
	Qnode *p;
	p = (QueuePtr)malloc(sizeof(QNode));
	p->data = e;
	p->next = NULL;
	Q.rear->next = p;
	Q.rear = p;
}

//出队
void DeQueue(LinkQueue &Q, QElemType &e) {
	//若队列不空,删除Q的对头元素,用e返回其值
	if (KoQueue(Q)) {
		printf("当前无就诊患者!\n");
		return;
	}
	Qnode *p;
	p = Q.front->next;
	e = p->data;
	Q.front->next = p->next;
	if (Q.rear == p)
		Q.rear = Q.front;
	free(p);
}
//查询
void SearchQueue(LinkQueue &Q) {
	QNode *p;
	if (KoQueue(Q)) {
		printf("当前无排队患者!\n");
		return;
	}
	else {
		printf("当前排队患者:\n");
		p = Q.front->next;
		while (p != NULL) {
			printf("病历号为%d的患者\n", p->data);
			p = p->next;
		}
	}
}
void QuitQueue(LinkQueue &q) {
	while (Q.front) {
		Q.rear = Q.front->next;
		free(Q.front);
		Q.front = Q.rear;
	}
}
//排队 就诊,查询,退出

int main() {
	printf("患者看病系统\n\n");
	printf("1.排队\n");
	printf("2.就诊\n");
	printf("3.查询\n");
	printf("4.退出\n\n");
	InitQueue(Q);
	int num = 1;
	int nowe;
	while (1) {
		printf("请选择:");
		scanf("%d", &n);
		if (n == 1) {//排队
			EnQueue(Q, num);
			printf("队尾患者病历号为:%d\n", num);
			num++;
		}
		if (n == 2) {//就诊
			DeQueue(Q, nowe);
			printf("当前就诊患者病历号为:%d\n", nowe);
		}
		if (n == 3) {//查询
			SearchQueue(Q);
		}
		if (n == 4) {//退出
			QuitQueue(Q);
			printf("您已成功退出!\n");
			break;
		}
	}
	system("pause");
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值