简单链接列表 - 无法访问节点

时间:2013-05-02 06:16:03

标签: 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一个struct并将其发送到列表中,如下所示:

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'的成员。我无法看到我出错的地方,有人可以通过代码识别问题吗?

2 个答案:

答案 0 :(得分:0)

TinCan没有字段 pid

也许:

testProc->label=69;

而不是

testProc->pid=69;

答案 1 :(得分:0)

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

- > return myList;

}