C++数据结构求助

来源:百度知道 编辑:UC知道 时间:2024/06/25 20:08:20
typedef char ElemType;
typedef struct node
{
ElemType data;
struct node *lchild;
struct node *rchild;
}BTNode;
//////这个是头文件

#define MaxSize 100
#include"btree.h"
#include <iostream.h>
#include<stdio.h>
void CreateBTNode(BTNode * &b,char * str)
{
BTNode *St[MaxSize],*p=NULL;
int top=-1,k,j=0;
char ch;
b=NULL;
ch=str[j];
while (ch!='0')
{
switch(ch)
{
case '(':top++;St[top]=p;k=1;break;
case ')':top--;break;
case ',':k=2;break;
default :p=new BTNode;
p->data=ch;p->lchild=p->rchild=NULL;
if(b==NULL)
b=p;
else
{
switch(k)
{
case 1:St[top]->lchild=p;break;
case 2:St[top]->rchild=p;break;
}
}
}
j++;
ch=str[j];
}
}
////////////////////////

ElemType *e;
cout<<"input tree:"<<endl;
cin>>e;

ElemType *e;换成ElemType e[50];这样输入广义表就没问题了

楼上是一种方式,主要就是在定义char*的时候要记得申请空间,还可以ElemType *e=new char[1024];
或是如楼上,直接定义成数组。。。