簡體   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