PDA

View Full Version : [SOLVED] Can't get linked lists working in C



crazyfuturamanoob
November 11th, 2008, 06:01 PM
I tried to make as simple linked list as possible in C.
Here's what I got, exactly. Nothing removed, nothing edited, this is the original file as it is:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct node
{

struct node *next;

char name[80];

int value;

}

struct list
{
struct node *nodes;
}

struct list *init_list()
{
struct list *L;
L = malloc( sizeof(struct list) );
L->nodes = NULL;
return L;
}

struct node *create_node( char name[80], int value )
{
struct node *n;
n = malloc( sizeof(struct node) );

if ( n == NULL )
return NULL;

memset( n, 0, sizeof(struct node) );

n->name = name;
n->value = value;

return n;
}

void add_node( struct list *L, struct node *N )
{
N->next = L->nodes;
L->nodes = N;
}

void destroy_node( struct node *N )
{
free(N);
}

void looptrough( struct list *L )
{
struct node *N;
for (N=L->nodes; N; N=N->next)
printf("Name: \"%s\"\nValue: %i\n", N->name, N->value);
}

int main()
{
/* do stuff */
return 0;
}
But when I try to compile it:

arho@ohra-laptop:~/Työpöytä/C/teststuff/linkedlist$ gcc -o list list.c
list.c:21: error: two or more data types in declaration specifiers
list.c:21: error: two or more data types in declaration specifiers
list.c: In function ‘init_list’:
list.c:26: warning: return from incompatible pointer type
list.c: In function ‘create_node’:
list.c:39: error: incompatible types in assignment
arho@ohra-laptop:~/Työpöytä/C/teststuff/linkedlist$

Can't get further. I need help. I'm a total noob in C, and I got no idea what might it be.

Cracauer
November 11th, 2008, 06:13 PM
Missing semicolons;

mvs1207
November 11th, 2008, 06:14 PM
missing ';' for struct definitions.

crazyfuturamanoob
November 11th, 2008, 07:02 PM
Where do I have to put semicolons?

Edit: Figured out with reference manual.

Sinkingships7
November 11th, 2008, 07:12 PM
Where do I have to put semicolons?

After the closing brace of struct definitions. Example:


struct node{
struct node *next;
};

You can also write it like this:


struct{
struct node *next;
}node;

but I personally don't prefer it...

Sinkingships7
November 11th, 2008, 07:14 PM
double post