Inserting a node in the end of a linked list

Nahids

Below fragment is invalid to inserting a node in the end of Linked List.

void insert_end(int item){

    nodeptr newNode = new ListNode;
    newNode->data = item;
    newNode->next = NULL;

    if(head == NULL){
        head = newNode;
        curr = head;

    }else{
        curr = head;

        while(curr != NULL) curr = curr->next;
        curr = newNode;
    }
}

Another fragment that is valid to inserting a node in the end of Linked List.

void insert_end(int item){

    nodeptr newNode = new ListNode;
    newNode->data = item;
    newNode->next = NULL;

    if(head == NULL){
        head = newNode;
        curr = head;

    }else{
        curr = head;

        while(curr->next != NULL) curr = curr->next;

        curr->next = newNode;
    }
}

My question is why 1st one is invalid? Actually two fragments should be similar. Assume i have three nodes already. Now i want to inserting another node.

  1. As first algorithm when curr = NULL, then while loop will not satisfied.
  2. As second algorithm when curr->next = NULL then while loop will not satisfied.

so can i say first algorithm's 'curr' and second algorithm 'curr->next' both are similar, when "while loop" terminated? if it's not similar then why?

aicastell

You must understand a pointer is a variable which value is an address.

First algorithm is wrong because when you finish the iteration of while loop, curr points to NULL (address 0x0), and NULL is not a valid node of your list.

Second algoritm works because when you finish the iteration of while loop, curr points to last node of your list, and you add newNode to curr->next, replacing NULL by the address of newNode.

Hope this helps! :)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Inserting a node at the end of linked list

From Dev

Inserting a node at the end of a linked list

From Dev

Inserting a node at the end of linked list, .exe crashes

From Dev

Inserting a node at the end of a doubly linked list

From Dev

inserting a node at the end of linked list recursivly

From Dev

Inserting linked list at the end

From Dev

Inserting Node into a Linked List

From Dev

Unexpected value after inserting node at the end of linked list

From Dev

inserting node to linked list in c

From Dev

Inserting Node in a Sorted linked list

From Dev

inserting node at beginning of linked list

From Dev

inserting node at beginning of linked list

From Dev

Adding a node to the end of a linked list

From Dev

Linked list, add node to end

From Dev

Add end node to linked list

From Dev

Inserting a node in a pre sorted linked list

From Dev

Inserting a node into a sorted doubly linked list

From Dev

Singly linked list in C(Inserting a node)

From Dev

Linked list - Inserting in middle, linking new node

From Dev

Inserting a node into a sorted doubly linked list

From Dev

Inserting a node at a specific position in a Linked list

From Dev

Inserting node at beginning of singly linked list

From Dev

Inserting a node before a given node in doubly linked list

From Dev

Inserting a new node into a doubly linked list after a given node

From Dev

adding a node at the end of a Linked List (java)

From Dev

Deletion of node at end of linked list C++

From Dev

Removing the end node from a linked list

From Dev

linked list moving node to the end C

From Dev

adding a node at the end of a Linked List (java)

Related Related

HotTag

Archive