且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在C语言编程,斯蒂芬·科昌 - 第11章练习3

更新时间:2023-11-20 16:47:04

作者斯蒂芬·科昌,有一个网站:clas-s-roomm.com

有一个论坛那里一小部分关于他的书,在C语言程序设计,第三版。它包括的答案的奇数结束章节练习。你会发现它非常有用。

下面是该网站作为一个略带如何回答行使相关信息:


  

您可以解决通过建立虚拟结构变量这个问题
      所谓从ListHead,


  
  

例如:

 结构进入从ListHead;

和则可以通过分配从ListHead的下一个成员设置指向列表的头,以指向实际第一
  列表中的条目:

  listHead.next =安培;取值范;

现在插入在列表的前面称为新条目新条目,你可以写:

  insertEntry(安培; new_entry,&安培; LIST_HEAD);


块引用>

我不会张贴code我写的练习 - 你会学到更多,如果你用它自己缠斗

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?

The author, Stephen Kochan, has a website: clas-s-roomm.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.