Exercise to create an insertString function to linked-list Working from 'Programming in C' by Kochan. I'm on an exercise in the chapter 'Pointers'.

This was the exercise:

'Write a function called insertEntry() to inset a new entry into a linked list. Have the procedure take as arguments a pointer to the list entry to be inserted (of type struct entry as defined in chapter), and a pointer to an element after which the new entry is to be inserted.

I've been struggling through this book but this exercise only took me a few minutes, I'm concerned I'm missing the point. Can you please make some suggestions regarding if I've gone wrong?

It compiles and runs fine.

Could I have done this better?

#include &lt;stdio.h&gt;

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

void insertEntry(struct entry *addOn, struct entry *element);

int main (void)
{


    struct entry n1, n2, n3, addOn;
    struct entry *list_pointer = &amp;n1;

    n1.value = 100;
    n1.next = &amp;n2;

    n2.value = 200;
    n2.next = &amp;n3;

    n3.value = 300;
    n3.next = (struct entry *) 0;

    while(list_pointer != (struct entry *) 0)
    {
        printf("%i\n", list_pointer-&gt;value);
        list_pointer = list_pointer-&gt;next;
    }

    list_pointer = &amp;n1;
    insertEntry(&amp;addOn, &amp;n3);

    while(list_pointer != (struct entry *) 0)
    {
        printf("%i\n", list_pointer-&gt;value);
        list_pointer = list_pointer-&gt;next;
    }

    return 0;
}

void insertEntry(struct entry *addOn, struct entry *element)
{
    element-&gt;next = addOn;
    addOn-&gt;value = 400;
    addOn-&gt;next = (struct entry *) 0;
}
I'm not sure if your insertEntry function is correct. It seems to be hardcoded to add an entry at the end of the linked list; you want to be able to add an entry anywhere (except at the start of the list which is the object of the next exercise in the book).

Here's my solution to this exercise.

/* Exercise 10.2

   Write a function called insertEntry() to insert a new entry into a linked
   list. Have the procedure take as arguments a pointer to the list entry to
   be inserted (of type struct entry as defined in this chapter), and a
   pointer to an element in the list *after* which the new entry is to be
   inserted.

   note: inserts n2_5 after n2
*/

#include &lt;stdio.h&gt;

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

void insertEntry (struct entry *new, struct entry *follow)
{
    new-&gt;next = follow-&gt;next;
    follow-&gt;next = new;
}

int main (void)
{
    void insertEntry (struct entry *new, struct entry *follow);

    struct entry n1, n2, n3, n4, n5, n2_5, *listPtr;

    n1.value = 100;
    n1.next = &amp;n2;

    n2.value = 200;
    n2.next = &amp;n3;

    n3.value = 300;
    n3.next = &amp;n4;

    n4.value = 400;
    n4.next = &amp;n5;

    n5.value = 500;
    n5.next = (struct entry *) 0;

    printf ("\nlinked list: ");

    listPtr = &amp;n1;

    while ( listPtr != (struct entry *) 0 ) {
        printf ("%i ", listPtr-&gt;value);
        listPtr = listPtr-&gt;next;
    }

    printf ("\n");

    // insert new entry

    n2_5.value = 250;

    printf ("inserting new entry %i ...\n", n2_5.value);

    insertEntry (&amp;n2_5, &amp;n2);    

    printf ("linked list: ");

    listPtr = &amp;n1;

    while ( listPtr != (struct entry *) 0 ) {
        printf ("%i ", listPtr-&gt;value);
        listPtr = listPtr-&gt;next;
    }

    printf ("\n");

    return 0;
}