linked lists

Write here if you have problems with your C source code

Moderator: Board moderators

Post Reply
zsepi
Learning poster
Posts: 51
Joined: Thu Sep 26, 2002 7:43 pm
Location: Easton, PA, USA

linked lists

Post 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 :(
Jordan Gordeev
New poster
Posts: 14
Joined: Tue Nov 12, 2002 6:04 pm
Location: Bulgaria

Post by Jordan Gordeev »

What says the Online Judge, that is, what error message you get?
zsepi
Learning poster
Posts: 51
Joined: Thu Sep 26, 2002 7:43 pm
Location: Easton, PA, USA

Post 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...
Jordan Gordeev
New poster
Posts: 14
Joined: Tue Nov 12, 2002 6:04 pm
Location: Bulgaria

Post 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]
Larry
Guru
Posts: 647
Joined: Wed Jun 26, 2002 10:12 pm
Location: Hong Kong and New York City
Contact:

Post by Larry »

if you use typedef, you have to define what you're typedef-ing.. =)
Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

Post 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
deddy one
Experienced poster
Posts: 120
Joined: Tue Nov 12, 2002 7:36 pm

Post 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 ????
Hisoka
Experienced poster
Posts: 120
Joined: Wed Mar 05, 2003 10:40 am
Location: Indonesia

Post 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
Post Reply

Return to “C”