36-数据结构-循环队列(2.0版本)-加了详细注释

#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值