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]
*/
加油,你我一起进步,与君共勉!!!