Ben said:
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)
This doesn't look right. You need to provide the definition
of function enqueue plus code on how you use it. Provide actual
code, not pseudo code.
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).
It will not let you because it is erroneous.
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?
The solution requires you to change your struct link.
Perhaps:
struct link
{
struct people data;
struct link *next;
};
or
struct link
{
struct people *data;
struct link *next;
};
Then rewrite function enqueue and other functions that manipulates
the link list. A simple cast will not work.
Example:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
struct people
{
char *name;
unsigned age;
char *address;
};
typedef struct NODE
{
struct people data;
struct NODE *next;
} NODE;
unsigned NodesInList( NODE *p );
int enqueue( NODE **p, const char *name, unsigned age,
const char *addr );
void PrintList( NODE *p );
void FreeList( NODE **p );
int main(void)
{
NODE *head = NULL;
enqueue(&head,"George Washington",46,
"2000 Adams Blvd\nWashington DC");
enqueue(&head,"Bill Clinton", 43,
"152 E. Hickory Ave\nLittle Rock, AR");
enqueue(&head,"George Bush",37,
"1401 Vine St\nHouston, TX");
printf("There are %u Nodes in the list.\n\n",
NodesInList(head));
PrintList(head);
FreeList(&head);
printf("\nFreed the list. Now there are %u "
"nodes in the list.\n",NodesInList(head));
return 0;
}
unsigned NodesInList( NODE *p )
{
unsigned nelem;
for(nelem = 0; p; p = p->next,nelem++) ;
return nelem;
}
int enqueue( NODE **p, const char *name, unsigned age,
const char *addr )
{
NODE **tmp, *new;
if((new = malloc(sizeof *new)) == NULL) return 0;
if((new->data.name = malloc(strlen(name)+1)) == NULL)
{
free(new);
return 0;
}
if((new->data.address = malloc(strlen(addr)+1)) == NULL)
{
free(new->data.name);
free(new);
return 0;
}
strcpy(new->data.name, name);
strcpy(new->data.address, addr);
new->data.age = age;
new->next = NULL;
for(tmp = p; *tmp != NULL;tmp = &(*tmp)->next) ;
*tmp= new;
return 1;
}
void PrintList( NODE *p )
{
unsigned i;
for(i = 1 ; p; p = p->next,i++)
printf("Node %u:\n%s Age %u\n%s\n\n",i,p->data.name,
p->data.age, p->data.address);
return;
}
void FreeList( NODE **p )
{
NODE *tmp;
for( ; *p; *p = tmp)
{
tmp = (*p)->next;
free((*p)->data.name);
free((*p)->data.address);
free(*p);
}
return;
}