C语言实现循环队列的构建、入队、出队、扩容、遍历等操作

C语言实现循环队列的构建、入队、出队、扩容、遍历等操作

一 队列的定义和初始化

  在C++的STL中有queue可以直接使用,小编这里采用C语言实现队列的基本操作,毕竟学会自己“造轮子”而不是拿来主义,可以更深刻的体会队列的性质。
  队列最大的性质就是先进先出,元素只可以从队尾入队,从队首出队列。至于队列和循环队列之间的区别,不及,我们后面会说。
  话不多说,我们直接看数据结构定义:

typedef struct queue {
    int *data;  //数据域
    int head, tail, cnt, size;
    //head,tail表示队首和队尾指针,cnt表示队列中元素个数,size表示数组大小
} Queue;

//队列中队尾指针我们这里表示下一个可以插入元素的位置,而不是队列中队尾元素的下标
Queue *init(int size) {
    Queue *q = (Queue *)malloc(sizeof(Queue));
    q->data = (int *)malloc(sizeof(int) * size);
    q->head = q->tail = q->cnt = 0; 
    q->size = size;
    return q;
}
二 元素入队列

  将元素从队尾放入队列,很简单,直接将元素放入队列的尾部,队尾指针后移,同时队列元素个数加一
  示意图如下:
元素入队列
  但是这里有一个问题,我们现在是用一个数组来存储元素,那么随着元素的入队出队,head和tail指针将会不断的后移,当数组最后一个空格已经存入元素,此时若我们想要再存入元素,数组是不是已经满了呢?我们现在是不是要重新申请一片数组空间,再将现在的元素复制过去呢?
  显然不是,因为随着元素出队列,head向后移动,所以数组的前面部分是空的,我们可以利用这一部分来存储元素,其实这就是循环队列,而不是普通的队列 (是不是感觉很简单?)
  示意图如下:
假溢出
循环队列
对应实现代码为:

bool expand(Queue *q) {  //扩容操作
    int extra_size = q->size;
    int *old_data = q->data;
    while (extra_size) {  //扩容
        q->data = (int *)malloc(sizeof(int) * (q->size + extra_size));
        if (q->data) break;
        extra_size /= 2;
    }
    if (!q->data) return false;
    for (int i = 0, j = q->head; i < q->cnt; i++) {  //元素复制
        q->data[i] = old_data[(i + j) % q->size];
    }
    q->head = 0, q->tail = q->cnt;  //队列变量的变化
    q->size += extra_size;
    free(old_data);
    return true;
}

bool push(Queue *q, int val) {  //元素入队列
    if (!q) return q;
    if (q->cnt == q->size) {
        if (expand(q)) printf("expand success!\n");
        else {
            printf("expand failed\n");
            return false;
        }
    }
    q->data[q->tail++] = val;
    if (q->tail == q->size) q->tail = 0;
    q->cnt++;
    return true;
}
三 元素出队列

  将元素出队列,操作和元素入队列类似,将head指针后移,当到达数组最后一个元素的下标时,将head指向0
  示意图如下:
元素出队列
实现代码如下:

bool pop(Queue *q) {
    if (!q || q->cnt == 0) return false;
    q->head++;
    if (q->head == q->size) q->head = 0;
    q->cnt--;
    return true;
}
四 判队列空、遍历输出队列、取队首元素

代码逻辑较为简单,因此此处直接粘贴代码,不再赘述

bool empty(Queue *q) {  //判队列空
    return q->cnt == 0;
}
int front(Queue *q) {  //取队首元素
    return q->data[q->head];
}

void output(Queue *q) {  //遍历输出
    if (!q) return ;
    printf("queue(%d) = [", q->cnt);
    for (int i = 0, j = q->head; i < q->cnt; i++) {
        if (i) printf(", ");
        printf("%d", q->data[(i + j) % q->size]);
    }
    printf("]\n");
}

void clear(Queue *q) {  //删除队列
    if (!q) return ;
    free(q->data);
    free(q);
}
五 总代码献上
#include <iostream>
using namespace std;

typedef struct queue {
    int *data;
    int head, tail, cnt, size;
} Queue;

Queue *init(int size) {
    Queue *q = (Queue *)malloc(sizeof(Queue));
    q->data = (int *)malloc(sizeof(int) * size);
    q->head = q->tail = q->cnt = 0;
    q->size = size;
    return q;
}

bool empty(Queue *q) {
    return q->cnt == 0;
}
int front(Queue *q) {
    return q->data[q->head];
}

bool expand(Queue *q) {
    int extra_size = q->size;
    int *old_data = q->data;
    while (extra_size) {
        q->data = (int *)malloc(sizeof(int) * (q->size + extra_size));
        if (q->data) break;
        extra_size /= 2;
    }
    if (!q->data) return false;
    for (int i = 0, j = q->head; i < q->cnt; i++) {
        q->data[i] = old_data[(i + j) % q->size];
    }
    q->head = 0, q->tail = q->cnt;
    q->size += extra_size;
    free(old_data);
    return true;
}
bool push(Queue *q, int val) {
    if (!q) return q;
    if (q->cnt == q->size) {
        if (expand(q)) printf("expand success!\n");
        else {
            printf("expand failed\n");
            return false;
        }
    }
    q->data[q->tail++] = val;
    if (q->tail == q->size) q->tail = 0;
    q->cnt++;
    return true;
}

bool pop(Queue *q) {
    if (!q || q->cnt == 0) return false;
    q->head++;
    if (q->head == q->size) q->head = 0;
    q->cnt--;
    return true;
}

void output(Queue *q) {
    if (!q) return ;
    printf("queue(%d) = [", q->cnt);
    for (int i = 0, j = q->head; i < q->cnt; i++) {
        if (i) printf(", ");
        printf("%d", q->data[(i + j) % q->size]);
    }
    printf("]\n");
}

void clear(Queue *q) {
    if (!q) return ;
    free(q->data);
    free(q);
}

int main() {
    #define MAX_OP 20 
    srand(time(0));
    Queue *q = init(1);
    int val, op;
    for (int i = 0; i < MAX_OP; i++) {
        val = rand() % 100;
        op = rand() % 4;
        switch (op) {
            case 0:
            case 1:
            case 2: {
                printf("push %d into queue = %d\n", val, push(q, val));
                break;
            }
            case 3: {
                printf("pop %d from queue = ", front(q));
                printf("%d\n", pop(q));
            }
        }
        output(q);
        printf("\n");
    }
    clear(q);
    return 0;
}
/*结果输出:
push 29 into queue = 1
queue(1) = [29]

expand success!
push 97 into queue = 1
queue(2) = [29, 97]

expand success!
push 25 into queue = 1
queue(3) = [29, 97, 25]

push 83 into queue = 1
queue(4) = [29, 97, 25, 83]

expand success!
push 24 into queue = 1
queue(5) = [29, 97, 25, 83, 24]

push 97 into queue = 1
queue(6) = [29, 97, 25, 83, 24, 97]

push 48 into queue = 1
queue(7) = [29, 97, 25, 83, 24, 97, 48]

push 21 into queue = 1
queue(8) = [29, 97, 25, 83, 24, 97, 48, 21]

expand success!
push 57 into queue = 1
queue(9) = [29, 97, 25, 83, 24, 97, 48, 21, 57]

pop 29 from queue = 1
queue(8) = [97, 25, 83, 24, 97, 48, 21, 57]

pop 97 from queue = 1
queue(7) = [25, 83, 24, 97, 48, 21, 57]

pop 25 from queue = 1
queue(6) = [83, 24, 97, 48, 21, 57]

push 76 into queue = 1
queue(7) = [83, 24, 97, 48, 21, 57, 76]

push 35 into queue = 1
queue(8) = [83, 24, 97, 48, 21, 57, 76, 35]

push 9 into queue = 1
queue(9) = [83, 24, 97, 48, 21, 57, 76, 35, 9]

push 73 into queue = 1
queue(10) = [83, 24, 97, 48, 21, 57, 76, 35, 9, 73]

push 44 into queue = 1
queue(11) = [83, 24, 97, 48, 21, 57, 76, 35, 9, 73, 44]

push 30 into queue = 1
queue(12) = [83, 24, 97, 48, 21, 57, 76, 35, 9, 73, 44, 30]

push 51 into queue = 1
queue(13) = [83, 24, 97, 48, 21, 57, 76, 35, 9, 73, 44, 30, 51]

pop 83 from queue = 1
queue(12) = [24, 97, 48, 21, 57, 76, 35, 9, 73, 44, 30, 51]
*/

加油,你我一起进步,与君共勉!!!

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
循环队列是利用数组实现队列的一种数据结构,其特点是可以避免数组元素的复制移动,从而提高队列操作的效率。下面是使用C语言来建立循环队列实现相关操作的示例代码: 首先,我们需要定义一个结构体来表示循环队列: typedef struct { int *data; // 队列的存储空间 int front; // 队头指针 int rear; // 队尾指针 int size; // 队列的长度 } circular_queue; 接下来,我们可以使用malloc函数为循环队列分配存储空间,并在初始化函数中对其进行初始化: circular_queue *init_circular_queue(int size) { circular_queue *queue = (circular_queue *)malloc(sizeof(circular_queue)); queue->data = (int *)malloc(sizeof(int) * size); queue->front = queue->rear = 0; queue->size = size; return queue; } 入队操作可以通过将队尾指针往后移动一位实现: void enqueue(circular_queue *queue, int value) { if ((queue->rear + 1) % queue->size == queue->front) { printf("Queue is full.\n"); return; } queue->data[queue->rear] = value; queue->rear = (queue->rear + 1) % queue->size; } 出队操作可以通过将队头指针往后移动一位实现: int dequeue(circular_queue *queue) { if (queue->front == queue->rear) { printf("Queue is empty.\n"); return -1; } int value = queue->data[queue->front]; queue->front = (queue->front + 1) % queue->size; return value; } 遍历队列中的元素可以通过循环从队头指针到队尾指针实现: void traverse(circular_queue *queue) { int i; printf("Queue: "); for (i = queue->front; i != queue->rear; i = (i + 1) % queue->size) { printf("%d ", queue->data[i]); } printf("\n"); } 总的来说,循环队列可以通过指针的移动来实现队列中元素的添加、删除等操作,可以提高队列操作的效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值