Reputation: 847
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.
Upvotes: 0
Views: 5356
Reputation: 225827
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){
Upvotes: 4