我想構建一個簡單的鏈接列表,但我得到一個編譯錯誤,告訴我我試圖訪問的鏈接列表節點不包含我期望它的字段。這是我的鏈接列表的方法:簡單的鏈接列表 - 無法訪問節點
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'的成員。我看不出哪裏出了問題,有人可以識別代碼中的問題嗎?
你的'createList()'函數沒有返回一個可用的值。編譯器應該發出警告。順便說一句:恕我直言,typedeffed的名字mare程序幾乎不可讀。 – wildplasser 2013-05-02 06:30:05
順便說一句:你不需要*列表頭部的特殊節點類型。它僅用於攜帶'struct LinkedListNode *頭部;'或'LinkedListNode *頭部'。 – wildplasser 2013-05-02 06:49:27