【问题标题】:a linked list of linked lists in CC中链表的链表
【发布时间】:2015-01-04 17:34:34
【问题描述】:

我正在做一个项目,想知道是否可以创建一个链表的链表。 我想在 C 中创建一个新类型 person,其中每个 person 都可以有 kidskidsperson 的列表,并且每个 person 都有 parents 他们也是 person em>s.所以我正在考虑使用结构和链表来做到这一点。

#include <stdio.h>

struct person {
unsigned int id;    //identity,unique for every person
char* name;
struct person **father;
struct person **mother;
struct kids **kids;
}

struct kids {
struct person **kid;
struct kids **next_kid;
}; 

提前感谢您的宝贵时间。

【问题讨论】:

  • 是的,你可以。您还有其他问题吗?
  • 我不确定你为什么要声明指针指针 (person **father)。 person *father 可能会。哦,正如@Quentin 所说的“当然可以,伙计”。其余的等等,无止境。欢迎来到复杂数据结构的奇妙世界。
  • 只需在结构中使用单个指针。对于函数代码,您可能希望使用指向结构指针的指针,这样就不需要对头指针和下一个指针进行特殊处理。例如,要初始化: node **ppnode = &head;推进:ppnode = &((*ppnode)->next); .
  • 非常感谢您的回复。我不太确定。@DanAllen 我做双指针的原因是,当我更改或初始化某人的父母时,我不必返回我所有的东西函数将是 void 而不是类型 person( void define_father(person p,person Father); 而不是 'p->father=define_father(person p,person Father);' )

标签: c linked-list


【解决方案1】:

是的,您可以拥有一系列列表,其中一个示例如下所示,每个孩子都有自己的玩具列表。

一、两类对象(儿童和玩具)的相关头文件和结构:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct sToy {
    char name[50];
    struct sToy *next;
} tToy;

typedef struct sChild {
    char name[50];
    tToy *firstToy;
    struct sChild *next;
} tChild;

然后,一个用于分配内存的辅助函数,这样我就不必通过大量错误检查来污染样本:

void *chkMalloc (size_t sz) {
    void *mem = malloc (sz);

    // Just fail immediately on error.

    if (mem == NULL) {
        printf ("Out of memory! Exiting.\n");
        exit (1);
    }

    // Otherwise we know it worked.

    return mem;
}

接下来,辅助函数分配这两种类型的对象并将它们插入到相关列表中。请注意,我在列表的开头插入以简化代码,因此我们不必担心列表遍历或存储最终项指针。

这意味着在转储详细信息时,所有内容都将以相反的顺序打印,但为了保持简单,这是一个很小的代价:

void addChild (tChild **first, char *name) {
    // Insert new item at start.

    tChild *newest = chkMalloc (sizeof (*newest));
    strcpy (newest->name, name);
    newest->next = *first;
    *first = newest;
}

void addToy (tChild *first, char *name) {
    // Insert at start of list.

    tToy *newest = chkMalloc (sizeof (*newest));
    strcpy (newest->name, name);
    newest->next = first->firstToy;
    first->firstToy = newest;
}

接下来,以可读格式转储列表的功能:

void dumpDetails (tChild *currChild) {
    // For every child.

    while (currChild != NULL) {
        printf ("%s has:\n", currChild->name);

        // For every toy that child has.

        tToy *currToy = currChild->firstToy;
        if (currToy == NULL) {
            printf ("   <<nothing>>\n");
        } else {
            while (currToy != NULL) {
                printf ("   %s\n", currToy->name);
                currToy = currToy->next;
            }
        }
        currChild = currChild->next;
    }
}

最后,还有一个将所有其他人联系在一起的主要功能:

int main (void) {
    tChild *firstChild = NULL;

    addChild (&firstChild, "Anita");
        addToy (firstChild, "skipping rope");
    addChild (&firstChild, "Beth");
    addChild (&firstChild, "Carla");
        addToy (firstChild, "model car");
        addToy (firstChild, "trampoline");

    dumpDetails (firstChild);

    return 0;
}

当你输入、编译和运行所有代码时,你会发现它很容易处理列表:

Carla has:
   trampoline
   model car
Beth has:
   <<nothing>>
Anita has:
   skipping rope

【讨论】:

  • 非常感谢您的时间和精力,现在一切似乎都不那么混乱了。
  • 如何为玩具添加主人?这意味着您必须将指向 struct sChild 的指针添加到 struct sToy 中,但 struct sChild 尚不清楚。
  • @nobody,您可以拥有指向不完整结构的指针,因此您可以在 sToy 定义之前放置 struct sChild; 声明,然后可以添加类似 struct sChild *owner; 的内容。
  • 谢谢。我自己也发现了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-06
  • 2021-06-26
  • 1970-01-01
相关资源
最近更新 更多