I'm confused on why this Linked List implementation works

I've just recently gotten into linked lists and I'm adding my own "add to end" function.

void insert_at_end(Node** head, Node* node)
    Node* temp;

    temp = *head;

    if (temp == NULL) // linked list is empty
        *head = node;
        while (temp->pNext != NULL)
            temp = temp->pNext;
    temp->pNext = node;

Originally I was doing temp != NULL instead of temp->pNext != NULL because I thought that way it would take me to the very LAST node, since it stops looping when temp still has data before it reaches NULL. Wouldn't temp->pNext != NULL stop at the 2nd to last node? Since it stops looping when it realizes that the last nodes next pointer is NULL, it does not travel to that node?

Thanks guys, If I need to clear anything up from that word vomit let me know.

Wouldn't temp->pNext != NULL stop at the 2nd to last node?

No. Here is the pictorial representation for ease of understanding.

With temp != NULL, temp will be pointing to NULL:

 +---+    +---+    +---+
 | 1 |--->| 2 |--->| 3 |---> NULL
 +---+    +---+    +---+      ^

As you see temp is iterating until it becomes NULL, at which point you cannot attach anything to the final node because you're past it.

With temp->pNext != NULL, temp will be pointing to last node.

 +---+    +---+    +---+
 | 1 |--->| 2 |--->| 3 |---> NULL
 +---+    +---+    +---+

Here temp will iterate till its next node is NULL. That means you're pointing to the final node and you can use that pointer to adjust that node to point to a new one with temp->pNext = node:

 +---+    +---+    +---+    +---+
 | 1 |--->| 2 |--->| 3 |--->| 4 |---> NULL
 +---+    +---+    +---+    +---+

As an aside, you may also want to add the extra safety of ensuring the node you're given points to NULL, on the off chance the caller may forget to do that. That's as simple as adding this line to the start of the function:

node->pNext = NULL;

I (Kendrick Lamar song), noun, plural I's or Is, i's or is. the ninth letter of the English alphabet, a vowel. any spoken sound represented by the letter I or i, as in big, nice, or ski. something� Create an account or log in to Instagram - A simple, fun & creative way to capture, edit & share photos, videos & messages with friends & family.

I, Create an account or log in to Instagram - A simple, fun & creative way to capture, edit & share photos, videos & messages with friends & family. Definition of -i- (Entry 8 of 8) —used as a connective vowel to join word elements especially of Latin origin matrilineal raticide me or I ?: Usage Guide Example Sentences Learn More about i

have a reference for simple linked list concept

let us take an example


struct node          
  int data;// data which you want to feed.
  struct node *next;// gona hold the address of the next data, so that link can be established
}*root,*p; // root is the starting reference to your linked list

case 1: you dont insert any element and your linked list is empty. Now when you insert a new data 10.it will check for any data in the global pointer reference to your linked list, if, the pointer seems to be NULL, then it means your liked list is empty.The memory stack will be created like this.



10 is the data, 1000 is its address.

case 2: adding an element at its back.you want to add, data 20 to your linked list.you will be looking whether the linked list is empty or not first, using your global linked list address reference(it will be a pointer variable, holding your linked list 1st address, as per your code)

now when we see root->data is not null 1000->data is 10

we go for root->next which is null, which we come to know that this is the last node. 1000->next is NULL

and we insert the new node address to the root->next so that a link is created to the newly inserted node.

stack will be created like this

10 20

1000 1004

case3: now you want to add another data say 30 to the end of list again. just follow the same as case 2.

check for the node which is currently null, i.e root->next == NULL, for this you use a loop to find the node which is currently in last, like this.

 struct node *temp; // creating a temp node which will be the new node we will insert
 temp = (struct node *)malloc(sizeof(struct node));// allocating the size for the temp node, same as the size of previous nodes.
 p = root;// i am giving my starting address to pointer p, i traverse the node with pointer p only, since i dont want to loose my root starting address.
 while(p->next != NULL)// checking for the last node
    p = p->next;// if last node not found, just move to next node, and see whether it is last node or not
 p->next = temp;// if last node is found, put the address of newly created temp node to the node previously found last in the linked list.
 temp->data = element;// feeding data to the temp node.
 temp->next = NULL;// keeping temp node as last, it is necessary to say temp wont has any more node connected.

NOTE dont think this is just a temp node, and will disappear once we come out of function, it is playing with pointer, so it wont destroyed until, the owner wont destroy it.

flow will be

newly created temp address is 1008

1000->next is 1004

1004->next is NULL

so, 1004->next will hold 1008 now

then,1008->data will be 30

then, 1008->next will be NULL

stack will be created like this

10 20 30

1000 1004 1008

Instagram, I definition is - the 9th letter of the English alphabet. How to use i in a sentence. me or I? I or i is the ninth letter and the third vowel letter of the modern English alphabet and the ISO basic Latin alphabet. Its name in English is i (pronounced / ˈaɪ /), plural ies.

I, The ninth letter of the basic modern Latin alphabet. I (lower case ı). The letter i without a dot above, in both the upper case and the lower case versions. Out now on iTunes: http://smarturl.it/iKL Google Play: http://smarturl.it/iKLgp Amazon: http://smarturl.it/iKLamz Best of Kendrick Lamar: https://goo.gl/PTr3

I, Windows is either a registered trademark or a trademark of Microsoft Corporation in the United States and/or other countries. Mac is a trademark of Apple Inc. Ï, lowercase ï, is a symbol used in various languages written with the Latin alphabet; it can be read as the letter I with diaeresis or I-umlaut.

[PDF] Form I-9, Employment Eligibility Verification, Consideration of Deferred Action for Childhood Arrivals. Department of Homeland Security. U.S. Citizenship and Immigration Services. USCIS. Form I- 821D. Log in to i-Ready®, online assessment and instruction that helps teachers provide all students a path to proficiency and growth in reading and mathematics.

  • You'll have a lot of questions like this as you learn more about data structures. When they come up, draw a picture of the data structure in question and point to it as you work through the code. You'll learn a lot that way.
  • @paxdiablo Thank you for the humbleness! It is very good improvement.
  • No problems, kiran In reality, I just couldn't stomach / \ as an up-arrow when a perfectly adequate ^ was available :-) All the other changes were just incremental improvements.
  • Sorry for the late reply but thank you, I understand this now
  • That would have been funnier if the NULL was the empty field beyond the buffer at the last station. By the time the train ends up in that field, there's no way to build tracks there :-)