JohnM.904
JohnM.904

Reputation: 1

Inserting a new node at the tail of a linked list

So I'm having trouble inserting a node into the tail of my linked list. I understand the concept, and I believe my code is correct but my program keeps crashing. I have my list created in main with a function that inserts new nodes to the head of the list. I have no issue with this, but just the function inserting to the tail. Here is the code below.

#include <stdio.h>
#include <stdlib.h>
typedef struct node {
    int number;
    struct node * next;
} Node;

typedef Node * Nodeptr;

void insertnode_tail(Nodeptr head);
Nodeptr find_last(Nodeptr head);

void insertnode_tail(Nodeptr head) // function to insert node at the tail.
{
    Nodeptr here = find_last(head);
    Nodeptr newentry = NULL;
    int n = 0;

    printf("Enter the value to be assigned to the new entry? \n");
    scanf("%d", &n);

    if((newentry = malloc(sizeof(Node))) == NULL) {
    printf("No Memory\n");
    exit(0);
    }

    newentry -> number = n;
    newentry -> next = NULL;
    here -> next = newentry;
    traverse1(head);
}


Nodeptr find_last(Nodeptr head) // Function to return the last node of list
{
    Nodeptr aux = head;
    int n = 0;
    while(aux != NULL) {
    aux = aux->next; // moves the aux pointer along the list
    n++;
 }

 return aux;
}

Upvotes: 0

Views: 124

Answers (3)

Vlad from Moscow
Vlad from Moscow

Reputation: 311068

The function find_last always returns NULL

Nodeptr find_last(Nodeptr head) // Function to return the last node of list
{
    Nodeptr aux = head;
    int n = 0;
    while(aux != NULL) {
    aux = aux->next; // moves the aux pointer along the list
    n++;
 }

 return aux;
}

because the loop inside it stops iterations only when the condition of the loop

    while(aux != NULL) {

is false that is when aux is equal to NULL.

So in this statement of the function insertnode_tail there is an attempt to dereference the NULL pointer.

here -> next = newentry;

Thus the function has undefined behavior.

The functions in general are invalid. The problem is that if a node is appended to an empty list then the head must be updated. It means that the head has to be passed to the functions by reference.

The functions can be defined the following way

Nodeptr * find_last( Nodeptr *head ) // Function to return the last node of list
{
    while( *head ) head = &( *head )->next;

    return head;
}

void insertnode_tail( Nodeptr *head ) // function to insert node at the tail.
{
    Nodeptr *here = find_last( head );

    int n = 0;

    printf("Enter the value to be assigned to the new entry? \n");
    scanf("%d", &n);

    if( ( *here = malloc( sizeof( Node ) ) ) == NULL) 
    {
        printf("No Memory\n");
        exit(0);
    }

    ( *here )->number = n;
    ( *here )-> next = NULL;

    traverse1(*head);
}

Upvotes: 1

Tanuj Yadav
Tanuj Yadav

Reputation: 1277

in find_last() function you should write-

while(aux->next != NULL)

so the updated function is-

Nodeptr find_last(Nodeptr head) // Function to return the last node of list
{
    Nodeptr aux = head;
    int n = 0;
    while(aux->next != NULL) {
    aux = aux->next; // moves the aux pointer along the list
    n++;
 }

 return aux;
}

your program was crashing because you were returning NULL value as the last node.

Upvotes: 1

Honza Dejdar
Honza Dejdar

Reputation: 957

Your find_last function always returns NULL. The condition in while cycle should be while (aux->next!= NULL).

Upvotes: 1

Related Questions