gpt4 book ai didi

c - 插入到链表中

转载 作者:行者123 更新时间:2023-11-30 16:59:55 26 4
gpt4 key购买 nike

大家好,我制作了一个程序,它插入 3 个结构体链表(链表),插入名称、路径和持续时间,如代码中所示,但是当我运行该项目时,它卡在了这一行:fgets(n3->frame->name, 19, stdin);我该如何解决这个问题?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Frame
{
char *name;
unsigned int duration;
char *path; // may change to FILE*
}*p , *p1,*p2;

typedef struct Frame frame_t;

struct Link
{
frame_t *frame;
struct Link *next;
}*n , *n1 ,*n2;

typedef struct Link link_t;

struct node *insert(struct node *n3);

int main()
{

printf("1\n");
insert(n);


printf("1\n");

}

struct node *insert(struct Link *n3)
{
while (n3 != NULL)
{
n3 = n3->next;

}
if (n3 == NULL)
{


n3 = (struct node *)malloc(sizeof(struct Frame)); //gives memory to the node
fgets(n3->frame->name, 19, stdin);
n3 = (struct node *)malloc(sizeof(struct Frame)); //gives memory to the node
fgets(n3->frame->path, 100, stdin);
n3 = (struct node *)malloc(sizeof(struct Frame)); //gives memory to the node
scanf("%d",&n3->frame->duration);
}


}

最佳答案

一些documentation对于 fgets 说:

This function reads as much of a line from a file as possible, stopping when the buffer is full (maxlength-1 characters), an end-of-line is detected, or EOF or an error is detected. It then stores a NULL to terminate the string.

上面的代码无法编译(缺少一些包含和 typedef?),因此我无法自己执行它,但是您是否在执行诸如确保在输入输入后按“enter”键之类的操作?

如果是的话,我相信您将会遇到一大堆其他问题。 another 中提供了如何使用 fgets 的一个示例。答案,上面的代码存在可疑之处,例如为类型 struct Frame 分配足够的内存,但将其转换为单独的类型并分配给第三个类型(struct Link)。如果使用 gcc 并且通常要注意警告,我建议将 -Wall 标志添加到编译步骤中。

关于c - 插入到链表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37912373/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com