我正在尝试构建一个简单的链接列表,但我收到一个编译错误,告诉我我尝试访问的链接列表节点不包含我期望的字段。这些是我的链表方法:
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”的成员。我看不到哪里出错了,有人可以识别代码的问题吗?