Hello,
Dont quite understand how your code fixes my query.
I already have a working, linkedlist.c. I know it works because I have
tested it using strings as data.
My linkedlist has the following struct
struct link {
char *data
link *next
}
#excluding respective typedef statements
#enqueue adds a new element to list
enqueue(*link, char *data)
In my main.c program.
I have another structure which i want to put into a linked list
struct people {
char *name
int *age
char *address
}
Now I have included my linkedlist.c into my main.c
However I cant do enqueue(peoplelist, *newpeople)
where peoplelist is a *link and *newpeople is a people
because gcc complains that it wants char instead of people
and enqueue(peoplelist, (char *) newpeople) only gives me
*name when i try to access the element again (because of the string
terminator?). It also wont let me cast it back to (people) and wont
let me access within newpeople (eg newpeople.age).
I was wondering if there was a way to allow linkedlist to use people
as an element but without changing the struct link's char *data to
people *data
Is this possible or is there another better solution?
Tim Rentsch <txr@alumnus.caltech.edu> wrote in message news:<kfn3c0mct19.fsf@alumnus.caltech.edu>...[color=blue]
>
ben19777@hotmail.com (Ben) writes:
>[color=green]
> > I am a newbie with C and am trying to get a simple linked list working
> > for my program. The structure of each linked list stores the char
> > *data and *next referencing to the next link. The problem I get is
> > that I am trying to link a struct that I have defined and its refusing
> > to link. I have tried casting my struct into char * but attempts to
> > cast it back to its original struct to access its contents only seg
> > faults.
> >
> > I'd attempt to change the type of data in the linked list to that my
> > struct but that just doesnt seem right.
> > Any advice is appreciated.[/color]
>
>
> First, don't do any casting. Casting is unnecessary for coding a
> simple linked list, and if casting is unnecessary it usually is best
> to avoid it.
>
>
> Second, start with defining the type, perhaps something like this:
>
> typedef struct node_struct_tag *List, ListElement;
>
> struct node_struct_tag {
> char *data;
> List next;
> };
>
>
> Now, a simple function:
>
> unsigned int
> list_length( List head_of_list ){
> List remainder; unsigned int result;
>
> remainder = head_of_list;
> result = 0;
>
> while( remainder != 0 ){
> result++;
> remainder = remainder->next;
> }
>
> return result;
> }
>
>
> A function for list formation:
>
> List
> lisp_style_cons( char *value, List tail ){
> List result;
>
> result = malloc( sizeof( *result ) );
> assert( result != 0 );
>
> result->data = value;
> result->next = tail;
>
> return result;
> }
>
>
> A function for more typical C-style manipulation:
>
> void
> splice_one_element_into_list( List element, List *list_address ){
> assert( list_address != 0 );
> assert( element != 0 );
>
> element->next = *list_address;
> *list_address = element;
> }
>
>
> That function might be used in a function that inserts into
> a sorted list:
>
> void
> insert_into_sorted_list( List element, List *sorted_list_address ){
> List *insertion_point;
>
> assert( element != 0 );
> assert( sorted_list_address != 0 );
>
> insertion_point = sorted_list_address;
> while( *insertion_point != 0 ){
> if( strcmp( (*insertion_point)->data, element->data ) > 0 ){
> break;
> }
> insertion_point = & (*insertion_point)->next;
> }
>
> splice_one_element_into_list( element, insertion_point );
> }
>
>
> Before trying to compile this code, read through it and make sure
> you understand what you think it's doing and why.
>
> If you want to try compiling, first find the appropriate headers for
> assert(), malloc() and strcmp(), and #include them.
>
> The functions above almost certainly aren't what you want, but if
> you can understand these you should be able to write the functions
> that you do want.
>
> Disclaimer: the code above has not been compiled or even syntax
> checked (except manually by myself). I'm confident the many
> readers of the NG will graciously report any bonehead errors
> I've made. :)[/color]