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
here's a corrected version of your program :
#include <stdio.h>
#include <stdlib.h>
typedef struct linkedList
{
int first;
struct linkedList* rest; // add struct in the beginning
} linkedList;
linkedList* addToList(linkedList* ll, int a);
void go_trough(linkedList *ll); // here's an extra function to check
int main()
{
linkedList *ll ; // working with a pointer is easier and makelist is pointless work with add to list instead
ll = NULL; // initialize to NULL
ll = addToList(ll, 7);
ll = addToList(ll, 9);
go_trough(ll);
return 0;
}
linkedList* addToList(linkedList* ll, int a) // I didn't understand what you were trying to do so ... here's my version
{
if(!ll)
{
ll = malloc(sizeof(linkedList*)); //allocating enought space to hold the structure
ll->first = a;
ll->rest = NULL;
}
else
ll->rest = addToList(ll->rest , a);
return ll;
}
void go_trough(linkedList *ll)
{
if(ll)
{
printf("%d\n" , ll->first);
go_trough(ll->rest);
}
}
in makeList change
second.rest = &c;
first.rest = &b;
to
ll.rest = &second;
second.rest = &third;
in the original you were giving the adresses of the int variables instead of the linkedList nodes. also, you had a variable 'first' which was never declared, that's where one of errors were taking place.
also try declaring all your variables first, it makes it easier to read.
The C compiler doesn't have a complete typedef
of linkedList
before you attempt to use it in your struct
. You have a couple of options:
typedef struct linkedList
{
int first;
struct linkedList* rest;
} linkedList;
Or:
typedef struct linkedList linkedList; // C allows this forward declaration
struct linkedList
{
int first;
linkedList* rest;
};
This is your starting point.
Additional problems include but are not limited to:
makeList
function refers to variable first
but it doesn't appear to be defined anywhere.ll->rest = newL;
assigned a type linkedList
to a pointer to linkedList
(linkedList *
) you can't assign a value to a pointer-to-value. The compiler error message linked.c:43:13:...
states this. It would need to be ll->rest = &newL;
... HOWEVER...newL
is LOCAL to the function addToList
, so you can't assign it's address to a persistent list item since it will go out of scope when the code leaves that block.addToList
you are assigning pointer to integer to a variable that holds pointer to linkedList
, e.g., second.rest = &c;
.A few observations,
Anyway, here is a single linked list, without keeping track of the tail of the list, or counting the elements.
#include <stdio.h>
#include <stdlib.h>
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;
}