So I have the following implementation of linked list:
#ifndef LINKED_LIST
#define LINKED_LIST
struct Student
{
char* name;
char* surrname;
char* signin_number;
char* grade;
char* date;
struct Student* next;
};
struct Student* begin = NULL;
void push_back(struct Student* student)
{
struct Student* temp = NULL;
if(begin == NULL)
{
begin = student;
begin->next = NULL;
}
else
{
temp = begin;
while( temp->next != NULL )
temp = temp->next;
student->next = NULL;
temp->next = student;
}
}
void clear()
{
struct Student* temp = NULL;
if (!begin) return;
while(begin)
{
temp = begin->next;
free(begin->name);
free(begin->surrname);
free(begin->signin_number);
free(begin->date);
free(begin->grade);
free(begin);
begin = temp;
}
}
void print()
{
struct Student* ptr = begin;
while(ptr != NULL)
{
printf("%s\n", (*ptr).name);
printf("%s\n", ptr->surrname);
printf("%s\n", ptr->signin_number);
printf("%s\n", ptr->date);
printf("%s\n", ptr->grade);
ptr = ptr->next;
}
}
#endif
But the clear function does not work ... how should I free malloced memory?