繁体   English   中英

将包含三个结构的链表传递给函数

[英]Passing linked list that consists of three structures to a function

我知道如何创建具有两种结构的链表

为此,我声明了一个包含所有必要数据的结构。 看起来像这样:

struct Data{
    int numb;
    int date;
}

第二结构表示具有 (即列表的第一元素)和到下一个节点的链接的节点。

struct llist{
    Data d;
    llist *next;
}

我不知道如果我想我的LLIST添加到会代表名单另一种结构。

struct mainList{
    llist l;
}

我知道这可能会带来一些困难,因为我不确定如何将主列表传递给函数。

在这里,我尝试打印链接列表

void show(mainlist *ml){
    llist *u = ml->l;
    while(u){
        printf("Date: %s\t Name: %s\n",  u->d.dat, u->d.uname/* u->d.dat, u->d.uname*/);
        u=u->next;
    }
}

但是出现一个错误,说“在初始化时我不能'从'到''到'”,所以,我在这里一无所知...有什么想法吗?

有很多问题-但是,与您要引用的错误有关的是一行:

llist *u = ml->l;  /* I guess you mean struct llist *u = ml->l */

show功能。 这里ustruct llist * ,但ml->lstruct llist ,但不是指向它的指针。 您需要将struct mainList更改为:

struct mainList{
    struct llist *l;
}

因此ml->lstruct llist *

在下面的有效解决方案中,您的代码段存在一些问题。 在评论中指出...

#include <iostream>
using namespace std;

struct Data {
    int numb;
    int date;
};

struct llist {
    Data d;
    llist *next;
};

struct mainList{
    llist *l; /*should be a pointer as you are referencing it as a pointer*/
};

void show(mainList *ml){ /*should be mainList, your code snippet shows 'mainlist'*/
    llist *u = ml->l;
    while(u){
        printf("Date: %d\t Name: %d\n",  u->d.date, u->d.numb/* u->d.dat, u->d.uname*/); /*your code snippet was using unavailable members of the struct*/
        u=u->next;
    }
}

int main ()
{

    mainList ml;

    show(&ml);

    return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM