#include <iostream>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
typedef struct {
int* base;
int front;
int rear;
}SqQueue;
Status InitQueue(SqQueue &Q) {
Q.base = new int[10];
if (!Q.base) exit(OVERFLOW);
Q.rear = 0;
Q.front = 0;
return 0;
}
Status QueueLenght(SqQueue &Q) {
return(Q.rear -Q.front + 10) % 10;
}
Status EnQueue(SqQueue& Q, int e) {
if ((Q.rear + 1) % 10 == Q.front) return ERROR;
Q.base[Q.rear]=e;
Q.rear = (Q.rear + 1) % 10;
return OK;
}
Status DeQueue(SqQueue& Q, int& e) {
if (Q.front == Q.rear) return ERROR;
e = Q.base[Q.front];
Q.front = (Q.front + 1) % 10;
return OK;
}
Status GetHead(SqQueue& Q) {
if (Q.front != Q.rear) return Q.base[Q.front];
}
int main()
{
SqQueue sq;
int status,in,num,temp,len,HeadEle;
InitQueue(sq);
while (true)
{
cout << "1、数据入队。" << endl;
cout << "2、队伍长度。" << endl;
cout << "3、数据出队。" << endl;
cout << "4、队头元素。" << endl;
cout << "5、输入其他数字退出" << endl;
cout << "------------------------------" << endl;
cout << "你的选择:";
cin >> in;
switch (in)
{
case 1:
cout << "需要入队的数据:" << endl;
cin >> num;
if (EnQueue(sq, num) == 0) {
cout << "队满" << endl;
cout << "------------------------------" << endl;
continue;
}
else {
cout << "入队成功->" << num << endl;
cout << "------------------------------" << endl;
continue;
}
case 2:
len = QueueLenght(sq);
cout << "当前队长->" << len << endl;
cout << "------------------------------" << endl;
continue;
case 3:
if (DeQueue(sq, temp) == 1){
cout << "已出队->" << temp << endl;
cout << "------------------------------" << endl;
continue;
}
else{
cout << "队空" << endl;
cout << "------------------------------" << endl;
continue;
}
case 4:
HeadEle = GetHead(sq);
cout << "当前头元素->" << HeadEle << endl;
cout << "------------------------------" << endl;
continue;
default:
cout << "输入错误,程序退出"<< endl;
cout << "------------------------------" << endl;
break;
}
break;
}
}
标签:Status,顺序,return,队列,SqQueue,int,front,rear
From: https://www.cnblogs.com/qianyuzz/p/17059932.html