I'm fairly new to C and we got an excersie where we need to programm a linked list with predetermined functions and parameters. Now I got a problem to add an new elemnt to the beginning of the list, everything else works, here the syntax.
int main(){
Vector3D foo;
foo.x = 521;
foo.y = 2;
foo.z = 3;
VectorList * head;
head = create_VL(NULL, &foo);
insertElementBack(head, &foo);
foo.x = 456;
insertElementBack(head, &foo);
foo.x = 2;
insertElementFront(head, &foo);
print_list(head);
printf("%d\n", size(head));
}
void insertElementFront(VectorList* l, Vector3D* v){
VectorList *previous, *new_VL;
previous = &l;
new_VL = NULL;
new_VL = malloc(sizeof(VectorList));
new_VL -> value = *v;
new_VL -> next = previous;
l = new_VL;
}
VectorList *create_VL(VectorList* l, Vector3D* v) {
VectorList* new_VL = (VectorList*)malloc(sizeof(VectorList));
if(new_VL == NULL)
{
printf("Error creating a new node.\n");
exit(0);
}
new_VL->value = *v;
new_VL->next = l;
return new_VL;
}
void insertElementBack(VectorList* l, Vector3D* v){
VectorList *vl = l;
while( vl -> next != NULL){
vl = vl -> next;
}
VectorList *new_List = create_VL(NULL, v);
vl -> next = new_List;
}
The name and the parameters aren't allowed to be changed, I could solve this problem with a double pointer as parameter but it's not allowed. Could someone please give me a hint, I tried a lot of things but nothing worked.
Best
Martin
You really should have included the definition of VectorList
instead of making us guess at its structure, but your problem is probably this:
previous = &l;
l
is a pointer to a VectorList
. By doing &l
you're taking the address of the pointer, not the VectorList
. So when you do this
new_VL -> next = previous;
your next
pointer now points to that pointer, not the VectorList
. In fact, aren't you getting a warning on that? Don't ignore warnings.
Your second and bigger problem is head
doesn't get updated to reflect the new head, and without being able to use double pointers your only way of solving that is to return the new head node as a return value from insertElementFront
.
In void insertElementFront(VectorList* l, Vector3D* v){
l is a local variable just holding the address of head. Changing the address locally does not affect head variable.
You can make head a global variable and approach the following way
void insertElementFront(VectorList* l, Vector3D* v){
VectorList *new_VL;
new_VL = NULL;
new_VL = malloc(sizeof(VectorList));
new_VL -> value = *v;
new_VL -> next = l;
head = new_VL
}
or You can return the head pointer
VectorList* insertElementFront(VectorList* l, Vector3D* v){
VectorList *new_VL;
new_VL = NULL;
new_VL = malloc(sizeof(VectorList));
new_VL -> value = *v;
new_VL -> next = l;
return new_VL
}
and
head = insertElementBack(head, &foo);
For more info look https://www.geeksforgeeks.org/how-to-write-functions-that-modify-the-head-pointer-of-a-linked-list/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.