推送堆栈未插入新值-C [英] Push of stack not inserting the new value - C

查看:28
本文介绍了推送堆栈未插入新值-C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

void push(stack *head, int valuee)
{
    if(head->next==NULL && head->value==-1)
    {
        head->value = valuee;
        printf("First element %d inserted\n",valuee);
    }
    else
    {
        stack *temp = new stack;
        temp->value = valuee;
        temp->next = head;
        head = temp;
        printf("Element %d inserted\n",valuee);
    }
}

第一个元素已正确插入,但当我继续插入元素时,第一个元素之后均未插入任何元素.读到某个地方,我必须将指针传递给堆栈的指针,但是在后缀中缀问题期间我做了同样的事情,并且在那儿工作.在这里帮助我解决这个问题.非常感谢您提前提供的帮助

First element is inserted properly but when i continue inserting elements, none of the elements are inserted after the first one. Read somewhere that i have to pass pointer to pointer of stack but I did this same thing during a postfix infix question and it was working over there. Help me with this problem here. Many thanks for any help in advance

以前的infix后缀示例工作正常

Previous example of infix postfix which was working fiine

void push(char c, stack *node)
{
    stack *B = (stack *)malloc(sizeof(stack));
    if (node->next == NULL)
    {
        node->next = B;
        B->value = c;
        B->next =NULL;
    }
    else
    {
        B->next = node->next;
        node->next = B;
        B->value = c;
    }
}

推荐答案

您可以更改函数

    stack* push(stack *head, int valuee)
    //return void to stack * 
    return head;
    //In the end return the new head

,它将起作用.像这样呼叫推送

and it will work. Call push like this

head = push(head,value);

这篇关于推送堆栈未插入新值-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆