简体   繁体   中英

Error: unknown type name List when trying to create a Linked List

So I'm new to C and am trying to create my linked list. But for some reason I keep getting this error: unknown type name List . This is what I have so far:

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;
}

I am coming here for help as I don't know how to debug in C and having come from Java don't know too much about pointers, memory allocation and stuff and wonder if I might have messed up there somehow. I also get this warning further down and don't know what this might mean either.

warning: implicit declaration of function 'add' [-Wimplicit-function-declaration]|

Help appreciated.

Anywhere a struct type is in use (assuming you're not using a typedef ), you have to put the struct keyword before the type name.

So this:

void add(List *list, int value){

Should be:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM