【问题标题】:Segmentation fault with a linked stack trying to evaluate postfix expressions链接堆栈尝试评估后缀表达式的分段错误
【发布时间】:2023-11-10 05:12:01
【问题描述】:

我正在尝试实现一个链接堆栈来评估修复后表达式,但是我不断遇到分段错误,不知道如何修复它。我是 C 编程的新手,这与 OOD 不同,所以欢迎任何建议。谢谢,请原谅我的 cmets 和间距。

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

struct node {
int data;
struct node *next;
};
 struct node *top = NULL;


void push(int data);
int pop();
int evaluatePostfix(char* exp);

void main() {
     char postfixExp[256];

     printf("What is your Postfix Expression? : \n");

     fgets(postfixExp, sizeof(postfixExp), stdin);
     printf("%d\n", evaluatePostfix(postfixExp));
}

//done
void push(int data) {
     struct node *newNode;
     newNode = (struct node*)malloc(sizeof(struct node));;
     (*newNode).data = data;
     (*newNode).next = NULL;
     //use isEmpty if can
     if (top != NULL) {
         (*newNode).next = top;
     }
     top = newNode;
}
//done
int pop() {
    struct node *temp = top;
    int *remove;
    //use isEmpty if can
    if (top != NULL) {
        *remove = (*top).data;
        top = (*top).next;
        free(temp);
    }
return *remove;

}

 int evaluatePostfix(char* exp) {
     struct node *top = NULL;
     for (int i = 0; i < sizeof(exp); i++) {
      if (isdigit(exp[i])) {
        push(exp[i]);
    } else {
        int val1 = pop();
        int val2 = pop();
        switch(exp[i]) {
            case '+':
                push(val2+val1);
                break;
            case '-':
                push(val2-val1);
                break;
            case '*':
                push(val2*val1);
                break;
            case '/':
                push(val2/val1);
                break;
        }
    }
}
return pop();

}

【问题讨论】:

  • (*newNode).data 是用newNode-&gt;data 加了语法糖的
  • *remove = (*top).data; BZZZ!取消引用无效指针 (remove)。
  • 应该是int result=0; result = top-&gt;data;return result;

标签: c linked-list stack postfix-notation


【解决方案1】:

我完全不明白你的错误价值的错误归因于许多因素

  1. @Eugene Sh 指出。 int remove把它的数据类型改成整数

  2. 您正在迭代的循环是表达式大小的两倍,因为您使用的是常量文字,您应该使用for (int i = 0; i &lt; strlen(exp)-1; i++) 减去1,因为\0

  3. 你在这里做的是

   if (isdigit(exp[i])) {
    push(exp[i]);
} else {
    int val1 = pop();
    int val2 = pop();

因为34+ 是一个表达式,所以您使用isdigit(exp[i]) 进行检查

isdigit(exp[i])只检查传入的字符是十进制数字字符。

480 的ascii 值减去(exp[i]) 以将十进制ascii 字符转换为整数

像这样修改你的代码

 int pop() {
struct node *temp = top;
int removed;
//use isEmpty if can
if (top != NULL) {
    removed = (*top).data;
    top = (*top).next;
    free(temp);
 }
 return removed;
}

2&3.

for (int i = 0; i < strlen(exp)-1; i++) {
  if (isdigit(exp[i])) {
    push(exp[i]-48);
} else {
    int val1 = pop();
    int val2 = pop();
printf("val1%d\n",val1);
printf("val2%d\n",val2);

您的工作代码 http://ideone.com/z58Phy

【讨论】:

    最近更新 更多