作者:Mickey-洁de妈咪_212 | 来源:互联网 | 2023-10-15 16:37
C调用队列的基本操作实现病人看病模拟程序包括排队、就诊、查询、退出等功能定义常量,根据需要定义队列的长度constintMaxSize20;队列的结构typed
C++ 调用队列的基本操作实现病人看病模拟程序包括排队、就诊、查询、退出等功能
定义常量,根据需要定义队列的长度
const int MaxSize = 20;
队列的结构
typedef struct {int data[MaxSize];int front, rear;
}SqQueue;
初始化队列
void InitQueue(SqQueue*& q) {q = (SqQueue*)malloc(sizeof(SqQueue));q->front = q->rear = -1;
}
销毁队列
void DestroyQueue(SqQueue*& q) {free(q);
}
判断队列是否为空
bool QueueEmpty(SqQueue*& q) {return(q->front == q->rear);
}
加入队列
bool enQueue(SqQueue*& q, int e) {if (q->rear == MaxSize - 1)return false;q->rear++;q->data[q->rear] = e;return true;
}
退出队列
bool deQueue(SqQueue*& q, int& e) {if (q->front == q->rear)return false;q->front++;e = q->data[q->front];return true;
}
输出当前队列患者
void SearchQueue(SqQueue*& q) {if (QueueEmpty(q)) {cout << "当前没有患者排队。" << endl;return;}SqQueue* p;p &#61; q;int show &#61; p->front;while (show !&#61; p->rear) {cout << "当前患者为&#xff1a;";cout << p->data[show&#43;1]<< endl;show&#43;&#43;;}
}
主程序
int main() {cout << "病人看病程序" << endl;cout << "1.排队" << endl;cout << "2.就诊" << endl;cout << "3.查询" << endl;cout << "4.退出" << endl;SqQueue* q&#61; new SqQueue;InitQueue(q);int number &#61; 1;int now&#61;1;int n;while (1) {cout << "请选择&#xff1a;" << endl;cin >> n;if (n &#61;&#61; 1) {enQueue(q, number);cout << "队尾患者的病历号为&#xff1a;";cout << number << endl;number&#43;&#43;;}if (n &#61;&#61; 2) {if (QueueEmpty(q)) {cout << "当前队列无患者。"<<endl;continue;}deQueue(q, now);cout << "当前就诊患者病历号为&#xff1a;";cout << now << endl;;}if (n &#61;&#61; 3) {if (QueueEmpty(q)) {cout << "当前队列无患者。" << endl;continue;}SearchQueue(q);}if (n &#61;&#61; 4) {DestroyQueue(q);cout << "退出成功。" << endl;break;}}return 0;
}
总代码
#include
using namespace std;const int MaxSize &#61; 20;typedef struct {int data[MaxSize];int front, rear;
}SqQueue;void InitQueue(SqQueue*& q) {q &#61; (SqQueue*)malloc(sizeof(SqQueue));q->front &#61; q->rear &#61; -1;
}void DestroyQueue(SqQueue*& q) {free(q);
}bool QueueEmpty(SqQueue*& q) {return(q->front &#61;&#61; q->rear);
}bool enQueue(SqQueue*& q, int e) {if (q->rear &#61;&#61; MaxSize - 1)return false;q->rear&#43;&#43;;q->data[q->rear] &#61; e;return true;
}bool deQueue(SqQueue*& q, int& e) {if (q->front &#61;&#61; q->rear)return false;q->front&#43;&#43;;e &#61; q->data[q->front];return true;
}void SearchQueue(SqQueue*& q) {if (QueueEmpty(q)) {cout << "当前没有患者排队。" << endl;return;}SqQueue* p;p &#61; q;int show &#61; p->front;while (show !&#61; p->rear) {cout << "当前患者为&#xff1a;";cout << p->data[show&#43;1]<< endl;show&#43;&#43;;}
}int main() {cout << "病人看病程序" << endl;cout << "1.排队" << endl;cout << "2.就诊" << endl;cout << "3.查询" << endl;cout << "4.退出" << endl;SqQueue* q&#61; new SqQueue;InitQueue(q);int number &#61; 1;int now&#61;1;int n;while (1) {cout << "请选择&#xff1a;" << endl;cin >> n;if (n &#61;&#61; 1) {enQueue(q, number);cout << "队尾患者的病历号为&#xff1a;";cout << number << endl;number&#43;&#43;;}if (n &#61;&#61; 2) {if (QueueEmpty(q)) {cout << "当前队列无患者。"<<endl;continue;}deQueue(q, now);cout << "当前就诊患者病历号为&#xff1a;";cout << now << endl;;}if (n &#61;&#61; 3) {if (QueueEmpty(q)) {cout << "当前队列无患者。" << endl;continue;}SearchQueue(q);}if (n &#61;&#61; 4) {DestroyQueue(q);cout << "退出成功。" << endl;break;}}return 0;
}
特别感谢前辈&#xff1a;https://blog.csdn.net/migu77777/article/details/52841370