#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
输入样例*/
请问为什么我这个程序只能输入四行而不是六行
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- qzjhjxj 2021-08-20 14:48关注
第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次,麻烦看看,谢谢
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
- ¥15 java写代码遇到问题,求帮助
- ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
- ¥15 有了解d3和topogram.js库的吗?有偿请教
- ¥100 任意维数的K均值聚类
- ¥15 stamps做sbas-insar,时序沉降图怎么画
- ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
- ¥15 关于#Java#的问题,如何解决?
- ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
- ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计