[英]Passing linked list that consists of three structures to a function
I know how create a linked list with two structures 我知道如何创建具有两种结构的链表
To do so, I declare a structure that contains all the necessary data. 为此,我声明了一个包含所有必要数据的结构。 It looks like this:
看起来像这样:
struct Data{
int numb;
int date;
}
Second structure represents a node that has a head (ie first element of the list) and a link to the next node. 第二结构表示具有头 (即列表的第一元素)和到下一个节点的链接的节点。
struct llist{
Data d;
llist *next;
}
I wonder what if I wanted to add my llist to another structure that would represent a list . 我不知道如果我想我的LLIST添加到会代表名单另一种结构。
struct mainList{
llist l;
}
I know it may create some difficulties as I'm not quite sure how to pass main list to a function. 我知道这可能会带来一些困难,因为我不确定如何将主列表传递给函数。
Here, I've tried to print linked list 在这里,我尝试打印链接列表
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;
}
}
But got an error saying that "I cannot 'llist' to 'llist*' in initialization' So, I'm clueless here... Any ideas? 但是出现一个错误,说“在初始化时我不能'从'到''到'”,所以,我在这里一无所知...有什么想法吗?
There are many issues - however, one pertaining to the error you are referring is the line: 有很多问题-但是,与您要引用的错误有关的是一行:
llist *u = ml->l; /* I guess you mean struct llist *u = ml->l */
in show
function. 在
show
功能。 Here u
is a struct llist *
, but ml->l
is a struct llist
, but NOT a pointer to it. 这里
u
是struct llist *
,但ml->l
是struct llist
,但不是指向它的指针。 You need to change the struct mainList
as: 您需要将
struct mainList
更改为:
struct mainList{
struct llist *l;
}
So that ml->l
is a struct llist *
. 因此
ml->l
是struct llist *
。
Working solution below, your code snippet had some issues. 在下面的有效解决方案中,您的代码段存在一些问题。 pointed out in comments...
在评论中指出...
#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.