You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

数组实现循环队列遇异常:无法添加第5元素及出队失败求助

循环队列代码问题排查与修复

你的循环队列实现存在几个关键问题,导致无法添加第5个元素、出队功能异常,下面逐一分析并给出修复方案:

1. 野指针引发的非法内存操作

main里只声明了cqueue *q1;,但没给这个指针分配内存,直接传给init函数会操作非法内存,这是最致命的问题,会导致各种不可预测的错误。

修复:
用栈上分配的结构体(更简单)或者动态分配内存:

// 栈上分配(推荐)
cqueue q1;
init(&q1);
// 后续调用函数都传&q1,比如enqueue(&q1, 10);

// 或者动态分配
cqueue *q1 = malloc(sizeof(cqueue));
if (!q1) { /* 处理内存分配失败 */ }
init(q1);
// 记得最后free(q1);

2. 入队时硬编码元素值

enqueue函数里写死了q->array[q->rear]=5;,完全忽略了传入的val参数,导致所有入队元素都是5,这显然不是你想要的。

修复:
把5改成传入的val:

q->array[q->rear] = val;

3. 出队函数返回值逻辑混乱

dequeue的返回值设计逻辑矛盾:成功出队时返回元素值,失败返回1,但调用者无法区分返回的1是出队失败还是出队的元素就是1。应该统一用返回0表示成功,1表示失败,和enqueue的逻辑保持一致。

修复后的dequeue函数:

int dequeue(cqueue *q,int *d){
    if(q->front == -1){ // 统一判断队列为空
        printf("Underflow\n");
        return 1;
    }
    *d = q->array[q->front];
    if(q->front == q->rear){ // 只剩最后一个元素,出队后清空队列
        q->front = q->rear = -1;
    } else {
        q->front = (q->front + 1) % 5;
    }
    return 0; // 成功返回0
}

4. 判满逻辑导致无法存满5个元素

你当前用front == ((rear+1)%5)作为判满条件,这种实现会牺牲一个数组空间来区分队列空和满的状态——也就是说,数组大小为5时,最多只能存4个元素,这就是你加不了第5个元素的原因。

如果想让数组能存满5个元素,可以给结构体加一个count变量记录元素个数,用count == 5判满,count == 0判空:

修改后的结构体和相关函数:

typedef struct {
    int array[5], front, rear, count;
} cqueue;

void init(cqueue *q) {
    q->front = q->rear = -1;
    q->count = 0;
}

int enqueue(cqueue *q,int val){
    if(q->count == 5){
        printf("Overflow\n");
        return 1;
    }
    q->front = (q->front == -1) ? 0 : q->front;
    q->rear = (q->rear + 1) % 5;
    q->array[q->rear] = val;
    q->count++;
    return 0;
}

int dequeue(cqueue *q,int *d){
    if(q->count == 0){
        printf("Underflow\n");
        return 1;
    }
    *d = q->array[q->front];
    if(q->front == q->rear){
        q->front = q->rear = -1;
    } else {
        q->front = (q->front + 1) % 5;
    }
    q->count--;
    return 0;
}

修复后的完整代码

#include <stdio.h>
#include <stdlib.h>

typedef struct {
    int array[5], front, rear, count;
} cqueue;

void init(cqueue *q)
{
    q->front = q->rear = -1;
    q->count = 0;
}

int enqueue(cqueue *q,int val){
    if(q->count == 5){
        printf("Overflow\n");
        return 1;
    }
    q->front = (q->front == -1) ? 0 : q->front;
    q->rear = (q->rear + 1) % 5;
    q->array[q->rear] = val;
    q->count++;
    return 0;
}

int dequeue(cqueue *q,int *d){
    if(q->count == 0){
        printf("Underflow\n");
        return 1;
    }
    *d = q->array[q->front];
    if(q->front == q->rear){
        q->front = q->rear = -1;
    } else {
        q->front = (q->front + 1) % 5;
    }
    q->count--;
    return 0;
}     

void display(cqueue *q){
    if(q->count == 0){
        printf("Queue is empty\n");
        return;
    }
    int n = q->front;
    do{
        printf("%d\n", q->array[n]);
        n = (n + 1) % 5;
    } while(n != (q->rear + 1) % 5);
}

int main() {
    int i;
    cqueue q1;
    init(&q1);
    
    enqueue(&q1,10);
    enqueue(&q1,20);
    enqueue(&q1,30);
    enqueue(&q1,40);
    enqueue(&q1,50); // 现在可以成功添加第5个元素
    
    printf("Queue after enqueues:\n");
    display(&q1);
    
    dequeue(&q1,&i);
    dequeue(&q1,&i);
    enqueue(&q1,60);
    
    printf("\nQueue after dequeues and enqueue:\n");
    display(&q1);
    
    return 0;
}

内容的提问来源于stack exchange,提问作者thetacodude

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 17:31:03