0

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

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”的成员。我看不到哪里出错了,有人可以识别代码的问题吗?

4

2 回答 2

0

TinCan 没有字段pid

也许:

testProc->label=69;

代替

testProc->pid=69;
于 2013-05-02T06:21:57.803 回答
0
LinkedList* createList() /*creates empty linked list*/
{
  LinkedList* myList;
  myList = (LinkedList*)malloc(sizeof(LinkedList));
  myList->head = NULL;

--> 返回我的列表;

}
于 2013-05-02T06:55:20.410 回答