#include<stdio.h>
#include<stdlib.h>
typedef struct child {
int place;
struct child* next;
} child;
typedef struct node {
char data;
int parent;
child* child;
} node;
typedef struct tree {
node a[100];
} tree;
tree cretree(int n) {
tree tree1;
for (int i = 0; i < n; i++) {
scanf("%c %d", &tree1.a[i].data, &tree1.a[i].parent);
tree1.a[i].child = NULL;
}//输入部分就到这里了,这里好像不能输入n次,麻烦看看,谢谢
for (int i = 0; i < n; i++) {
if (tree1.a[i].parent != -1) {
int q = tree1.a[i].parent;
child* child1 = (child*)malloc(sizeof(child));
child1->place = i;
child1->next = NULL;
if (tree1.a[q].child == NULL) {
tree1.a[q].child = child1;
}
else {
child* p = tree1.a[q].child;
while (p->next != NULL) {
p = p->next;
}
p->next = child1;
}
}
}
return tree1;
}
int main() {
tree tree1=cretree(9);
return 0;
}
/*
a -1
b 2
c 3
e 2
f 0
g 0
输入样例*/
第18行 --- 第21行:
for (int i = 0; i < n; i++) {
getchar();
scanf("%c %d", &tree1.a[i].data, &tree1.a[i].parent);
tree1.a[i].child = NULL;
}//输入部分就到这里了,这里好像不能输入n次,麻烦看看,谢谢