2015-12-29 34 views
0

因此,我是C新手,正在嘗試創建鏈接列表。但由於某種原因,我一直得到這個error: unknown type name List。這是我到目前爲止有:錯誤:未知的類型名稱嘗試創建鏈接列表時的列表

struct Node{ 
    int data; 
    struct Node *next; 
}; 

struct List{ 
    struct Node *head; 
}; 

void add(List *list, int value){ 
    if(list-> head == NULL){ 
     struct Node *newNode; 
     newNode = malloc(sizeof(struct Node)); 
     newNode->data = value; 
     list->head = newNode; 
    } 

    else{ 
     struct Node *tNode = list->head; 
     while(tNode->next != NULL){ 
      tNode = tNode->next; 
     } 
     struct Node *newNode; 
     newNode = malloc(sizeof(struct Node)); 
     newNode->data = value; 

     tNode->next = newNode; 
    } 
} 



void printNodes(struct List *list){ 
    struct Node *tNode = list->head; 
    while(tNode != NULL){ 
     printf("Value of this node is:%d", tNode->data); 
     tNode = tNode->next; 
    } 
} 

int main() 
{ 
    struct List list = {0}; //Initialize to null 
    add(&list, 200); 
    add(&list, 349); 
    add(&list, 256); 
    add(&list, 989); 
    printNodes(&list); 
    return 0; 
} 

我來這裏尋求幫助,因爲我不知道如何用C調試並具有來自Java的不知道太多關於指針,內存分配和東西並想知道我是否會以某種方式搞砸了。我也進一步得到這個警告,不知道這可能意味着什麼。

警告:隱式聲明函數'add'[-Wimplicit-function-declaration] |

幫助讚賞。

回答

4

在使用結構類型的任何地方(假設您沒有使用typedef),必須將struct關鍵字放在類型名稱的前面。

所以這個:

void add(List *list, int value){ 

應該是:

void add(struct List *list, int value){ 
+0

大。這消除了錯誤,但現在我的打印節點出現問題。沒有打印出來。我將編輯我的問題以包含printNodes函數以及我如何使用它。我是否缺少基本的東西? –