0

我正在尝试通过将字符串值传递给 initialize 方法然后将该字符串分配给新的链表来创建一个初始化多个链表的方法。

例如,如果我传递Initialize('list1')给 Initialize 方法,我需要它来初始化一个名为 list1 的新链表。

这是我需要每个列表使用的结构和初始化方法:

struct node {
    int number;
    struct node *next;
};
typedef struct node item;

void Initialize(char *name) {

}

我不知道如何开始创建初始化方法。请帮忙。

4

1 回答 1

0

A good place to start would be realizing that your question doesn't make sense as-is. Initialize() can't "initialize a linked list called X". What Initialize can do is allocate and return an item*.

item* Initialize() {
  /* Call malloc() here, among other things. */
  /* And what do you want to initialize the number to? Where should that come from? */
}

Then,

int main(void) {
  item* list1=Initialize();
  /* ... */
}

Note that this initializes one node - a "list" of one isn't very useful, is it?

于 2013-11-06T19:23:58.423 回答