gpt4 book ai didi

c - C 语言编程,Stephen Kochan - 第 11 章,练习 3

转载 作者:行者123 更新时间:2023-11-30 19:12:45 26 4
gpt4 key购买 nike

我正在通过 Stephen Kochan 的《C 语言编程》一书自学 C,并进行了以下关于指针的练习:

  • 编写一个名为 insertEntry 的函数将新条目插入到链接列表中。让该过程将指向要插入的列表条目的指针(类型为本章中定义的struct entry)和指向列表中要插入新条目的元素的指针作为参数。
  • struct entry结构体定义如下:

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

    这是我的代码:

    #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;
    }

    这段代码工作得很好并且完成了工作。但是还有练习#3:

  • 练习 2 中开发的函数仅在列表中的现有元素之后插入一个元素,从而阻止您在列表的前面插入新条目。如何使用相同的功能并克服这个问题? (提示:考虑设置一个特殊的结构来指向列表的开头。)
  • 我不知道如何继续。如果我设置另一个结构,我将无法在不更改 insert_entry 的形式参数的情况下将其作为参数传递。函数需要。但我无法使用list_ptr指针,因为如果我将它作为第二个参数传递给 insert_entry执行语句new_entry->ptr = prev_entry->ptr;没有意义。

    有关此问题的其他问题集中在练习 #2 上,但我无法找到任何相关内容。帮助将永远感激不已。提前致谢。

    *编辑:这就是我的代码现在的样子(感谢 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应该是一个虚拟结构。该代码的唯一问题是循环显示 n_start.x 的值。 。我怎样才能使它不显示?

    最佳答案

    作者 Stephen Kochan 有一个网站:classroomm.com

    那里有一个论坛,其中有一小部分与他的书“C 语言编程,第三版”相关。它包括奇数章末练习的答案。您会发现它非常有用。

    以下是该网站上的相关信息,作为如何回答练习的小提示:

    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);

    我不会发布我为练习编写的代码 - 如果您自己研究它,您会学到更多。

    关于c - C 语言编程,Stephen Kochan - 第 11 章,练习 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36532241/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com