【发布时间】:2013-08-22 06:20:31
【问题描述】:
代码:
#include<stdio.h>
#include<malloc.h>
typedef struct tree
{
char data;
struct tree *left;
struct tree *right;
}*pos;
pos stack[30];
int top=-1;
pos newnode(char b)
{
pos temp;
temp=(struct tree*)malloc(sizeof(struct tree));
temp->data=b;
temp->left=NULL;
temp->right=NULL;
return(temp);
}
void push(pos temp)
{
stack[++top]=temp;
}
pos pop()
{
pos p;
p=stack[top--];
return(p);
}
void inorder(pos t)
{
if(t!=NULL)
{
inorder(t->left);
printf("%s",t->data);
inorder(t->right);
}
}
void preorder(pos t)
{
if(t!=NULL)
{
printf("%s",t->data);
preorder(t->left);
inorder(t->right);
}
}
void postorder(pos t)
{
if(t!=NULL)
{
postorder(t->left);
postorder(t->right);
printf("%s",t->data);
}
}
void main()
{
char *a;
pos temp,t;
int j,i;
puts("Enter the expression :");
scanf("%s",&a);
for(i=0;a[i]!='\0';i++)
{
if(a[i]=='*' || a[i]=='/' || a[i]=='+' || a[i]=='-')
{
temp=newnode(a[i]);
temp->right=pop();
temp->left=pop();
push(temp);
}
else
{
temp=newnode(a[i]);
push(temp);
}
}
inorder(temp);
printf("\n");
preorder(temp);
printf("\n");
postorder(temp);
}
错误:分段错误
此代码用于构造二叉树遍历和后缀到中缀和前缀的转换。我不知道哪里出了问题,但它一直在说同样的错误。 谁能帮我解决这个问题?
【问题讨论】:
-
您是否尝试过使用调试器?
-
标签 [dsa] 在这里做什么?