#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<stdbool.h>
#include<conio.h>
#include<queue>
#define TRUE 1
#define FALSE 0
using namespace std;
typedef char QueueElemType;
#define MAXSIZE 10
//循环队列的相关操作
//利用一个空结点
typedef struct {
QueueElemType data[MAXSIZE];
int rear, front;
} CycQueue;
void InitCycQueue(CycQueue *Q)
{
Q->front = 0;
Q->rear = 0;
}
int IsQueueEmpty(CycQueue Q)
{
return Q.front == Q.rear;
}
int IsQueueFull(CycQueue *Q)
{
return (Q->rear + 1) % MAXSIZE == Q->front;
}
int EnterQueue(CycQueue *Q, QueueElemType x)
{
if ((Q->rear + 1) % MAXSIZE == Q->front)
return FALSE;
Q->rear = (Q->rear + 1) % MAXSIZE;
Q->data[Q->rear] = x;
return TRUE;
}
int DeleteQueue(CycQueue *Q, QueueElemType *x)
{
if (Q->front == Q->rear)
return FALSE;
*x = Q->data[Q->front];
Q->front = (Q->front + 1) % MAXSIZE;
return TRUE;
}
int GetHead(CycQueue *Q, QueueElemType *x)
{
if (Q->front == Q->rear)
return FALSE;
*x = Q->data[Q->front];
return TRUE;
}
int ClearQueue(CycQueue *Q)
{
Q->front = 0;
Q->rear = 0;
return TRUE;
}
//模拟控制台输入输出缓冲区
void BufferInput()
{
char ch1, ch2;
CycQueue Q;
InitCycQueue(&Q);
for (;; ) {
for (;;) {
cout<<"A";
if (_kbhit()) {
ch1 = getchar();
if (ch1 == ';' || ch1 == ',')
break;
int res = EnterQueue(&Q, ch1);
if (res == NULL) {
cout << "循环队列已满"<<endl;
break;
}
}
}
while (!IsQueueEmpty(Q)) {
DeleteQueue(&Q, &ch2);
putchar(ch2);
}
if (ch1 == ';')
break;
}
}
//模拟看病过程
void SeeDoctor() {
queue<int> q;
int n = 0;
int flag = 1;
while (flag) {
cout << "请输入命令";
char ch = getche();
switch (ch) {
case 'p':
cout << "请输入病历号" << endl;
scanf("%d", &n);
q.push(n);
break;
case 'n':
if (!q.empty()) {
n = q.front();
q.pop();
cout << "病历号为" << n << "的病人就诊" << endl;
}
else
cout << "没有正在等待的病人" << endl;
break;
case 'q':
cout << "停止挂号,下列病人依次就诊" << endl;
while (!q.empty()) {
n = q.front();
q.pop();
cout << n << " ";
}
flag = 0;
break;
default:
cout << "非法字符" << endl;
}
}
}
int main() {
//BufferInput();
SeeDoctor();
return 0;
}
循环队列实现
最新推荐文章于 2024-08-02 09:58:01 发布