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

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

问题描述

我正在制作一个项目,想知道是否可以创建链接列表的链接列表.我想在C中创建一个新的类型 person ,其中每个 person 都可以有 kids .孩子 person 的列表,并且每个 person 都有 parent 也是 person 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;
}; 

提前感谢您的时间.

推荐答案

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

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.\n");
        exit (1);
    }

    // Otherwise we know it worked.

    return mem;
}

接下来,帮助程序将分配两种类型的对象并将其插入相关列表.请注意,我将在列表的开头插入以简化代码,因此我们不必担心列表遍历或存储最终项目指针的情况.

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

最后,还有一个主要功能,可将所有其他功能绑在一起:

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天全站免登陆