Hi,

I am trying to understand linked lists. Can someone help me understand this linked list function:

Code:
```Node* insertNode(Node*head,int value1){
Node*newNode=(Node*)malloc(sizeof(Node));
newNode->value=value1;
}
else{

}
}{```
thanks

2. This function is to insert elements to the end of a singly linked list.
Assuming you know what a linked list is, you call the insertNode() passing it the pointer to the first node in the linked list(head) and the value of the element (value1) you want to insert to the rear of the list.
Inside the function, first you allocate space for a Node and assign it value1, ie. newNode->value=value1 and also initialize the Node pointer to NULL.
Next you check whether head is NULL, if yes it means that the linked list is empty and you are inserting the first element, hence you simply assign head to newNode and return head. So now head points to the first node.

now suppose you call this function again, after allocating space for a new Node and assigning the value to it, you check if head is NULL. but as there is one Node in the list, it is not NULL. So now to insert the new Node to the rear of the list you have to find the last Node.
the "else" part finds the last Node of the list, and then links the last Node to the newNode ie. temp->link=newNode

3. This site explains linked lists well. Very easy concept. Here's a bigger illustration.

Quzah.

4. flyingfin thanks for the help. quzah the site you gave have a bunch of games inside it.

5. Right. Barrel of Monkys. Perfect illustration of linked lists.

Quzah.

6. Originally Posted by quzah
This site explains linked lists well. Very easy concept. Here's a bigger illustration.

Quzah.
lol oh now i get it!