【问题标题】:Queue push not working队列推送不起作用
【发布时间】:2014-11-12 20:40:24
【问题描述】:

我正在尝试实现一些队列操作,但即使在推送所有元素之后,前面似乎仍然是 NULL。在主函数中,我只是读取一些元素并将它们推入队列。我的代码:

typedef struct nod
{
    int info;
    struct nod *link;
}tnod;
tnod *front=NULL,*rear=NULL;
void push(tnod *front,int item)
{
    tnod *tmp;
    tmp=malloc(sizeof(tnod));
    if(tmp==NULL)
    {
        printf("Memorie indisponibila\n");
        return;
    }
    tmp->info = item;
    tmp->link=NULL;
if(front==NULL)      /*daca stiva e goala*/
        {front=tmp; printf("%d",front->info);}
else
    rear->link = tmp;
    rear=tmp;
}

提前致谢。

【问题讨论】:

    标签: c queue


    【解决方案1】:

    您正在使用函数前面的void push(tnod *front,int item) 来隐藏全局变量front

    当您在函数中更改front 时,全局front 不会更改。更改变量名称。

    如果你偶然将全局变量front作为push函数的第一个参数传递,那么通过改变函数中的参数将不会改变全局front。

    【讨论】:

    • 如果我只是从函数中删除该参数,它会起作用吗?
    • @Andy 如果您不需要该参数,可以将其删除。看看你是怎么调用函数的。
    • 我应该更改哪个变量名?是全局的还是函数中的?
    猜你喜欢
    • 2017-11-26
    • 1970-01-01
    • 2013-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多