附加判定标志的循环队列的基本操作
#include<iostream>
#include<cstring>
using namespace std;
const int maxsize = 30;
typedef struct {
int Q[maxsize];
int front;
int rear;
int tag;
}Queue;
bool isEmpty(Queue Q) {
//因入队导致的相等,队满
//因出队导致的相等,队空
if (Q.front == Q.rear && Q.tag == 0) {
return true;
}
return false;
}
bool isFull(Queue Q) {
if (Q.front == Q.rear && Q.tag == 1) {
return true;
}
return false;
}
void EnQueue(Queue& Q,int x) {
if (!isFull(Q)) {
Q.Q[Q.rear] = x;
Q.rear = (++Q.rear) % maxsize;
Q.tag = 1;
}
}
void DeQueue(Queue& Q) {
if (!isEmpty(Q)) {
Q.front = (++Q.front )% maxsize;
Q.tag = 0;
}
}
void print(Queue Q) {
int i = Q.front % maxsize;
while (i != Q.rear) {
cout << Q.Q[i];
i = (++i) % maxsize;
if (i != Q.rear) cout << " ";
}
cout << endl;
}
void InitQueue(Queue& Q, int n) {
Q.front = 0;
Q.rear = 0;
Q.tag = 0;
memset(Q.Q, 0, sizeof(Q.Q));
for (int i = 0; i < n; i++) {
int x;
cin >> x;
EnQueue(Q, x);
}
}
int main() {
int n;
while (cin >> n && n != 0) {
Queue Q;
InitQueue(Q,n);
print(Q);
}
return 0;
}