Reputation: 131
I have a linked list, and I need to create a node right after the head..
it means I have something like this:
node *head = NULL;
and my linked list in the end should be like :
head -> node -> NULL
...
but when I use a normal addNode function, it gives me a runtime error(not sure which, my debug has problems)...
this is what I wrote:
void addNode(node *head)
{
node *temp = head; // a temp to not move the head
node *newNode = (node*)malloc(sizeof(node)); // The new node
while (temp -> next != NULL)
{
temp = temp -> next // Getting to the last node
}
temp -> next= newNode; // Adding the new node into the linked list insted of the NULL
newNode -> next = NULL; // Adding the NULL after the new node
}
This code works great to me when I have a linked list with already 1 or more nodes ,but if the linked list only has a head, it does problems to me... how can I solve the problem?
(if you didnt understand my problem - With the addNode function I wrote here, i'm getting a runtime error for adding a new node into a head that points to NULL already)..
Thanks, Amit :)
Upvotes: 3
Views: 2704
Reputation: 44250
You could use a pointer to pointer:
void addNode(node **pp)
{
node *newNode = malloc(sizeof *newNode); // The new node
if (newNode) {
newNode->next = *pp; // steal the parent. (this will also work if *pp happens to be NULL)
*pp = newNode; // let *pp point to the new node
}
}
To be called like:
...
node *head = NULL;
addNode( &head);
...
Upvotes: 1
Reputation: 4186
You have to check if Head is null. Otherwise when you try to check
head->next != NULL
head is NULL so you are referring to random place in memory
You can't add node after head if head is NULL. You have to allocate memory for head and then set 'next' pointer. By the way why u want to set head->next while head is null?
EDIT
Mayby you should try add flag to nodes like bool active and set it false when you want to pass it.
I'll try to say it in another way. You can't set head->next because head is NULL. NULL means, that it's just a pointer, to nowhere. It's a variable where u can place some address, but nothing else. If u want to have there a structure, like node, you have to place there address of new object of type Node:
Node element = malloc(sizeof(Node));
head = element;
After that u will have in head address of Node object and u will be able to revoke to variables (like Node *next) inside this structure.
Upvotes: 2
Reputation: 122001
Need to check if head
is NULL on entry, otherwise a null pointer will be dereferenced:
node *temp = head; /* temp set to head, possibly null. */
while (temp->next != NULL) /* 'temp' dereferenced, undefined behaviour
if 'temp' is null. */
In order for the change to be seen by the caller, you will need to pass in a node**
(as suggested by wildplasser), as C passes arguments by value. Change to (for example):
void addNode(node **head)
{
node *newNode = malloc(sizeof(node)); /* No need to cast. */
if (newNode)
{
newNode->next = NULL;
if (NULL == *head)
{
*head = newNode; /* Set the head, as there isn't one yet. */
}
else
{
node* temp = *head;
while (temp->next) temp = temp->next;
temp->next = newNode;
}
}
}
This would be called:
node* my_list = NULL;
addNode(&my_list);
Upvotes: 3