'(C) Inserting into a Linked List struct is not reflecting within other functions
I am attempting to insert nodes into the head of a Linked List object, but the updated head does not appear to reflect in other functions.
My professor has dictated that our insert functions need a List struct and not a pointer to the List struct, but the List struct contains a pointer to a node - the head.
typedef struct Node {
void *Object;
struct Node *next;
} Node;
typedef struct{
Node *head;
} List;
My insertAtHead function resembles this:
int insertAtHead(void* data, List list) {
int error = 0;
Node * head = list.head;
Node * newNode = createNode(data, &error);
newNode->next = head;
list.head = newNode;
return error; }
Node * createNode(void * Object, int * error) {
Node *pointer;
pointer = malloc(sizeof(Node));
if (pointer == NULL) {
*error = 1;
return NULL;
}
pointer->Object = Object;
pointer->next = NULL;
*error = 0;
return pointer;
}
However, my getSize function prints 0 - it only finds the dummy node I use with initList.
int getListLength(List list) {
Node * head = list.head;
int i = 0;
while (head->next != NULL) {
printf("Test\n");
i++;
head = head->next;
}
return i;
}
List initList(int * error) {
int * q = malloc(sizeof(int));
*q = 1;
// I use an integer here solely to prove
List list = {createNode(q, error)};
return list;
}
I'm pretty confused on what I'm doing wrong. I suspect there's some pass-by-value/reference shenanigans going on, but I deal with the head pointer in all of my functions using the list. Please let me know if there's anything I can do to clarify further. Thank you!
Solution 1:[1]
This function
int insertAtHead(void* data, List list) {
int error = 0;
Node * head = list.head;
Node * newNode = createNode(data, &error);
newNode->next = head;
list.head = newNode;
return error; }
deals with a copy of used as an argument object of the type List. So changing the copy is not reflected on the original object.
You need to pass the original object by reference through a pointer to it. For example
int insertAtHead(void* data, List *list) {
int error = 0;
Node * newNode = createNode(data, &error);
if ( error == 0 )
{
newNode->next = list->head
list->head = newNode;
}
return error;
}
If you are not allowed to pass the original object of the type List by reference then you have to return from the function the modified object of the type List as for example
List insertAtHead(void* data, List list, int *error ) {
Node * newNode = createNode(data, error);
if ( *error == 0 )
{
newNode->next = list.head
list.head = newNode;
}
return list;
}
And you will need to assign the returned object to the object of the type List in the caller.
Also this function
List initList(int * error) {
int * q = malloc(sizeof(int));
*q = 1;
// I use an integer here solely to prove
List list = {createNode(q, error)};
return list;
}
does not make a sense.
Initially the list should be empty. So initialization of the list consists from initialization of the data member head to NULL.
Also the function getListLength should look at least like (if you are not allowed to pass the list by reference)
int getListLength(List list) {
Node * head = list.head;
int i = 0;
while (head != NULL) {
printf("Test\n");
i++;
head = head->next;
}
return i;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
