# Thread: i can't seem to understand the logic behind linked lists :|

1. and here i've shortended to code even more, i guess it's easier to understand:

Code:
```  	for (prev=NULL; loc != NULL; loc = loc->next)
{
prev = loc;
}

prev->next = temp;```

2. Originally Posted by Axel
and here i've shortended to code even more, i guess it's easier to understand:

Code:
```  	for (prev=NULL; loc != NULL; loc = loc->next)
{
prev = loc;
}

prev->next = temp;```
check for loc->next is not equal to null

ssharish2005

3. wouldn'tt that already be handled when i malloc the new node?

4. noo. the reason why i said, when u interate through each node u will check for the last node.The way u can find is that the last node next should be null. So when u check for loc->next is equal to null u will come to know that u reached end of the node. and then u can follow with appending a new node.

ssharish2005

5. Originally Posted by Axel
and here i've shortended to code even more, i guess it's easier to understand:

Code:
```  	for (prev=NULL; loc != NULL; loc = loc->next)
{
prev = loc;
}

prev->next = temp;```
this is not quite rite ...

say u have
Code:
`"Panda" -> NULL`
on the first iteration, loc = *start yes?
and then you assign prev to loc...
Code:
`prev = loc;`
and after that, the for loop checks the nullity of loc
Code:
`for (prev=NULL; loc != NULL; loc = loc->next)`
.. in this case, no... and then loc goes to the next element which is null...
Code:
`for(..., loc = loc->next)`
which is a problem because after that
Code:
`prev = loc; // prev becomes null (but we don't want it to be)`
remember in linked list, the last element isn't denoted by NULL... it's denoted by the last item BEFORE NULL
NULL is just like '\0' character in string.

got me? this is exactly what ss was saying

6. ok i think i've understood my original code now please correct me if i'm wrong.

Code:
`if (*start == NULL)`
evaluates to false so we go to the else bit.

Code:
```else
{
loc = *start; // MAKE loc point to the start of the list.
prev = NULL;

for (i=0; loc != NULL; i++) //here we just loop till we find an empty position to append to
{

prev = loc; // keep track of the previous node by copying what the current node equals to
loc = loc->next; // set loc to the next node
}```
Code:
```once the loop ends it means we have found an empty position i.e.:

[   CAT  ]    [ NULL ]

prev              loc

so:

prev->next = temp;

we just insert the new node AFTER prev->next i.e.:

[   CAT  ]    [ DOG (new node inserted) ]

prev      - >  next```

7. i don't see why you need the i in
Code:
`for (i=0; loc != NULL; i++)`
just do something like
Code:
`for(loc = *start; loc != NULL; loc = loc -> next)`
this, not only you make it easier to understand, but also make your code shorter because then you can take out the
Code:
` loc = *start`
at the start of the else bit.

better yet
Code:
```for(loc = *start; loc->next != NULL; loc = loc->next) {
// do nothing, it's all done in the loop
}

loc->next = temp;```