我正在创建一个程序,从文件中读取信息并在 4 个链接列表之间分配信息。我创建了以下结构来表示信息。
struct ListStruct{
int waitingTime;
struct ListStruct *next;
};
我的问题与这些 typedef 有关。虽然第一个定义了结构,但我不确定第二个的作用。它创建了一个指向 struct 'Patron' 的指针,但是它的 typedef 是否意味着任何可以让我轻松地在 4 个链表之间移动的东西?如果是这样,那么有效的利用方法是什么?
typedef struct ListStruct Patron;
typedef Patron *ListHeadPtr;
最佳答案
does it being typedef'd imply anything that may allow me to easily move between 4 linked lists?
不,这只是为了方便。
If so, what is an effective way to utilize this?
您现在可以将链接列表传递到函数中,如下所示:
ListHeadPtr append(ListHeadPtr aList, ListHeadPtr anotherList) {
....
}
附录
“cons”一个元素(即将其插入列表的开头):
ListHeadPtr cons(int waitTime, ListHeadPtr list) {
ListHeadPtr first = malloc(sizeof(Patron));
if (first == NULL) {
/* Take evasive action */
}
first->waitingTime = waitTime;
first->next = list;
return first;
}
关于从一个结构创建多个链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15712732/