1 #include2 #include 3 using namespace std; 4 5 #define maxSize 20 6 7 typedef struct 8 { 9 int data[maxSize];10 int front;11 int rear;12 }SqQueue;13 14 void InitQueue(SqQueue &Q)15 {16 Q.front=Q.rear=0;17 }18 19 int IsEmpty(SqQueue Q)20 {21 if(Q.front==Q.rear)22 return 1;23 else24 return 0;25 }26 27 int EnQueue(SqQueue &Q,int e)28 {29 if((Q.rear+1)%maxSize==Q.front)30 return 0;//队满31 Q.rear=(Q.rear+1)%maxSize;32 Q.data[Q.rear]=e;33 return 1;34 }35 36 int DeQueue(SqQueue &Q,int &e)37 {38 if(Q.front==Q.rear)39 return 0;40 Q.front=(Q.front+1)%maxSize;41 e=Q.data[Q.front];42 return 1;43 }44 45 int Print(SqQueue Q)46 {47 if(Q.front==Q.rear)48 {49 cout<<"\n队空!\n";50 return 0;51 }52 cout<<"队中元素依次为:\n";53 while(Q.front!=Q.rear)54 {55 (++Q.front)%maxSize;56 cout<<" "< <<" ";57 }58 return 1;59 }60 61 int main()62 {63 int i,e;64 SqQueue Q;65 InitQueue(Q);66 if(IsEmpty(Q)==1)67 cout<<"\n队空!\n";68 else69 cout<<"\n队非空!\n";70 71 cout<<"\n---------------------------------\n";72 for(i=0;i<10;++i)73 EnQueue(Q,i);74 Print(Q);75 if(IsEmpty(Q)==1)76 cout<<"\n队空!\n";77 else78 cout<<"\n队非空!\n";79 cout<<"\n---------------------------------\n";80 81 cout<<"依次出队5个元素为:\n";82 for(i=0;i<5;++i)83 {84 DeQueue(Q,e);85 cout<<" "< <<" ";86 }87 cout<