标题:D:\C数据结构\collect2.exe [Error] ld returned 1 exit status
取消只看楼主
九州
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2017-11-23
结帖率:0
已结贴  问题点数:20 回复次数:2 
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编的。
搜索更多相关主题的帖子: struct left NULL printf return 
2017-11-23 12:14
九州
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2017-11-23
得分:0 
回复 2楼 grmmylbs
谢谢啊,后来我换了格式,就把错误找出来了
2017-11-26 18:59
九州
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2017-11-23
得分:0 
#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 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;
                //printf("%c %d\n",s[top]->data,top);
                break;      
            case ')':
                if(top==-1){
                    printf("二叉树广义表字符串错!\n");
                    exit(1);
                }
                top--;//printf("%d\n",top);
                break;
            case ',':
                k=2;break;
            default:
                p=(BTreeNode*)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;
                    //printf("%c\n",s[top]->data);
                }
        }
        i++;
    }
    //for(int top=0;top<3;top++)
        //printf("%c %d\n",s[top]->right->data,top);
 }
 
//检查二叉树是否为空
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;
    }
 }
 
//初始化二叉树
void InitBTree(struct BTreeNode **BT)
{
    *BT=NULL;
}

//查找结点
ElemType* FindBTree(struct BTreeNode*BT,ElemType x)
{
    if(BT==NULL)    return NULL;
    else{
        if(BT->data==x)    return &(BT->data);
        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];
        printf("%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;
        }
    }
 }
 
int  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("查找成功:%c\n",*px);
    else    printf("查找失败\n");
    printf("二叉树的深度为: ");
    printf("%d\n",BTreeDepth(bt));
    ClearBTree(&bt);
    return 0;
}
这是正确的,后来出了那个printf之外,好像还改了别的。现在运行没错误了
2017-11-26 19:01



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




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

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