标题:[求助]哪位大侠给改一下错
只看楼主
feishu57
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2005-12-30
 问题点数:0 回复次数:0 
[求助]哪位大侠给改一下错

请哪位大侠给看看,这个建立二叉树,前序,中序,后序遍历二叉树的程序编译时提示"possible use of "p" before definition in function creat",应该怎样改才行啊?
#include <stdio.h>
#include<malloc.h>
#define MaxSize 100
typedef char ElemType;
typedef struct node
{
ElemType data;
struct node *left,*right;
} BTree;

void creatree(BTree **BT,char *str)
{
BTree *stack[MaxSize],*p;
int top=-1,k,j=0;/*top为栈指针,k指定是左还是右孩子,j为str指针*/
char ch;
*BT=NULL;
ch=str[j];
while (ch!='\0')
{
switch(ch)
{
case '(':top++;stack[top]=p;k=1; /*为左结点*/
break;
case ')':top--;
break;
case ',':k=2; /*为右结点*/
break;
default: p=(BTree *)malloc(sizeof(BTree));
p->data=ch;p->left=p->right=NULL;
if (*BT==NULL) /*根结点*/
*BT=p;
else
{
switch(k)
{
case 1:stack[top]->left=p;
break;
case 2:stack[top]->right=p;
}
}
}
j++;
ch=str[j];
}
}
void preorder(BTree *BT)
{
if (BT!=NULL)
{
printf("%c",BT->data);
preorder(BT->left);
preorder(BT->right);
}
}
void inorder(BTree *BT)
{
if (BT!=NULL)
{
inorder(BT->left);
printf("%c",BT->data);
inorder(BT->right);
}
}
void postorder(BTree *B)
{
if (B!=NULL)
{
postorder(B->left);
postorder(B->right);
printf("%c",B->data);
}
}

main()
{
BTree *B;
char *s="A(B(D,E(H,I)),C(G))";
creatree(&B,s);
printf("\n前序遍历:");
preorder(B);
printf("\n中序遍历:");
inorder(B);
printf("\n后序遍历:");
postorder(B);
}

搜索更多相关主题的帖子: definition function possible include before 
2005-12-30 18:27



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-40990-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.271066 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved