#include<iostream>
#include<fstream>
#include<stdlib.h>
using namespace std;
#define MAXQSIZE 100
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef struct {
char *data;
int front;
int rear;
} SqQueue;
int InitQueue(SqQueue &Q) {
Q.data = new char[MAXQSIZE];
if (!Q.data)
exit(OVERFLOW);
Q.front = Q.rear = 0;
return OK;
}
bool IsEmpty(SqQueue Q){
if(Q.front==Q.rear)
return true;
else
return false;
}
int QueueLength(SqQueue Q) {
return (Q.rear - Q.front + MAXQSIZE) % MAXQSIZE;
}
int EnQueue(SqQueue &Q, char e) {
if ((Q.rear + 1) % MAXQSIZE == Q.front)
return ERROR;
Q.data[Q.rear] = e;
Q.rear = (Q.rear + 1) % MAXQSIZE;
return OK;
}
int DeQueue(SqQueue &Q, char &e) {
if (Q.front == Q.rear)
return ERROR;
e = Q.data[Q.front];
Q.front = (Q.front + 1) % MAXQSIZE;
return OK;
}
char GetHead(SqQueue Q) {
if (Q.front != Q.rear)
return Q.data[Q.front];
}
void TraverseQueue(SqQueue Q){
while (Q.front != Q.rear) {
cout<<Q.data[Q.front];
Q.front++;
}
}
int main()
{
freopen("input.txt", "r", stdin);
SqQueue Q;
int n;char e;
InitQueue(Q);
cout<<"输入队列的长度:"<<endl;
cin>>n;
cout<<"输入队列的元素:"<<endl;
for (int i = 1; i <= n; i++){
cin>>e;
EnQueue(Q, e);
}
cout<<"现在队首元素是:";
cout<<GetHead(Q)<<endl;
cout<<"队列长度为:"<<QueueLength(Q)<<endl;
cout<<endl<<"请输入要插入的元素的数值:";
cin>>e;
EnQueue(Q,e);
cout<<endl<<"遍历队列结果为:";
TraverseQueue(Q);
cout<<endl;
cout << "依次弹出的队头元素为:\n";
while (DeQueue(Q, e)) {
int flag = -1;
cout << e << " ";
}
cout<<endl;
if (IsEmpty(Q))
cout<<"队列为空"<<endl;
else
cout<<"队列非空"<<endl;
return 0;
}