简单的链表 - 无法访问节点 [英] Simple Linked List - Not able to access nodes

查看:158
本文介绍了简单的链表 - 无法访问节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图构建一个简单的链表,但我得到一个编译错误,告诉我​​,我试图访问不包含我希望它到外地链表节点。这是我的链接列表方式:

I'm trying to construct a simple Linked List but I get a compile error telling me the Linked List node I'm trying to access doesn't contain the field I expect it to. These are my Linked List methods:

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一个结构,并将其发送到列表中,像这样:

I malloc a struct and send it to the list like so:

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++;
        }
}

然后我尝试接取节点像这样:

And then I try to acess the node like so:

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没有名为标签的成员。我看不到的地方我已经错了,有人可以识别与code的一个问题?

The compile error shows up for the last method: 'LinkedListNode' has no member named 'label'. I can't see where I've gone wrong, can someone identify a problem with the code?

推荐答案

TinCan没有现场的 PID

TinCan doesn't have field pid

也许

testProc->label=69;

而不是

testProc->pid=69;

这篇关于简单的链表 - 无法访问节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆