数据结构--队列

#include <stdio.h>

typedef struct
{
    int qHeader;
    int qRear;
    int qSize;
    int* queue;
} queueType;

void enQueue(queueType *q, int data){
    //添加判断语句,如果rear超过Size,则直接将其从a[0]重新开始存储,如果rear+1和front重合,则表示数组已满
    if (((q->qRear) + 1) % (q->qSize + 1) == q->qHeader) {
        printf("\nfull\n");
        return;
    }
    q->queue[q->qRear % q->qSize] = data;
    //printf("a%d = %d ", *rear, a[(*rear)%Size]);
    q->qRear++;
}
void deQueue(queueType *q, int data){
    //如果Header==rear,表示队列为空
    if(q->qHeader == (q->qRear) % q->qSize) {
        printf("empty");
        return;
    }
    //printf("out: %d\n",a[*Header]);
    //front不再直接 +1,而是+1后同Size进行比较,如果=Size,则直接跳转到 a[0]
    q->qHeader = (q->qHeader + 1) % q->qSize;
}

int queue[10] = {0};
queueType queue1;
int main() {
    //设置队头指针和队尾指针,当队列中没有元素时,队头和队尾指向同一块地址
    queue1.qHeader = 0;
    queue1.qRear = 0;
    queue1.queue = queue;
    queue1.qSize = sizeof(queue) / sizeof(int);
    for (int i = 0; i < 10; i++)
    {
        enQueue(&queue1, i);
    }
    for(int i = 0; i < 10; i++)
    {
        printf("%d ", queue[i]);
    }
    enQueue(&queue1, 10);
    return 0;
}

输出:
0 1 2 3 4 5 6 7 8 9
full

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值