【问题标题】:Dequeue function does not work [closed]出队功能不起作用[关闭]
【发布时间】:2023-04-04 03:00:02
【问题描述】:

我正在尝试使用代码块学习 C,我的部分任务是使用队列作为输入和输出方法。

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

typedef struct QUEUE {
    int head, tail;
    int list[4];
} queue;

void create( queue *q ) {
    q->head = 0;
    q->tail = 0;
}

int empty( queue *q ) {
    if ( q->tail == 0 )
        return 1;
    else
        return 0;
}

int full( queue *q ) {
    if ( q->tail == 4 )
        return 1;
    else
        return 0;
}

void enqueue( queue *q ) {
    int i, data;
    if ( full( q ) == 1 )
        printf( "\nQueue is Full" );
    else {
        printf( "\nEnqueue Integer : " );
        scanf( "%d", &data );
        q->list[q->tail] = data;
        q->tail++;
    }
}

void dequeue( queue *q ) {
    int i;
    if ( empty( q ) == 1 )
        printf( "\n Queue is Empty" );
    else
        q->head++;
}

void main( ) {
    int select;
    queue q;

    create( &q );

    while ( 1 ) {
        printf( "\n1->Enqueue \t 2->Dequeue \t 3->Exit: " );
        scanf( "%d", &select );

        switch ( select ) {
        case 1:     enqueue( &q );
                    break;
        case 2:     dequeue( &q );
                    break;
        default:    goto out;
        }
    }
out:
    exit( EXIT_SUCCESS );
}

这是我的代码,出队函数返回队列一开始是空的,但是在我入队一个整数后,它根本不会出队。谢谢你的帮助。我正在使用代码块来编译我的程序。

【问题讨论】:

  • 您对dequeue 有什么期望?也许将其重置为原始状态?
  • 可以,但是如果队列已满,出队功能将不起作用。
  • 你是说一旦你enqueue一个值,使用dequeue两次不会打印“队列为空”?
  • 投票结束。你还没有告诉你的代码应该做什么,所以我们不能帮助修复它。
  • 此代码中没有正确使用尾部成员或头部成员。它缺少必要的代码来管理头部和尾部之间的差异以确定完整、空和元素数信息。

标签: c queue


【解决方案1】:

在 struct QUEUE 内部创建一个新的变量 size,在创建队列时初始化为 0,表示你队列中存储的数据量。

typedef struct QUEUE {
    int head, tail,size;
    int list[4];
} queue;

void create( queue *q ) {
    q->head = 0;
    q->tail = -1;
    q->size=0;
}

因此大小 0 表示我们的队列中没有数据,大小 4 表示队列已满。

int empty( queue *q ) {
    if ( q->size==0 )
        return 1;
    else
        return 0;
}

int full( queue *q ) {
    if ( q->size==4)
        return 1;
    else
        return 0;
}

当您将数据入队时,大小会增加 1,当您将数据出队时,大小会减少 1。

void enqueue( queue *q ) {
    int i, data;
    if ( full( q ) == 1 )
        printf( "\nQueue is Full" );
    else {
        printf( "\nEnqueue Integer : " );
        scanf( "%d", &data );
        q->tail++;
        q->size++;
        if(q->tail==4){
            q->tail=0;
        }
        q->list[q->tail] = data;
        if(tail==4){
            q->tail==0;
        }
    }
}

void dequeue( queue *q ) {
    int i;
    if ( empty( q ) == 1 )
        printf( "\n Queue is Empty" );
    else{
        q->head++;
        q->size--;
    }
}

注意-您的代码缺少队列的循环流。当 tail 越过分配的最后一块内存时,它应该到达第一个块。

所以,我们在 enqueue 函数中添加了这个:

if(tail==4){
                q->tail==0;
            }

【讨论】:

    【解决方案2】:

    好的,根据您的评论,您似乎希望在将 queue 的地址传递给 dequeue 函数时重置它。这将是一种肤浅的答案,但是您为什么不在必要时将dequeue 函数内的q-&gt;tail 重置回0

    void dequeue( queue *q ) {
        int i;
        if ( ... )
            printf( ... );
        else
            q->tail = 0;    // <-- changed this
    }
    

    结构中的head 属性似乎一点用都没有,至少现在是这样。

    【讨论】:

    • 谢谢,但这会重置整个队列,我要做的是一次删除一个节点。
    • @user3483503 在您的代码中没有诸如 nodes 之类的东西...您是否希望dequeue 只删除排队的第一个元素?
    【解决方案3】:

    队列是一个 FIFO(先进先出),您有一个固定大小的 FIFO(4 个整数)以及一个指向头部和尾部的“指针”。

    查看您的代码,tail 指向下一个可用空间,而head 指向第一个填充空间。

    当您 enqueue 时,您添加值并增加 tail 中的值。

    当您dequeue 时,您会增加head 中的值。

    这没关系,除了 headtail 应该是数组的索引。您绝不会减少存储在其中的值。这意味着一旦您向队列中添加了 4 个值,您就不能再添加了。

    您还假设tail == 0时列表为空,但是如果head == tailhead == 1,根据您的代码,在这种情况下列表为空,但empty函数不会知道它是空的。

    由于您没有使用动态内存,我将完全删除 head 变量。

    当您dequeue 时,我会将内部数组中的值移动到前一个索引中。所以list[1] 被放入list[0]。然后递减tail

    【讨论】:

      猜你喜欢
      • 2013-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-03
      • 2013-08-06
      • 2021-03-26
      相关资源
      最近更新 更多