//#include <stdio.h> // c 库 #include <stdlib.h> //maclloc 库 #include <iostream> // c++ 库 // 有本句 ,下面cout 前面可以没有 std:: using namespace std; #define MAXSIZE 4 typedef char Elem; typedef struct { Elem data[MAXSIZE]; int f, r; //定义队首、尾下标,初始均为0,入队r++,出队f++ ,对MAXSIZE取余后在区间(0,MAXSIZE) }SqQueue; void InitQ(SqQueue& Q) { Q.f = Q.r = 0; } bool EmpQ(SqQueue& Q) { if (Q.f == Q.r) //初始态或出队完毕 return 1; else return 0; } bool FullQ(SqQueue& Q) { if (Q.f == (Q.r + 1) % MAXSIZE) // return 1; else return 0; } void EnQ(SqQueue& Q, Elem x) { if (FullQ(Q)) cout << endl << "full ! " << endl; else { Q.data[Q.r] = x; Q.r = (Q.r + 1) % MAXSIZE; //先赋值后入队,r指向下次赋值位置 } } Elem DelQ(SqQueue& Q) { Elem e; if (EmpQ(Q)) { cout << endl << " empty !" << endl; return 0; } else { e = Q.data[Q.f]; Q.f = (Q.f + 1) % MAXSIZE; return e; } } void main() { Elem e; //出队列元素 SqQueue(Q); InitQ(Q); EnQ(Q, 'a'); EnQ(Q, 'b'); EnQ(Q, 'c'); EnQ(Q, 'd'); e = DelQ(Q); cout << " Del is " << e << endl; //a e = DelQ(Q); cout << " Del is " << e << endl; // b e = DelQ(Q); cout << " Del is " << e << endl; //c EnQ(Q, 'e'); EnQ(Q, 'f'); }