C中链表的链表 [英] a linked list of linked lists in C

查看:28
本文介绍了C中链表的链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am making a project and was wondering if I could create a linked list of linked lists. I want to create a new type person in C in which, every person can have kids. kids are a list of persons and also every person has parents who are also persons.So I am thinking of doing that using structs and linked lists.

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

提前感谢您抽出宝贵时间.

Thank you in advance for your time.

推荐答案

是的,您可以拥有列表列表,下面显示了一个示例,每个孩子都有自己的列表玩具.

Yes, you can have lists of lists, one example of which is shown below, a list of children each with their own list of toys.

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

First, the relevant header files and structures for the two types of objects (children and toys):

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

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

Then, a helper function for allocating memory so I don't have to pollute the sample with lots of error checking:

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

    // Just fail immediately on error.

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

    // Otherwise we know it worked.

    return mem;
}

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

Next, helper functions to allocate the two types of object and insert them into the relevant list. Note that I'm inserting at the start of the list to simplify the code, so we don't have to worry about list traversal or storing the final item pointer as well.

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

That means everything will be printed in reverse order when dumping the details but that's a small price to pay for keeping things simple:

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

接下来,以可读格式转储列表的函数:

Next, the function for dumping the lists in readable format:

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

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

        // For every toy that child has.

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

最后,一个将所有其他函数联系在一起的主要函数:

And, lastly, a main function for tying all the others together:

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

当您输入、编译和运行所有代码时,您可以看到它很容易处理列表:

When you enter, compile and run all that code, you can see that it quite easily handles lists of lists:

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

这篇关于C中链表的链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆