#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
# define N 7
struct Queue{
int *pBase;
int front;
int rear;
};
void init(struct Queue*);
bool is_full(struct Queue*);
bool is_empty(struct Queue*);
bool en_queue(struct Queue*,int);
bool out_queue(struct Queue*,int*);
void traverse_queue(struct Queue*);
int main(void)
{
int val;
struct Queue Q;
init(&Q);
en_queue(&Q,1);
en_queue(&Q,2);
en_queue(&Q,3);
traverse_queue(&Q);
if(out_queue(&Q,&val))
{
printf("%d is out of the queue!\n",val);
}
else
{
printf("The queue is empty!\n");
}
traverse_queue(&Q);
return 0;
}
void init(struct Queue *pQ)
{
pQ->pBase=(int *)malloc(sizeof(int)*N);
if(NULL==pQ->pBase)
{
printf("动态内存申请失败!\n");
exit(-1);
}
else
{
pQ->front=0;
pQ->rear=0;
return;
}
}
bool is_full(struct Queue *pQ)
{
if((pQ->rear+1)%N==pQ->front)
return true;
else
return false;
}
bool is_empty(struct Queue *pQ)
{
if(pQ->front==pQ->rear)
return true;
else
return false;
}
bool en_queue(struct Queue *pQ,int val)
{
if(is_full(pQ))
{
return false;
}
else
{
pQ->pBase[pQ->rear]=val;
pQ->rear=(pQ->rear+1)%N;
return true;
}
}
bool out_queue(struct Queue *pQ,int *pVal)
{
if(is_empty(pQ))
return false;
else
{
*pVal=pQ->pBase[pQ->front];
pQ->front=(pQ->front+1)%N;
return true;
}
}
void traverse_queue(struct Queue *pQ)
{
int i=pQ->front;
while(i!=pQ->rear)
{
printf("%d ",pQ->pBase[i]);
i=(i+1)%N;
}
printf("\n");
return;
}
08-14
1355
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-14
465
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-14
413
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)