Home > Blockchain >  Inserting a new node between a linked list using double pointer
Inserting a new node between a linked list using double pointer

Time:10-15

The following function is supposed to insert a new node into its proper place in an ordered list, returning a pointer to the first node in the modified list. Unfortunately, the function doesn't work correctly in all cases. Explain what's wrong with it and show how to fix it. Assume that the node structure is the one defined in Section 17.5.

struct node {
    int value;
    struct node *next;
};

struct node *insert_into_ordered_list(struct node *list, struct node *new_node)
{
    struct node *cur = list, *prev = NULL;
    while (cur->value <= new_node->value) {
        prev = cur;
        cur = cur->next;
    }
    prev->next = new_node;
    new_node->next = cur;
    return list;
}

that above one is the problem of k.n.king C programming chapter 17 exercise 13 and I coded it as below one.

struct node *insert_into_ordered_list(struct node *list, struct node *new_node)
{
    struct node **pp = &list;
    while (list != NULL) {
        if (list->value >= new_node->value)
            break;
        pp = &list->next;
        list = list->next;
    }
    list = new_node;
    return list;
}

it doesn't make any compile error but did I use double pointer in the right way in a linked list? I used it to keep track of the list until I find the right place to insert new_node and after finding it, loop terminates and then new_node will be assigned to list. But it doesn't seem to work properly, please tell me what made it wrong.

CodePudding user response:

This function

struct node *insert_into_ordered_list(struct node *list, struct node *new_node)
{
    struct node **pp = &list;
    while (list != NULL) {
        if (list->value >= new_node->value)
            break;
        pp = &list->next;
        list = list->next;
    }
    list = new_node;
    return list;
}

changes the local variable list. Pay attention to that function parameters are function local variables

Change the function the following way

struct node * insert_into_ordered_list( struct node *list, struct node *new_node )
{
    struct node **pp = &list;

    while ( *pp != NULL && !( new_node->value < ( *pp )->value ) ) 
    {
        pp = &( *pp )->next;
    }

    new_node->next = *pp;
    *pp = new_node;

    return list;
}.

Here is a demonstration program.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

struct node 
{
    int value;
    struct node *next;
};

struct node * insert_into_ordered_list( struct node *list, struct node *new_node )
{
    struct node **pp = &list;

    while ( *pp != NULL && !( new_node->value < ( *pp )->value ) ) 
    {
        pp = &( *pp )->next;
    }

    new_node->next = *pp;
    *pp = new_node;

    return list;
}

FILE * display( const struct node *head, FILE *fp )
{
    for ( ; head != NULL; head = head->next )
    {
        fprintf( fp, "%d -> ", head->value );
    }

    fputs( "null", fp );

    return fp;
}

int main(void) 
{
    struct node *head = NULL;

    const size_t N = 10;

    srand( ( unsigned int )time( NULL ) );

    for ( size_t i = 0; i != N; i   )
    {
        struct node *new_node = malloc( sizeof( *new_node ) );

        new_node->value = rand() % N;
        new_node->next = NULL;

        head = insert_into_ordered_list( head, new_node );
    }

    fputc( '\n', display( head, stdout ) );

    return 0;
}

The program output might look like

0 -> 0 -> 1 -> 2 -> 3 -> 3 -> 4 -> 6 -> 6 -> 8 -> null

Of course you need to write yourself a function that clears the list that is that frees all the allocated memory.

As for me then I would declare and define the function the following way as it is shown in the next demonstration program.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

struct node 
{
    int value;
    struct node *next;
};

int insert_into_ordered_list( struct node **head, int value )
{
    struct node *new_node = malloc( sizeof( *new_node ) );
    int success = new_node != NULL;

    if ( success )
    {
        new_node->value = value;

        while ( *head != NULL && !( new_node->value < ( *head )->value ) ) 
        {
            head = &( *head )->next;
        }

        new_node->next = *head;
        *head = new_node;
    }

    return success;
}

FILE * display( const struct node *head, FILE *fp )
{
    for ( ; head != NULL; head = head->next )
    {
        fprintf( fp, "%d -> ", head->value );
    }

    fputs( "null", fp );

    return fp;
}

int main(void) 
{
    struct node *head = NULL;

    const size_t N = 10;

    srand( ( unsigned int )time( NULL ) );

    for ( size_t i = 0; i != N; i   )
    {
        insert_into_ordered_list( &head, rand() % N );
    }

    fputc( '\n', display( head, stdout ) );

    return 0;
}
  • Related