c - 简单链表 - 无法访问节点

标签 c data-structures linked-list

我正在尝试构建一个简单的链接列表,但出现编译错误,告诉我尝试访问的链接列表节点不包含我期望的字段。这些是我的链接列表方法:

typedef struct TinCan
    {
    int label;
    } TinCan;

typedef struct LinkedListNode
    {
    TinCan *data;
    struct LinkedListNode *next;
    } LinkedListNode;

typedef struct LinkedList
    {
    LinkedListNode *head;
    } LinkedList;

LinkedList* createList() /*creates empty linked list*/
    {
    LinkedList* myList;
    myList = (LinkedList*)malloc(sizeof(LinkedList));
    myList->head = NULL;
    }

我 malloc 一个结构并将其发送到列表,如下所示:

LinkedList* canQueue=createList();
TinCan* testCan = (TinCan*) malloc(sizeof(TinCan));
testProc->pid=69;
insertLast(canQueue, testCan);

void insertLast(LinkedList* list, ProcessActivity *newData)
    {
        int ii = 1;
    LinkedListNode* newNode = (LinkedListNode*)malloc(sizeof(LinkedListNode));
    newNode->data = newData;

    //check if queue empty
    if(list->head == NULL)
        {
        list->head = newNode;
        newNode->next=NULL;
        }
    else
        {
        LinkedListNode* current = list->head;
        while (current->next != NULL)
            {
            current = current->next;
            }
        current->next = newNode;
        printf("%d", ii);
        ii++;
        }
}

然后我尝试像这样访问节点:

testLinkedList(cpuQueue);

void testLinkedList(LinkedList* list)
    {
        int count = 1;
        LinkedListNode* current = list->head;
        while (current != NULL)
            {
            printf("%d: Label is is %d", count, current->data->label);



            current = current->next;
            count++;
            }
    }

最后一个方法出现编译错误:“LinkedListNode”没有名为“label”的成员。我看不出哪里出了问题,有人可以找出代码的问题吗?

最佳答案

TinCan 没有字段 pid

也许:

testProc->label=69;

而不是

testProc->pid=69;

关于c - 简单链表 - 无法访问节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16331530/

相关文章:

c - memcpy 将数组对象复制到 C 中的字符指针,反之亦然

java - 删除链表中出现多次的每个元素的第一次出现

c - 在c中排序双向链表

c - 找到节点后从链表中删除节点

c - GstMultifilesink 后消息回调

c - 这个C语言程序有什么问题吗?

java - 使用数据库中的数据创建 JSON 数组

java - 二叉搜索树Tree Implementation,程序终止于eclipse。简单的插入和显示操作

arrays - 将链表数据获取到 C 中的 String 数组

c - 二维数组程序在输出中显示垃圾值