删除值为x的叶子结点

来源:百度知道 编辑:UC知道 时间:2024/06/17 17:24:52
基本代码如下:
#include "stdafx.h"
#include <iostream.h>
#define max 100

//链表
typedef struct ctnode
{
int child;
ctnode *next;
}ctnode;

//双亲结点
typedef struct
{
char data;
ctnode *firstchild;
}ctbox;

//数组
typedef struct
{
ctbox nodes[max];
int n;//结点数
}ctree;
//双亲结点下标
int locate(ctree t,char x)
{
for(int i=0;i<t.n;i++)
{if(x=t.nodes[i].data)
return i;}
return -1;
}

//创建树t 有n个
void create(ctree &t,int n)
{
t.n=n;
int i;
for(i=0;i<t.n;i++)
{
cin>>t.nodes[i].data;
t.nodes[i].firstchild=NULL;//设置默认值
}
char x,y; int j,k;
ctnode *p;
for(i=0;i<t.n;i++)
{
cin>>x;
j=locate(t,x);
cin>>y;
while(y!='.')
{
k=locate(t,y);
p=new ctnode;
p->child=k;<

这个数据结构是不是安排的有问题?
直接一个节点上带next和firstchild更好吧?
typedef struct ctnode
{
char data;
ctnode *next;
ctnode *firstchild;
}ctnode;
或者直接用索引也行
typedef struct ctnode
{
char data;
int next;
int firstchild;
}ctnode;