-1

I'm teaching myself C with the book Programming in C by Stephen Kochan and I have come to the following exercise on pointers:

  1. 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.

The struct entry structure is defined as follows:

struct  entry
{
    int x;
    struct entry  *ptr;
};

This is my code:

#include <stdio.h>

void insert_entry (struct entry  *new_entry, struct entry  *prev_entry);

struct entry
{
    int x;
    struct entry  *ptr;
};

int main (void)
{
    struct entry  n1, n2, n3, new_entry;
    struct entry  *list_ptr = &n1;

    n1.x = 1;
    n1.ptr = &n2;

    n2.x = 2;
    n2.ptr = &n3;

    n3.x = 4;
    n3.ptr = (struct entry *) 0;

    insert_entry (&new_entry, &n2);

    // Loop to display the list to check if insert_entry worked
    while ( list_ptr != (struct entry *) 0 ) {
        printf ("%i\n", list_ptr->x);
        list_ptr = list_ptr->ptr;
    }
}

void insert_entry (struct entry  *new_entry, struct entry  *prev_entry)
{
    new_entry->ptr = prev_entry->ptr;
    prev_entry->ptr = new_entry;

    // Assign a value to make it easier to check if the function worked 
    new_entry->x = 3;
}

This code works just fine and gets the job done. But then there's exercise #3:

  1. The function developed in exercise 2 only inserts an element after an existing element in the list, thereby preventing you from inserting a new entry at the front of the list. How can you use this same function and yet overcome this problem? (Hint: Think about setting up a special structure to point to the beginning of the list.)

I have no idea how to continue. If I set up another structure I won't be able to pass it as an argument without changing the formal parameters of the insert_entry function takes. But I can't use the list_ptr pointer, because if I passed it as the second argument to the insert_entry function the statement new_entry->ptr = prev_entry->ptr; wouldn't make sense.

Other questions about this focus on exercise #2 but I wasn't able to find anything on this. Help would be eternally grateful. Thanks in advance.

*EDIT: This is what my code looks like now (thanks to u/ringzero):

#include <stdio.h>

void insert_entry (struct entry  *new_entry, struct entry  *prev_entry);

struct entry
{
    int x;
    struct entry  *ptr;
};

int main (void)
{
    struct entry  n_start, n1, n2, n3, new_entry;
    struct entry  *list_ptr = &n_start;

    n_start.ptr = &n1;

    n1.x = 1;
    n1.ptr = &n2;

    n2.x = 2;
    n2.ptr = &n3;

    n3.x = 3;
    n3.ptr = (struct entry *) 0;

    insert_entry (&new_entry, &n_start);

    // Loop to display the list to check if insert_entry worked
    while ( list_ptr != (struct entry *) 0 ) {
        printf ("%i\n", list_ptr->x);
        list_ptr = list_ptr->ptr;
    }
}

void insert_entry (struct entry  *new_entry, struct entry  *prev_entry)
{
    new_entry->ptr = prev_entry->ptr;
    prev_entry->ptr = new_entry;

    // Assign a value to make it easier to check if the function worked 
    new_entry->x = 0;
}

n_start is supposed to be a dummy structure. The only problem with the code is that the loop displays the value of n_start.x. How can I make it so that it doesn't get displayed?

  • 1
    This is no tutoring site. But as a hint: Learn about "ring lists". – too honest for this site Apr 10 '16 at 16:21
  • As I understand it the exercise wants you to solve it with the concepts it has taught you, which is a basic linked list. – Hello_World Apr 10 '16 at 16:24
  • 1
    Don't declare functions inside other functions. The declaration should be at global scope, just as the function is defined at global scope. (Yes, it is syntactically valid to do as you did; I'm just discouraging its use. See also GCC option `-Wnested-externs`.) – Jonathan Leffler Apr 10 '16 at 17:16
  • You've not initialized the `new_entry` so its `.x` value is undefined (is that why you have your 'testing mode' `new_entry->x = 3;`). The `.ptr` value doesn't matter since the insertion code will set it anyway. You should factor your printing loop into a function. You should then call the function twice, once before you insert the new entry, and once afterwards. You might end up deciding to print the pointers (the pointer to the current node and the pointer to the next node). See [Correct format specifier to print pointer (address)](https://stackoverflow.com/questions/9053658/). – Jonathan Leffler Apr 10 '16 at 17:19
  • It doesn't make a difference if I initialize `new_entry.x` or assign it a value later. The main problem is solved anyway. I just need to know how to display the list without displaying the dummy structure (`n_start`). BTW, thanks for the function scope tip, FTFY. – Hello_World Apr 10 '16 at 17:26

1 Answers1

1

The author, Stephen Kochan, has a website: classroomm.com

There's a Forum there with a small section relating to his book "Programming in C, 3rd edition." It includes the answers to the odd-numbered end-of-chapter exercises. You could find it quite useful.

Here's the relevant information from that website as a slight hint how to answer the exercise:

You can solve this problem by setting up a "dummy" structure variable called listHead,

for example:

     struct entry  listHead;  

and you can then set it pointing to the head of the list by assigning the next member of listHead to point to the actual first entry of the list:

       listHead.next = &entry1;  

Now to insert a new entry called newEntry at the front of the list, you can write:

       insertEntry (&new_entry, &list_head);

I won't post the code I wrote for the exercise -- you'll learn more if you wrangle with it yourself.

Flexo
  • 87,323
  • 22
  • 191
  • 272
Verbatim
  • 245
  • 1
  • 2
  • 8
  • Thank you very much. I think I've got it now. The only problem now is that the loop displays also the `x` member of the `n_start` structure, which is supposed to be a dummy structure. – Hello_World Apr 10 '16 at 17:17
  • @Hello_World: You need to set list_ptr = n_start.ptr just before printing the list of entries. Also, n_start.next = &n1; should probably be n_start.ptr = &n1; – Verbatim Apr 10 '16 at 18:22