#include<stdio.h>
#include<stdlib.h>
typedef struct cnode{//孩子结点
int child;
struct cnode*next;
}cnode;
typedef struct pnode{//双亲结点
char data;
struct cnode*firstchild;
}pnode;
typedef struct tree{
pnode tn[100];
int n;//结点数
}tree;
void creat(tree&t){
printf("please input 结点数:");
scanf("%d",&t.n);
for(int i=0;i<t.n;i++){
printf("input 各结点值:");
scanf("%c",&t.tn[i].data);
int w=getchar();//吸收空格
t.tn[i].firstchild=NULL;
}
for(int i=0;i<t.n;i++){
int j;
printf("please input %c 的结点的孩子数与位置:");
scanf("%d",&j);
cnode *p=NULL;
cnode *q=NULL;
for(int b=0;b<j;b++){
q=(cnode*)malloc(sizeof(cnode));
scanf("%d",&(q->child));
q->next=NULL;
if(b==0)t.tn[i].firstchild=q;
else p->next=q;
p=q;
}
}
}
void printchild(cnode *t){
cnode*p=t;
while(p){
printf("%d->",p->child);
p=p->next;
}
printf("NULL");
}
void print(tree &t){
printf("print 结点与孩子\n");
for(int i=0;i<t.n;i++){
printf("%d %c ",i,t.tn[i].data);
printchild(t.tn[i].firstchild);
printf("\n");
}
}
int main(){
tree t;
creat(t);
print(t);
}
c语言数据结构---树的孩子链表表示法
最新推荐文章于 2024-07-08 21:10:38 发布