简体   繁体   English

如何解决 C 中的堆栈问题?

[英]how can i solve the problem with stack in C?

i made Global Variables我做了全局变量

struct node* top;
struct node {
    char x;
    struct node* next;
};

it is struct node它是结构节点

struct node* newnode(char x) {
    struct node* new1 = (struct node*)malloc(sizeof(struct node));
    new1->x = x;
    new1->next = NULL;
    return new1;
}

and i Initialized top=NULL;我初始化顶部=NULL;

ex) push(top, a)例如)推(顶部,一)

void push(struct node* h, char c) {
    struct node* new1 = newnode(c);

    if (count(h) > N) {
        printf("Stack FULL\n");
        return;
    }
    new1->next = h;
    h= new1;

}

Then shouldn't the address value of top be the address value of new1 after the push function?那么top的地址值不应该是push function之后new1的地址值吗?

Even after the push function ends, the top remains NULL, so I don't know what to do.即使在push function结束后,顶部仍然是NULL,所以我不知道该怎么办。

Please give me some advice!请给我一些建议!

You are passing the value in top , so the function cannot change what is in top .您在top中传递,因此 function 无法更改top中的内容。 You can either pass a pointer to top , or have the function return the new value you want for top so that the caller can assign it to top .您可以将指针传递给top ,或者让 function 返回您想要的top新值,以便调用者可以将其分配给top

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM