循环队列

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 #define MaxSize 50
 5 typedef struct{
 6     int data[MaxSize];
 7     int front,rear;
 8 }Queue;
 9 //初始化
10 bool InitQueue(Queue &q){
11     q.front=q.rear=0;
12     return true;
13 } 
14 //判断队空
15 bool isEmpty(Queue &q){
16     if(q.front==q.rear)
17     return true;
18     else
19     return false;
20 } 
21 //入队
22 bool EnQueue(Queue &q,int x){
23     if((q.rear+1)%MaxSize==q.front)//判断队满情况 
24     return false;
25     q.data[q.rear]=x; 
26     q.rear=(q.rear+1)%MaxSize;//队尾指针加一 
27     return true;
28 } 
29 //出队
30 bool DeQueue(Queue &q,int &x){
31     if(q.front==q.rear)//队空报错 
32     return false;
33     x=q.data[q.front]; 
34     q.front=(q.front+1)%MaxSize;
35     return true;
36 } 
37 //遍历
38 bool travel(Queue &q){
39     int w=q.front;
40     while(w!=q.rear){
41         printf("%d",q.data[w]);
42         w=(w+1)%MaxSize;
43     }
44     return true;
45 } 
46 int main(){
47     Queue q;
48     int x;
49     InitQueue(q);
50     EnQueue(q,8);
51     EnQueue(q,5);
52     EnQueue(q,6);
53     travel(q);
54     //printf("%d",isEmpty(q));
55     //DeQueue(q,x);
56     //printf("%d",x);
57     return 0;
58 }

 

posted @ 2022-07-12 20:05  梦开始的地方  阅读(39)  评论(0)    收藏  举报