#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
//【1】定义数据类型
typedef struct queue
{
//【2】两个参数 front rear
int front; int rear;
//【3】数组名字
int* pbase;
}QUEUE,*PQUEUE;
void init(PQUEUE PQ)
{
//【5.1】创建数组首地址
PQ->pbase = (int *)malloc(sizeof(int)*6);
//【5.2】front 和rear 初始化
PQ->front = 0;
PQ->rear = 0;
}
void entry_queue(PQUEUE PQ,int val)
{
//【6.1】把变量放进去
PQ->pbase[PQ->rear] = val; //移动尾巴
//【6.2】移动尾巴
PQ->rear = (PQ->rear + 1)%6;
}
void travel_queue(PQUEUE PQ)
{
//【7.1】相当于出站,输出pbase[pq->front],直到front 等于rear
int i=PQ->front;//最好就写成i
while (i != PQ->rear)
{
printf("输出=%d\n",PQ->pbase[i]);
i = (i + 1) % 6;
printf("此时i=%d\n\n", i);
}
}
int main()
{
//【4】生成对列
QUEUE Q;
//【5】初始化对列
init(&Q);
//【6】入队
entry_queue(&Q,5);
entry_queue(&Q,4);
entry_queue(&Q,3);
entry_queue(&Q,2);
//【7】遍历
travel_queue(&Q);
return 0;
}