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 .
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. Here u
is a struct llist *
, but ml->l
is a struct llist
, but NOT a pointer to it. You need to change the struct mainList
as:
struct mainList{
struct llist *l;
}
So that ml->l
is a 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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.