#include<iostream>
#include <string>
using namespace std;
#define MAXSIZE 10
typedef string QElemType;
typedef int Status;
typedef struct QUEUE
{
QElemType *base;//動態分配存盤空間
int front;//頭指標
int rear;//尾指標
}SqQueue;
Status InitQueue(SqQueue Q)
{
Q.base = (QElemType*)malloc(MAXSIZE * sizeof(QElemType));//初始化佇列
if (!Q.base)
{
exit(0);
}
Q.rear = Q.front = 0;
return 1;
}
Status EnQueue(SqQueue Q, QElemType e)//排隊
{
if ((Q.rear + 1) % 10 == Q.front)//如果隊滿
{
cout << "waiting" << endl;
return 0;
}
Q.base[Q.rear] = e;
Q.rear = (Q.rear + 1) % 10;
return 1;
}
Status Dequeue(SqQueue Q, QElemType &e)//售票
{
if (Q.front == Q.rear)//隊空
{
cout << "無人排隊" << endl;
return 0;
}
e = Q.base[Q.front];
Q.front = (Q.front + 1) % 100;
cout << e << "購票成功" << endl;;
return 1;
}
Status QueueTraverse(SqQueue Q, QElemType e)//查看佇列
{
int i;
i = Q.front;
while (i != Q.rear)
{
cout << Q.base[i];
i = (i + 1) % 10;
}
cout << endl;
return 1;
}
int main()
{
cout << "排隊:1 售票:2 查看佇列:3 結束:4" << endl;
SqQueue q1 = { 0 };
InitQueue(q1);
int a;
string e;
loop:cin >> a;
if (a == 1) {
cin >> e;
EnQueue(q1, e);
goto loop;
}
if (a == 2)
{
Dequeue(q1, e);
goto loop;
}
if (a == 3)
{
QueueTraverse(q1, e);
goto loop;
}if (a == 4); return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/63009.html
標籤:茶館
上一篇:delete結構體變數出錯?
