为什么我的节点上下文不打印任何内容?



我正在尝试创建一个保持算术运算顺序的计算器。我的想法是将中缀表示法转换为后缀表示法,以便我可以从左到右解决它,而不必担心括号。在尝试将中缀转换为后缀表示法之前,我想解决一个后缀表示法练习,我尝试使用节点来解决这个问题,但是我在将数字和运算符划分为一个节点时遇到了问题。我对指针和结构很陌生,所有的事情都让我感到困惑。

这是尝试划分它的函数:

typedef char* String;
typedef struct node
{
    String       str;
    struct node *next;
} Node;
Node *rpn_divider(String equation, int eq_size)
{
     Node *rpn_parts = node_alloc(1); //pointer to first element in the node
     Node *part_temp = rpn_parts; //pointer to the lattest element in the node
     String temp = malloc(sizeof(char*) * NUM_SIZE);
     int i, j; //i = string equation index, j = string temp index
     for (i = 0, j = 0; i < eq_size; i++)
     {
         if (isNum(equation[i]))
            temp[j++] = equation[i];
         else if (isOper(equation[i]))
         {
              temp[0] = equation[i];
              temp[1] = '';
              next_node(part_temp, temp);
         }
         else
         {
              if (temp == '') continue;
              temp[j] = '';
              next_node(part_temp, temp);
              j = 0;
         }
     }
     free(part_temp->next);
     free(temp);
     return rpn_parts;
}

这是next_node函数:

void next_node(Node *node, String str)
{
  node->str = str;
     node->next = node_alloc(1);
     node = node->next;
     free(str);
     str =  malloc(sizeof(char*) * NUM_SIZE);
     str[0] = '';
}

当我尝试打印节点上下文时,它不执行任何操作:

Node *ptr;
for (ptr = head; ptr != NULL; ptr = ptr->next);
{
    printf("The Str = %s", ptr->str);
}

next_node 函数中,您正在分配内存并将其分配给 str 的本地副本。这是一个内存泄漏,调用方将永远不会看到 str 的新值。相反,您可以这样做:

void next_node(Node *node, String *str)
{
     node->str = *str;
     node->next = node_alloc(1);
     node = node->next;
     free(*str);
     *str =  malloc(sizeof(char*) * NUM_SIZE);
     (*str)[0] = '';
}

并像这样使用它:

next_node(part_temp, &temp);

你犯了一个大错误。
你在 for 循环后放了一个分号,就像这样

for (ptr = head; ptr != NULL; ptr = ptr->next);

应该是这样的

for (ptr = head; ptr != NULL; ptr = ptr->next)

也许这有帮助。

好吧,这很奇怪,如果我输入字符串,它可以工作:

String rpn_equation = "2 3 5 + 6 2 + 5 * + *";
即使是 2 位数字或

4 位数字,但如果我输入 3 位或 5 位数字,它划分错误,我不明白为什么:

Node *rpn_divider(String equation, int eq_size)
{
     Node *head = node_alloc(1); //pointer to first element in the node
     Node *part_temp = head; //pointer to the lattest element in the node
     String temp = malloc(sizeof(char*) * NUM_SIZE);
     int i, j = 0; //i = string equation index, j = string temp index
     for (i = 0; i < eq_size; i++)
     {
         if (isNum(equation[i]))
            temp[j++] = equation[i];
         else if (isOper(equation[i]))
         {
              temp[0] = equation[i];
              temp[1] = '';
              part_temp->str = temp;
              part_temp->next = node_alloc(1);
              part_temp = part_temp->next;
              temp =  malloc(sizeof(char*) * NUM_SIZE);
              temp[0] = '';
         }
         else
         {
              if (temp[j] == '') continue;
              temp[j] = '';
              part_temp->str = temp;
              part_temp->next = node_alloc(1);
              part_temp = part_temp->next;
              temp =  malloc(sizeof(char*) * NUM_SIZE);
              temp[0] = '';
              j = 0;
         }
     }
     free(part_temp);         
     return head;
}

我删除了node_next函数,因为它不起作用。

最新更新