为什么用动态链表构建的堆栈在实现中缀表达式转后缀表达式时符号不输出?(语言-c语言)

为啥输出不了符号
问题相关代码,请勿粘贴截图
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define maxsize 999
 typedef struct SNode {
    char string;
    struct SNode *next;
} stack;
stack *creakstackhead();
void push(stack* now, char string);
char pop(stack* now);
int check(stack* now);
int main()
{
    stack *head = creakstackhead();
    stack *now=head;
    char userinput[maxsize];
    
    gets(userinput);
    for (int i = 0; i <= strlen(userinput)-1; i++)
    {
        if (userinput[i] >= '0' && userinput[i] <= '9')
            printf("%c", userinput[i]);
        else if (!check(now))
            push(now, userinput[i]);
        else if (userinput[i] < now->string)
            push(now,userinput[i]);    
        else
        {
            printf("%c", pop(now));
            i--;
        }
        //printf("%c", now->string);
    }
    while(check(now))
        printf("%c", pop(now));
    return 0;
}

stack* creakstackhead()
{
    stack* s = (stack*)malloc(sizeof(stack));
    s->next = NULL;
    return s;
}

void push(stack* now,char string)
{
        stack* s = (stack*)malloc(sizeof(stack));
        s->next = now;
        s->string = string;
        now = s;
}

char pop(stack* now)
{
    char result;
    stack* s = now;
    result = now->string;
    now = now->next;
    free(s);
    return result;
}
int check(stack* now)
{
    return (now->next);
}


img

我看了一下你的push函数应该是有问题的,你要知道函数的参数传递进行的是值传递,你看,你的push函数在进行操作时首先把主函数中的now指针里存放的地址赋值给你push函数的now指针,然后在你进行完一系列操作之后你让now里面存放的地址变成了s的地址,这个很好,,逻辑上没有任何问题,但是实际操作是有问题的,因为你现在的now只是push函数里面的一个形参,当push函数结束的时候它会被释放,所以实际上你主函数里面的now指针并没有改变,修改方法就是将s作为函数的返回值传递给主函数里的now代码如下

stack* push(stack* now,char string)
{
        stack* s = (stack*)malloc(sizeof(stack));
        s->next = now;
        s->string = string;
        return s;
}
 
主函数里
 now = push(now, userinput[i]);

我查了一下ASCII码表

img

可以看到加减乘除所对应的ASCII码值不能通过简单的比大小来判断是否可以出栈,这里建议你再思考一下

现在我们假设就按你的逻辑进行操作,我们就假设优先级高的运算符的ASCII码值小,然后你的代码逻辑就没问题了,我们只需要判断大小就可以构建后缀表达式,但我个人觉得你的问题还是主要是出在你主函数for循环里最后一个else里面,首先我不知道为什么要 i--,在一个就是比方说我们的表达式为5-3*4-2, 那么三个符号分别为 - * - ,当 - 和 * 入栈后,这时我们碰到最后一个 - ,那如果你只是进行星号的出栈操作,那等于说你就漏掉了最后一个减号,所以这里逻辑也有点问题,当然在你写程序的时候主函数的变量名和你定义的函数的变量名尽量区别开来,我看了你的代码很多地方都是你把主函数的now变量传到你定义的函数里面之后就忘了她只是一个临时变量在函数结束后变量会被释放这个问题,你会误以为主函数里面的变量已经修改过了,导致你对出错地方的判断失误
修改好的代码如下,供你参考(没有实现全部功能,只是在你的基础上进行了修改)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define maxsize 999
typedef struct SNode {
    char string;
    struct SNode *next;
} stack;
stack *creakstackhead();
stack* push(stack* now, char string);
stack* pop(stack* now, char* result);
int check(stack* now);
int main()
{
    stack *head = creakstackhead();
    stack *now=head;
    char result;
    char userinput[maxsize];
    
    gets(userinput);
    for (int i = 0; i <= strlen(userinput)-1; i++)
    {
        if (userinput[i] >= '0' && userinput[i] <= '9')
            printf("%c", userinput[i]);
        else if (!check(now))
            now = push(now, userinput[i]);
        else if (userinput[i] < now->string)
            now = push(now,userinput[i]);    
        else
        {
            now = pop(now, &result);
            printf("%c", result);
            now = push(now,userinput[i]);
        }
        //printf("%c", now->string);
    }
    while(check(now))
    {
        now = pop(now, &result);
        printf("%c", result);
    }
        
    return 0;
}
 
stack* creakstackhead()
{
    stack* s = (stack*)malloc(sizeof(stack));
    s->next = NULL;
    return s;
}
 
stack* push(stack* now,char string)
{
        stack* s = (stack*)malloc(sizeof(stack));
        s->next = now;
        s->string = string;
        return s;
}
 
stack* pop(stack* now, char* result)
{
    stack* s = now;
    *result = now->string;
    now = now->next;
    free(s);
    return now;
}
int check(stack* now)
{
    if(now->next)
    {
        return 1;
    }
    return 0;
}

结果如下

img