标题:哪位高手帮我看看,这个程序运行怎出错!在bitree_node *top_stack(stack * ...
只看楼主
cataler
Rank: 1
等 级:新手上路
帖 子:6
专家分:3
注 册:2010-10-22
结帖率:100%
已结贴  问题点数:10 回复次数:2 
哪位高手帮我看看,这个程序运行怎出错!在bitree_node *top_stack(stack *h){return h->top->tree_p
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
/*树结构类型 */
typedef struct node
{
        int elem;
        struct node *lchild,*rchild;
}bitree_node;
/*栈结构类型*/
typedef struct stacknode
{
        bitree_node * tree_point;
        struct stacknode * next;
}stacknode;
typedef struct
{
        stacknode * top;
}stack;
stack * h;
/*栈的数据结构*/
void creat_stack(stack *h)
{
     h->top=NULL;
}
void push_stack(stack *h,bitree_node *e)
{
     stacknode * p;
     p=(stacknode *)malloc(sizeof(stacknode));
     p->tree_point=e;
     p->next=h->top;
     h->top=p;
}
void pop_stack(stack *h)
{
    stacknode *p;
    p=h->top;
    h->top=p->next;
    free(p);
}
bitree_node *top_stack(stack *h)
{
    return h->top->tree_point;
}
int stack_empty(stack *h)
{
    if(!h) return 0;
    else return 1;
}
/*树的数据结构*/
void creat_tree(bitree_node * bit)
{
    int rear=0,root,data;
    printf("请输入根结点\n");
    scanf("%d",&root);
    bit->elem=root;
    push(h,bit);
    bitree_node *p;
    while(1)
    {
            printf("请输入左孩子\n");
            scanf("%d",&data);
            if(data!=999)
            {
                   bit=(bitree_node *)malloc(sizeof(bitree_node));
                   bit->elem=data;
                   push_stack(h,bit);
                   bit=bit->lchild;
            }
            else
            {
          a:    p=top_stack(h);
                if(p->lchild!=NULL&&p->rchild!=NULL)
                {
                    pop_stack(h);
                    goto a;
                }
                p=p->rchild;
                printf("请输入右孩子\n");
                scanf("%d",&data);
                if(data==999)
                {
                    pop_stack(h);
                    goto a;
                }
                p->elem=data;
                push_stack(h,p);
                if(stack_empty(h)) break;
            }
    }
}
void main()
{
     h=(stack *)malloc(sizeof(stack));
     creat_stack(h);
     bitree_node * bit;
     bit=(bitree_node *)malloc(sizeof(bitree_node));
     creat_tree(bit);
}
搜索更多相关主题的帖子: stack node tree top return 
2010-10-29 16:35
NoSoul
Rank: 9Rank: 9Rank: 9
来 自:沈阳化工大学
等 级:蜘蛛侠
帖 子:283
专家分:1010
注 册:2009-6-6
得分:10 
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
/*树结构类型 */
typedef struct node
{
    int elem;
    struct node *lchild,*rchild;
} bitree_node;
/*栈结构类型*/
typedef struct stacknode
{
    bitree_node * tree_point;
    struct stacknode * next;
} stacknode;
typedef struct
{
    stacknode * top;
} stack;
stack * h;
/*栈的数据结构*/
void creat_stack(stack *h)
{
    h->top=NULL;
}
void push_stack(stack *h,bitree_node *e)
{
    stacknode * p;
    p=(stacknode *)malloc(sizeof(stacknode));
    p->tree_point=e;
    p->next=h->top;
    h->top=p;
}
void pop_stack(stack *h)
{
    stacknode *p;
    p=h->top;
    h->top=p->next;
    free(p);
}
bitree_node *top_stack(stack *h)
{
    return h->top->tree_point;
}
int stack_empty(stack *h)
{
    if(!h) return 0;
    else return 1;
}
/*树的数据结构*/
void creat_tree(bitree_node * bit)
{
    int rear=0,root,data;
    printf("请输入根结点\n");
    scanf("%d",&root);
    bit->elem=root;
    push_stack(h,bit);//你的是push(h,bit);
    bitree_node *p;
    while(1)
    {
        printf("请输入左孩子\n");
        scanf("%d",&data);
        if(data!=999)
        {
            bit=(bitree_node *)malloc(sizeof(bitree_node));
            bit->elem=data;
            push_stack(h,bit);
            bit=bit->lchild;
        }
        else
        {
        a:
            p=top_stack(h);
            if(p->lchild!=NULL&&p->rchild!=NULL)
            {
                pop_stack(h);
                goto a;
            }
            p=p->rchild;
            printf("请输入右孩子\n");
            scanf("%d",&data);
            if(data==999)
            {
                pop_stack(h);
                goto a;
            }
            p->elem=data;
            push_stack(h,p);
            if(stack_empty(h)) break;
        }
    }
}
int main()
{
    h=(stack *)malloc(sizeof(stack));
    creat_stack(h);
    bitree_node * bit;
    bit=(bitree_node *)malloc(sizeof(bitree_node));
    creat_tree(bit);
    return 0;
}

我想伸手拉近點,竟觸不到那邊,就欠一點點,但這一點點...卻好遠
2010-10-29 16:43
cataler
Rank: 1
等 级:新手上路
帖 子:6
专家分:3
注 册:2010-10-22
得分:0 
回复 2楼 NoSoul
不是 那个问题 你说的那个我后来加的 忘改了!是那top_stack函数在调试时出现错误
2010-10-29 21:45



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




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

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