Page 1 of 1

linked lists

Posted: Fri Jan 03, 2003 7:30 pm
by zsepi
could somebody tell me the proper form to create a linked list? I thought the
[c]
typedef struct myStruct {
int data;
myStruct *next;
};
[/c]
was the way, but the OJ demonstrated me that I was wrong :(

Posted: Fri Jan 03, 2003 7:45 pm
by Jordan Gordeev
What says the Online Judge, that is, what error message you get?

Posted: Fri Jan 03, 2003 7:51 pm
by zsepi
I received compile error... however, I did a bit of search on the net, and I found one way the OJ compiles it:
[c]
typedef struct _myStruct {
int data;
struct _myStruct *next;
}myStruct;
[/c]
this compiles both on my machine and the OJ's - though to be honest, I pretty much don't know why I had to put the underscore :( could someone give me a link to a good & clear explaination of this issue? now I just have to figure out why I am getting WA, but that's another forum...

Posted: Fri Jan 03, 2003 8:05 pm
by Jordan Gordeev
Not the underscore makes it work. It is the struct keyword.
See:
[c]typedef struct myStruct {
int data;
/*!!!*/myStruct *next;
};[/c]
[c]typedef struct _myStruct {
int data;
/*!!!*/struct _myStruct *next;
}myStruct;[/c]

Posted: Tue Jan 28, 2003 10:45 am
by Larry
if you use typedef, you have to define what you're typedef-ing.. =)

Posted: Tue Jan 28, 2003 2:03 pm
by Dominik Michniewski

Code: Select all

typedef struct _x
{
     struct _x *next;
} X;
is using in C . _x is necessary, because we don't know what's type of next when compiling this code (the same as struct before it)

in C++ we could write

Code: Select all

struct X
{
     X *next;
};
because struct in C++ is treated as object :) and comiler know type of next in moment of compiling . So ... it's easy :D

Regards
Dominik Michniewski

Posted: Thu Apr 17, 2003 7:54 pm
by deddy one
Hmm I always get runtime error when I tried
to apply linked list

the libarary function that I need
is

#include <stdlib.h>
and
#include <malloc.h>


right ????

Posted: Thu Apr 17, 2003 9:17 pm
by Hisoka
I always use stdlib.h when I used linkedlist, and I was not got RE. and I don't know what happen if you use malloc.h