2013-05-02 24 views
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'的成員。我看不出哪裏出了問題,有人可以識別代碼中的問題嗎?

+1

你的'createList()'函數沒有返回一個可用的值。編譯器應該發出警告。順便說一句:恕我直言,typedeffed的名字mare程序幾乎不可讀。 – wildplasser 2013-05-02 06:30:05

+1

順便說一句:你不需要*列表頭部的特殊節點類型。它僅用於攜帶'struct LinkedListNode *頭部;'或'LinkedListNode *頭部'。 – wildplasser 2013-05-02 06:49:27

回答

0

TinCan沒有現場PID

可能:

testProc->label=69; 

,而不是

testProc->pid=69; 
+0

OP已編輯帖子並報告了其他錯誤 – suspectus 2013-05-02 06:23:55

+0

對不起,我在粘貼的代碼中犯了一個錯誤,並在您回覆後修復了它。 – Dawson 2013-05-02 06:24:49

+1

代碼[http://pastebin.com/6LMth3Qc](http://pastebin.com/6LMth3Qc),編譯ok – Denis 2013-05-02 06:36:23

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

- >返回myList中;

}