C 无法将元素推到堆栈上

C 无法将元素推到堆栈上,c,C,我正在做一个项目,在这个项目中,我必须用反向波兰符号表示一个表达式,将整数和运算符推到堆栈上,然后在插入二叉搜索树时将它们从堆栈中弹出 #include <stdlib.h> #include <ctype.h> #include <stdio.h> #include <string.h> struct snode { int datum; struct snode* bottom; }; struct tnode { int d

我正在做一个项目,在这个项目中,我必须用反向波兰符号表示一个表达式,将整数和运算符推到堆栈上,然后在插入二叉搜索树时将它们从堆栈中弹出

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

struct snode 
{
  int datum;
  struct snode* bottom;
};

struct tnode
{
  int datum;
  struct tnode* left;
  struct tnode*right;
};

struct snode* 
push(struct snode* stack, int x) {
  struct snode *S = (struct snode*)malloc(sizeof(struct snode));
  S->datum = x;
  S->bottom = stack;
  return S;
}

struct snode* pop(struct snode* stack) {
  struct snode *S;
  if (stack == NULL)
    return NULL;
  S = stack->bottom;
  free(stack);
  return S;
}

int
peek(struct snode* stack){
  return stack->datum;
}


struct tnode*
create_node(int x){
  struct tnode* tmp;
  tmp = (struct tnode*)malloc(sizeof(struct tnode));
  tmp->datum = x;
  tmp->right = NULL;
  tmp->left = NULL;
  return tmp;
}

void
print_table(struct tnode *AST){
  if(AST !=NULL){
    print_table(AST->left);
    printf("%d ", AST->datum);
    print_table(AST->right);
  }
}

struct tnode*
build_tree(struct snode *S)
{
  struct tnode* root;
  if (S==NULL){
    return NULL;
  }else{
    int top = peek(S);
    if (top == 65 || top == 83 || top == 88 || top == 68 || top == 77){
      return create_node(top);
    }else{
      root= create_node(top);
      root->right = build_tree(pop(S));
      root->left = build_tree(pop(S));
      return root;
    }
  }
}

int
main(int argc, const char *argv[])
{

  int i = 1;
  struct tnode *tree = NULL;
  struct snode *stack = NULL;


  while (argv[i]!= NULL){
    stack = push(stack, argv[i][0]);
    i++;
  }

 tree =  build_tree(stack);
 print_table(tree);

 return EXIT_SUCCESS;
}
结果是

135208 135224 135208 135240 135208 135224 135208 0 135208 135224 135208 52 0 53 0 
应该是什么时候

4 65 5
我认为这是因为我是如何将元素推到堆栈上的

编辑:

我初始化I,使I=1

这就是我得到的结果

134480 134496 134480 0 5 4 5 
编辑2:

我决定去掉atol(argv[I]),把它改为argv[I][0]。 参见代码

现在我的输出只是

65

终于弄明白发生了什么事


下面是新修改的部分,这些部分应该可以使代码正常工作(请查看
//使用前没有刷新内存,这是一种不好的做法。这是C,所以不要强制转换
malloc()的返回。C++中需要的,但是在C中。如果你认为问题是在将元素推到堆栈上的方式,那么删除所有处理树的代码并检查堆栈。个人认为,在代码> BugdIdTrase<代码>中有一个问题。我认为<代码> ATOL(“A”)
对于最后一个arg
A
没有做您认为它做的事情。似乎您被分配了与相同的任务-构建树是您的问题,而不是堆栈实现!事实上argv数组已经形成了某种堆栈,因此在堆栈上从那里移动值是毫无用处的。请尝试将堆栈上语法树的节点按入请不要调用树,因为你正在构建一个搜索树。非常感谢。我之所以如此困惑,是因为在今天的课堂上,我的导师给了一个用伪代码构建树函数的课堂。我认为我重写的是正确的,但我想我没有。这就是为什么我认为这是我推到堆栈上的方式
65
struct tnode*
build_tree(struct snode *S)
{

  struct tnode* root;
  if (S == NULL)
    return NULL;

  int top = peek(S);

  if (top == 65 || top == 83 || top == 88 || top == 68 || top == 77)
  {
     root = create_node(top);
     S = pop(S); // <== EDIT
     root->right = build_tree(S);


     S = pop(S); // <== EDIT
     root->left = build_tree(S);
     return root;
  } 

  root= create_node(top);

  return root;
}

int
main(int argc, const char *argv[])
{

  int i = 1;
  struct tnode *tree = NULL;
  struct snode *stack = NULL;

  int value = 0;
  while (argv[i]!= NULL)
  {
    if ((value = atoi(argv[i])) == 0) // <== EDIT
        value = argv[i][0];
    stack = push(stack, value);
    i++;
  }


 tree =  build_tree(stack);
 print_table(tree);

 return EXIT_SUCCESS;
}
    if ((value = atoi(argv[i])) == 0) // <== EDIT
        value = argv[i][0];
    stack = push(stack, value);
  A
 / \
4   5
  int top = peek(S);

  if (top == 65 || top == 83 || top == 88 || top == 68 || top == 77)
  {
     root = create_node(top);
     S = pop(S); // <== EDIT
     root->right = build_tree(S);


     S = pop(S); // <== EDIT
     root->left = build_tree(S);
     return root;
  } 

  root= create_node(top);

  return root;
     S = pop(S); // <== EDIT
     root->right = build_tree(S);


     S = pop(S); // <== EDIT
     root->left = build_tree(S);