3
\$\begingroup\$

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 <stdio.h>

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 = &n1;

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

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

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

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

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

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

    return 0;
}

void insertEntry(struct entry *addOn, struct entry *element)
{
    element->next = addOn;
    addOn->value = 400;
    addOn->next = (struct entry *) 0;
}
\$\endgroup\$
4
\$\begingroup\$

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 <stdio.h>

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

void insertEntry (struct entry *new, struct entry *follow)
{
    new->next = follow->next;
    follow->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 = &n2;

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

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

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

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

    printf ("\nlinked list: ");

    listPtr = &n1;

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

    printf ("\n");

    // insert new entry

    n2_5.value = 250;

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

    insertEntry (&n2_5, &n2);    

    printf ("linked list: ");

    listPtr = &n1;

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

    printf ("\n");

    return 0;
}
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.