簡體   English   中英

處理將結構插入鏈表的混亂(C)

[英]Confusion dealing with inserting struct into linked list (C)

我有一個任務,其中一些功能交給我,其他一些功能要自己實現。 我自己執行的功能之一是將單個參數添加到結構對象,然后調用插入函數(已提供)將該對象放入鏈表中。 但是,插入函數讓我丟失的參數之一。

這是 .h 的。

struct node{
    Task *task;
    struct node *next;
};
typedef struct task{
    char *name;
    int tid;
    int priority;
    int burst;
} Task;

這是我的 add 函數的開始,我在這里創建對象並打算將它插入到鏈表中。

void add(char *name, int priority, int burst){

    Task task1 = (Task){.name = name, .priority = priority, .burst = burst};
    //insert() here
}

但是,插入的參數讓我感到困惑。

void insert(struct node **head, Task *task);

我知道 task1 將是第二個參數,但我不知道如何處理第一個參數。 如何在我的添加函數中調用插入?

謝謝!

您需要在某處保留一個列表頭變量,初始化為NULL ,例如:

struct node *list_head = NULL;

然后將地址傳遞給您的函數:

insert(&list_head, task);

傳遞地址的原因是, insert可以修改它以指向新的列表頭。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM