Ok I am creating an ADT for a singly linked list. I have a Struct name List that stores the pointer to the first node(first item in the list, which is also a struct)and size. The node stores first name and a pointer to the next node. Here are the sturcts:
typedef struct List *ListP;
struct List{
ListP head;
int size;
};
struct node{
char name[20];
nodeP next;
};
First I called malloc to give me memory for struct List:
ListP newList;
newList = malloc(sizeof(struct List)); //should I typecast this to (ListP *)?
if (newList == NULL){
fprintf(stderr, "Memory allocation failed");
}
else{
newList->head = NULL;
newList->size = 0;
}
Then I called malloc again to give me memory for the first node:
struct node *new;
newNode = malloc(sizeof(struct node));
if (newNode == NULL){
fprintf(stderr, "Memory allocation failed");
}
else{
newNode->name = 'Jay';
newNode->next = NULL;
Now that I have my List and a new node, I assigned list->head to the address of the new node;
newList->head = newNode;
Untill this time the compiler doesn't complain. But when I try to access the elements in the first node using the pointer that I have for the list:
name = newList->head->name;
The compiler complains that struct List has no member named 'name'
How can I access the field in struct node, assuming that I only have pointer to the struct List and List->head is pointing to the first node. Any help would be appreciated.