如何管理链表中的各种数据? [英] How to manage different kinds of data in a linked list?

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

问题描述

我有一个项目,老师要求我们在链接列表中进行一些操作。好的,它们很容易实现,但是我无法管理列表中的数据。它们可以是以下任意一种:int,char,float或string(char array)。我知道如何单独链接这些链接,但是当它们混合在一起时,事情就会变得混乱起来。

I have a project that the teacher asks us to do some operations in a linked list. Okay, they are pretty easy to implement but I'm having trouble to manage the data inside of my list. They can be any of those: int, char, float or string(char array). I know how to link any of those individually but when they're mixed up things start to get messy.

我没有尝试太多,我被困住了。这是我想到的一些想法:创建4个结构,每种数据类型1个(但我从未见过不同结构的链表,也许不是定义列表,因为它们不是同一结构类型)或为每种数据类型创建一个带有声明的结构。重要的是要告诉我有一个变量,该变量可以告诉我当时正在管理哪种类型的数据(但是,当我为函数传递参数时,我并没有全部参数,除非我想出了一些标志但是看起来很愚蠢,而且该项目没有为我的变量指定任何限制)。

I have not tried much, I'm stuck. Here are some thoughts that passed through my mind: create 4 structs, 1 for each data type (but I've never seen a linked list of different structs, maybe it's not a list by definition because they are not of the same struct type) or create 1 struct with a declaration for every data type. It's important to tell that I have a variable that tells me what type of data I'm managing at that moment (but when I pass the arguments for my function, I don't have all of them, unless I come up with some flags but it seems pretty dumb and the project didn't specify any limitations for my variables).

抱歉,没有显示任何代码,我认为在这种情况下没有必要,因为我的想法没有用。我可以向您显示期望的结果,例如:

Sorry for not showing any code, I think that it's not necessary in this case because my ideas are not working. I can show you the results that I expect to have, for example:

给出数据(第一个数字告诉我列表中有多少个节点):

Given the data (the first number tells me how many nodes my list has):

5

f 3.14

d 100

cx

s园景

d 300

我希望我的结果是:

3.1400 100 x园林300

3.1400 100 x gardenal 300

我是这个学科的新手,我试图在上面阐明我的代码思想。感谢您阅读本文,并祝您星期四愉快。

I'm new at this subject and I tried to explicit my ideas of code above. Thank you for reading this far and have a nice Thursday.

推荐答案

通常,您需要在<$ c中添加类型标记$ c>结构节点,以便您可以跟踪存储在各个节点中的数据的类型。

In general you need to add a type tag to struct Node so that you can track the kind of data stored in the individual nodes.

要存储数据,可以使用void指针,也可以使用联合。如果您使用空指针,则每次访问数据时都需要进行强制转换。如果您使用联合,则每个节点最终将使用与最大联合成员的大小相对应的内存。

For storing data you can use a void pointer or you can use a union. If you use a void pointer you'll need casting whenever accessing data. If you use a union every node will end up using memory corresponding to the size of the largest union member.

这里是一个简单的示例,使用空指针 strong>:

Here is a simple example using void pointer:

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

enum ListType 
{
    INT = 0,
    FLOAT,
    CHAR,
    STRING,
};

struct Node
{
    struct Node *next;
    enum ListType type;
    void *data;
};

void printNode(struct Node *p)
{
    switch (p->type)
    {
        case INT:
            printf("%d ", *((int*)p->data));
            break;
        case FLOAT:
            printf("%f ", *((float*)p->data));
            break;
        case CHAR:
            printf("%c ", *((char*)p->data));
            break;
        case STRING:
            printf("%s ", (char*)p->data);
            break;
        default:
            printf("ERROR ");
            break;
    }
}

void printList(struct Node *p)
{
    while(p)
    {
        printNode(p);
        p = p->next;
    }
}

void freeListData(struct Node *p)
{
    while(p)
    {
        free(p->data);
        p = p->next;
    }
}

int main(void) {
    // Build the list manually to illustrate the printing
    struct Node N1;
    struct Node N2;

    N1.type = FLOAT;
    N1.data = malloc(sizeof(float));
    *((float*)N1.data) = 3.14;
    N1.next = &N2;

    N2.type = INT;
    N2.data = malloc(sizeof(int));
    *((int*)N2.data) = 100;
    N2.next = NULL;

    // .. more nodes

    printList(&N1);

    freeListData(&N1);

    return 0;
}

输出:

3.140000 100

这是一个示例使用工会

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

enum ListType 
{
    INT = 0,
    FLOAT,
    CHAR,
    STRING,
};

union ListData
{
    int d;
    float f;
    char c;
    char *str;  // Memory for the string must be malloc'ed
};

struct Node
{
    struct Node *next;
    enum ListType type;
    union ListData data;
};

void printNode(struct Node *p)
{
    switch (p->type)
    {
        case INT:
            printf("%d ", p->data.d);
            break;
        case FLOAT:
            printf("%f ", p->data.f);
            break;
        case CHAR:
            printf("%c ", p->data.c);
            break;
        case STRING:
            printf("%s ", p->data.str);
            break;
        default:
            printf("ERROR ");
            break;
    }
}

void printList(struct Node *p)
{
    while(p)
    {
        printNode(p);
        p = p->next;
    }
}

void freeListStrings(struct Node *p)
{
    while(p)
    {
        if (p->type == STRING) free(p->data.str);
        p = p->next;
    }
}

int main(void) {
    // Build the list manually to illustrate the printing
    struct Node N1;
    struct Node N2;
    struct Node N3;

    N1.type = FLOAT;
    N1.data.f = 3.14;
    N1.next = &N2;

    N2.type = INT;
    N2.data.d = 100;
    N2.next = &N3;

    N3.type = STRING;
    N3.data.str = malloc(sizeof "Hello World");
    strcpy(N3.data.str, "Hello World");
    N3.next = NULL;

    // .. more nodes

    printList(&N1);

    freeListStrings(&N1);

    return 0;
}

输出:

3.140000 100 Hello World 

这篇关于如何管理链表中的各种数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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