[英]How to add a structure to a linked list? in C
I'm trying to add a frame to a linked list of such structures, but instead of adding a new structure to the list each time, the program adds nothing, can anyone tell me what the problem is?我正在尝试将一个框架添加到此类结构的链表中,但不是每次都向列表中添加一个新结构,程序没有添加任何内容,谁能告诉我问题是什么? Or give me a better exercise method?
或者给我一个更好的锻炼方法?
The structures:结构:
typedef struct Frame
{
char* name;
int duration;
char* path;
} Frame;
// Link (node) struct
typedef struct FrameNode
{
Frame* frame;
struct FrameNode* next;
} FrameNode;
The functions:功能:
FrameNode* addFrame(Frame* frame)
{
char name[100] = { 0 };
char path[100] = { 0 };
int dur = 0;
Frame* p = (Frame*)malloc(sizeof(Frame));
printf("*** Creating a new frame ***\n");
printf("Please insert frame path:\n");
fgets(path, 100, stdin);
path[strcspn(path, "\n")] = 0;
strcpy(p->path, path);
printf("Please insert frame duration <in miliseconds>:\n");
scanf("%d", &dur);
getchar();
p->duration = dur;
printf("Please chooce a name for a new frame:\n");
fgets(name, 100, stdin);
name[strcspn(name, "\n")] = 0;
strcpy(p->name, name);
while (list != NULL)
{
if (strcmp(list->frame->name, p->name) == 0)
{
printf("The name is already taken, Please enter another name\n");
fgets(name, 100, stdin);
name[strcspn(name, "\n")] = 0;
strcpy(p->name, name);
}
list = list->next;
}
free(p);
return p;
}
FrameNode* insertAtEnd(FrameNode** list, Frame* fr)
{
if (*list)
{
FrameNode* help = *list;
FrameNode* tmp = (FrameNode*)malloc(sizeof(FrameNode));
tmp->frame = addFrame(fr);
while (help->next != NULL)
{
help = help->next;
}
help->next = tmp;
}
else
{
list = (FrameNode*)malloc(sizeof(FrameNode));
FrameNode* tmp = (FrameNode*)malloc(sizeof(FrameNode));
tmp->frame = addFrame(fr);
list = tmp;
}
}
I really need help, I have to submit it by Sunday.我真的需要帮助,我必须在周日之前提交。 If you notice any more problems please tell me Thank you to everyone!!!!!
如果您发现任何问题,请告诉我谢谢大家!!!!!!!
A separate struct for a linked list usually adds nothing but complexity.链表的单独结构通常只会增加复杂性。 Why not just use:
为什么不直接使用:
struct my_frame {
struct my_frame *next;
char *name;
int duration;
char *path;
}
It reduces complexity and simplifies memory allocation.它降低了复杂性并简化了 memory 分配。
A few personal preferences: I suggest using a prefix to prevent generic names like "frame".一些个人喜好:我建议使用前缀来防止像“框架”这样的通用名称。 I also suggest dropping the typedef.
我还建议删除 typedef。 It does nothing for you.
它对你没有任何作用。 It only adds obscurity.
它只会增加模糊性。 Drop the capitals in type names.
去掉类型名称中的大写字母。 If you want to program in C, learn C don't try to make it look like C++, C# or Java.
If you want to program in C, learn C don't try to make it look like C++, C# or Java.
At the end of the addFrame
function you free p
, but then you return it.在
addFrame
function 结束时,您释放p
,但随后您将其返回。
So in the line tmp->frame = addFrame(fr);
所以在
tmp->frame = addFrame(fr);
in the function insertAtEnd
, you assign a pointer that has been freed already, whose contents are unknown.在 function
insertAtEnd
中,您分配了一个已被释放的指针,其内容未知。
The solution is to simply remove the line free(p)
in addFrame
, but make sure that you have a free'ing mechanism later in your program for this list.解决方案是简单地删除
addFrame
中的free(p)
行,但请确保稍后在程序中为该列表提供释放机制。
Edit: also, I now see that in the line list = tmp;
编辑:另外,我现在在
list = tmp;
行中看到了, you are actually assigning a value to the local variable list
, and what you should be doing is *list = tmp
. ,你实际上是在给局部变量
list
赋值,你应该做的是*list = tmp
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.