从一个结构创建多个链表

标签 c data-structures linked-list

我正在创建一个程序,从文件中读取信息并在 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/

相关文章:

c - 该算法更准确的时间复杂度是多少?

data-structures - 谁说的 "data structure(s) is half the code"?

android - Java,如何实现perl的哈希数组?

Java LinkedList 数组引用

C 通过读取系统调用在我的缓冲区中查找换行符

c++ - libcurl 无法获取 CURLINFO_EFFECTIVE_URL

c# - 如何减少 ""中涉及的内存和操作量 找到添加到 N"面试挑战的所有对?

java - Java 中的链表引用

algorithm - 在循环链表的末尾插入一个节点的时间复杂度?

c - 循环文本以匹配 C 中两个链接列表之间的字符计数