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 ‘linkedList’
linked.c: In function ‘makeList’:
linked.c:30:2: error: ‘first’ undeclared (first use in this function)
linked.c:30:2: note: each undeclared identifier is reported only once for each function it appears in
linked.c: In function ‘addToList’:
linked.c:36:9: error: used struct type value where scalar is required
linked.c:43:13: error: incompatible types when assigning to type ‘int *’ from type ‘linkedList’
if anybody can see what is wrong and explain it to me, it would be much appreciated. My code is below.
#include <stdio.h>
typedef struct linkedList
{
int first;
linkedList* rest;
} linkedList;
linkedList makeList(int a, int b, int c);
void addToList(linkedList* ll, int a);
int main()
{
linkedList ll = makeList(1,3,5);
addToList(&ll, 7);
addToList(&ll, 9);
return 0;
}
linkedList makeList(int a, int b, int c)
{
linkedList ll;
ll.first = a;
linkedList second;
second.first = b;
linkedList third;
third.first = c;
third.rest = NULL;
second.rest = &c;
first.rest = &b;
return first;
}
void addToList(linkedList* ll, int a)
{
while (*ll)
{
if (ll->rest == NULL)
{
linkedList newL;
newL.first = a;
newL.rest = NULL;
ll->rest = newL;
break;
} else
{
continue;
}
}
}