scanf
を使用してリンクリストのcharポインタに複数回入力しようとしています。新しい入力を入力するたびにすべてのフィールドにname
の変更が加えられます。ここリンクリストcharポインタscanf入力
は私のリンクリストである:ここ
struct node {
struct node *next;
int level;
char *name;
};
は私の主な機能です:
struct node *root = NULL;
while (1) {
char arrays[12];
char *n;
n = arrays;
int i = NULL;
printf("Enter level: ");
scanf("%i", &i);
printf("\nEnter name: ");
scanf("%s", arrays);
insert(&root, i, n, compare);
display(root);
}
挿入機能:基本的に
void insert(struct node **head, const int level, char *name, int(*cmp)(struct node *l, struct node *r))
{
struct node *new;
new = malloc(sizeof *new);
new->level = level;
new->name = name;
/* Find the insertion point */
for (; *head != NULL; head = &(*head)->next)
{
if ((*head)->level > level || (*head)->level == level && cmp(*head, new) > 0) { break; }
}
new->next = *head;
*head = new;
}
I入力の場合:
input: | expected output: | actual output:
1 smith | 1 john | 1 alice
1 john | 1 smith | 1 alice
3 malek | 2 alice | 2 alice
2 alice | 3 malek | 3 alice
注:私はscanf
などせずに、手動でデータを入力していたときに期待通りの機能が働いている:
insert(&root, 1, "Abbas", compare);
insert(&root, 1, "Calbass", compare);
私は尋ねることは許してもらえますが、 'main()'メソッドの無限ループが終わることはありません。また、 'insert'関数のコードを表示してください。 –
挿入機能と表示機能のコードを提供しています.... – Rupsingh
@TimBiegeleisenあなたは正しいですが、無限ループはデバッグのためだけでした。 – Poorya