1 #include <iostream> 2 #include <stdlib.h> 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 else 24 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<<" 队空! "; 50 return 0; 51 } 52 cout<<"队中元素依次为: "; 53 while(Q.front!=Q.rear) 54 { 55 (++Q.front)%maxSize; 56 cout<<" "<<Q.data[Q.front] <<" "; 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<<" 队空! "; 68 else 69 cout<<" 队非空! "; 70 71 cout<<" --------------------------------- "; 72 for(i=0;i<10;++i) 73 EnQueue(Q,i); 74 Print(Q); 75 if(IsEmpty(Q)==1) 76 cout<<" 队空! "; 77 else 78 cout<<" 队非空! "; 79 cout<<" --------------------------------- "; 80 81 cout<<"依次出队5个元素为: "; 82 for(i=0;i<5;++i) 83 { 84 DeQueue(Q,e); 85 cout<<" "<<e<<" "; 86 } 87 cout<<endl; 88 Print(Q); 89 if(IsEmpty(Q)==1) 90 cout<<" 队空! "; 91 else 92 cout<<" 队非空! "; 93 cout<<" --------------------------------- "; 94 95 return 0; 96 }