注册 登录
编程论坛 C图形专区

有关栈与树的问题,郁闷很久了~大家都来看看啊谢了~

wycwyc 发布于 2008-11-21 16:20, 814 次点击
就是用栈储存树,再调用函数求树的根节点

#include<stdio.h>
#include"malloc.h"

typedef struct node
{
    char data;
    struct node *lchild;
    struct node *rchild;
}shu;

typedef struct
{ shu * qu[100];
  int top;
} SeqStack;

SeqStack* make()
{
 SeqStack *w;
 w=(SeqStack*)malloc(sizeof(SeqStack));
 w->top=1;
 return w;
}

SeqStack * past()
{
    char ch;
    SeqStack *w;
    int j=0,n=0;
    shu *root=NULL,*s=NULL;
    ch=getchar();
    while(ch!='\n')
    {
        if(ch=='@')w->qu[w->top]=NULL;
        else if(w->top>1&&w->qu[w->top/2]==NULL)//我调试时程序走到这里就走不下去了,提示undandled exceptionin wycwyc.exe:OxCOOOOOO5:Violation 是说的什么意思 帮忙解释下谢了//        {ch=getchar();continue;}
        else
        {
            s=(shu*)malloc(sizeof(shu));
            s->data=ch;s->lchild=NULL;s->rchild=NULL;
            w->qu[w->top]=s;
            if(w->top==1)root=s;
            else if(w->top%2==0) w->qu[w->top/2]->lchild=s;
            else w->qu[w->top/2]->rchild=s;
            n++;
        }printf("iiii");
        w->top++;
        ch=getchar();
    }printf("%d",w->qu[w->top]);
    return (w);
}

main()
{
    SeqStack *W;
    W=make();
    W=past();
    printf("%d",W->qu[W->top]);
}
0 回复
1