D:\C数据结构\collect2.exe [Error] ld returned 1 exit status
#include<stdio.h>#include<stdlib.h>
#define QueueMaxSize 20
#define StackMaxSize 10
typedef char ElemType;
struct BTreeNode{
ElemType data;
struct BTreeNode*left;
struct BTreeNode*right;
};
//初始化二叉树
void InitBTree(struct BTreeNode **BT)
{
*BT=NULL;
}
//建立二叉树
void CreateBTree(struct BTreeNode** BT,char *a)
{
struct BTreeNode*p;
struct BTreeNode* s[StackMaxSize];
int top=-1;
int k;
int i=0;
*BT=NULL;
while(a[i]){
switch(a[i]){
case ' ':
break;
case '(':
if(top==StackMaxSize-1){
printf("栈空间太小,需增加StackMaxSize!\n");
exit(1);
}
top++;s[top]=p;k=1;
break;
case ')':
if(top==-1){
printf("二叉树广义表字符串错!\n");
exit(1);
}
top--;break;
case ',':
k=2;break;
default:
p=malloc(sizeof(struct BTreeNode));
p->data=a[i];p->left=p->right=NULL;
if(*BT==NULL) *BT=p;
else{
if(k==1) s[top]->left=p;
else s[top]->right=p;
}
}
i++;
}
}
//检查二叉树是否为空
int BTreeEmpty(struct BTreeNode *BT)
{
if(BT==NULL) return 1;
else return 0;
}
//求深度
int BTreeDepth(struct BTreeNode *BT)
{
if(BT==NULL) return 0;
else{
int dep1=BTreeDepth(BT->left);
int dep2=BTreeDepth(BT->right);
if(dep1>dep2)
return dep1+1;
else
return dep2+1;
}
}
//查找结点
ElemType *FindBTree(struct BTreeNode*BT,ElemType x)
{
if(BT==NULL) return NULL;
else{
ElemType *p;
if(p=FindBTree(BT->left,x)) return p;
if(p=FindBTree(BT->right,x)) return p;
return NULL;
}
}
//输出二叉树
void PrintBTree(struct BTreeNode*BT)
{
if(BT!=NULL){
printf("%c",BT->data);
if(BT->left!=NULL || BT->right!=NULL){
printf("(");
PrintBTree(BT->left);
if(BT->right!=NULL) printf(",");
PrintBTree(BT->right);
printf(")");
}
}
}
//清空
void ClearBTree(struct BTreeNode**BT)
{
if(*BT==NULL){
ClearBTree(&((*BT)->left));
ClearBTree(&((*BT)->right));
free(*BT);
*BT=NULL;
}
}
//前序
void Preorder(struct BTreeNode* BT)
{
if(BT!=NULL){
printf("%c ",BT->data);
Preorder(BT->left);
Preorder(BT->right);
}
}
//中序
void Inorder(struct BTreeNode* BT)
{
if(BT!=NULL){
Inorder(BT->left);
printf("%c ",BT->data);
Inorder(BT->right);
}
}
//后序
void Postorder(struct BTreeNode* BT)
{
if(BT!=NULL){
Postorder(BT->left);
Postorder(BT->right);
printf("%c ",BT->data);
}
}
//按层
void Levelorder(struct BTreeNode* BT)
{
struct BTreeNode *p;
struct BTreeNode *q[QueueMaxSize];
int front=0,rear=0;
if(BT!=NULL){
rear=(rear+1)%QueueMaxSize;
q[rear]=BT;
}
while(front!=rear){
front=(front+1)%QueueMaxSize;
p=q[front];
ptintf("%c ",p->data);
if(p->left!=NULL){
rear=(rear+1)%QueueMaxSize;
q[rear]=p->left;
}
if(p->right!=NULL){
rear=(rear+1)%QueueMaxSize;
q[rear]=p->right;
}
}
}
void main()
{
struct BTreeNode *bt;
char b[50];
ElemType x,*px;
InitBTree(&bt);
printf("输入二叉树广义表字符串:\n");
scanf("%s",b);
CreateBTree(&bt,b);
PrintBTree(bt);
printf("\n");
printf("前序:");
Preorder(bt);printf("\n");
printf("中序:");
Inorder(bt);printf("\n");
printf("后序:");
Postorder(bt);printf("\n");
printf("按层:");
Levelorder(bt);printf("\n");
printf("输入一个待查的字符:");
scanf(" %c",&x);
px=FindBTree(bt,x);
if(px) printf("查找成功:%\n",*px);
else printf("查找失败\n");
printf("二叉树的深度为: ");
printf("%d\n",BTreeDepth(bt));
ClearBTree(&bt);
}
为什么总是出现题目中的问题?是用Devc编的。