我试图构建一个简单的链表,但我遇到了一个编译错误,告诉我我试图访问的链表节点没有包含我期望的字段。以下是我的链表方法:
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;
LinkedList* createList() /*creates empty linked list*/
{
LinkedList* myList;
myList = (LinkedList*)malloc(sizeof(LinkedList));
myList->head = NULL;
-->return myList;
}