I am trying to make a struct in C that is a linked list. I am not really sure what is going wrong though. My errors are:
linked.c:6:2: error: unknown type name
A few observations,
Anyway, here is a single linked list, without keeping track of the tail of the list, or counting the elements.
#include
#include
typedef struct listnode
{
int data;
struct listnode* next;
} linkedList;
linkedList* makeList(int a, int b, int c);
void addToList(linkedList* ll, int a);
void ListPrint(linkedList* ll);
int main()
{
linkedList* ll = makeList(1,3,5);
addToList(ll, 7);
addToList(ll, 9);
ListPrint(ll);
return 0;
}
linkedList* ListNew(int a) //new linkedList node
{
linkedList* newL = (linkedList*)malloc(sizeof(linkedList));
newL->data = a;
newL->next = NULL;
return newL;
}
linkedList* makeList(int a, int b, int c)
{
linkedList* ll = ListNew(a);
addToList(ll, b);
addToList(ll, c);
return ll;
}
void addToList(linkedList* ll, int a)
{
if(!ll) return;
//find end of list
while (ll->next)
{
ll = ll->next;
}
ll->next = ListNew(a);
return;
}
void ListPrint(linkedList* ll) //print list
{
if(!ll) return;
linkedList* p;
for( p=ll; p; p=p->next )
{
printf("%x: %d\n",p,p->data);
}
return;
}