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!!!!!
frameparameter for? It does not seem to be used.listvariable supposed to point to? Thelist = list->next;line changes this variable so that it eventually becomesNULL. If it was pointing to the start of a list of nodes it no longer does so. Perhaps you should use a local variable there to avoid clobbering the global variable.