Programming in C, Stephen Kochan - Chapter 11, Exe

2019-06-14 19:02发布

问题:

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:

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.