Im trying to create a LinkedList that will be populated with LinkedLists. The main LinkedList will hold a characters name and description, as well as how many nodes are held in its inner LinkedList. The inner LinkedList will hold the chapter number they appear in and a brief description for that chapter. So for instance, character Joe(name) is a King(descriptor) and appears in chapters 4 7 and 10. So he would have 3 inner nodes with a description of what he did in those chapters. I don't think Im adding to the lists right though, because when I call to iterate through the lists and print everything, I only get the first person I added.
The two structs I made.
struct Person{
char * name;
char * descriptor;
int count;
struct Person * next;
struct Information * info_head;
};
struct Information{
char * text;
int chapter;
struct Information * next;
};
Creating pointers.
struct Person * new_person, *temp_pers, *head_pers;
struct Information * new_info, *temp_info, *head_info;
function used for adding new characters.
void add(char * name, char * descriptor, char * info, int chapter){
new_person = (struct Person *)malloc(sizeof(struct Person));
new_info = (struct Information *)malloc(sizeof(struct Information));
new_info->chapter = chapter;
new_info->text = info;
new_person->name = name;
new_person->descriptor = descriptor;
if(head_pers == NULL){ //List is empty
head_pers = new_person; //add new person
new_person->info_head = new_info;//link to information
head_pers->next = NULL;
head_info = new_info; //name that information start of information list
head_pers->count = 1;
head_info->next = NULL;
}
else{
temp_pers = head_pers;
temp_info = head_info;
while(temp_pers != NULL){ //iterate through list of people
if(strcmp(temp_pers->name, name) == 0){ //adding a duplicate
while(temp_info != NULL){ //iterate through that persons info list
temp_info = temp_info->next;
} //reached the end of the list
temp_info = new_info;
temp_pers->count = temp_pers->count + 1;
temp_pers->next = NULL;
}
temp_pers = temp_pers->next;
}
//reached end of persons list with no find
//add new person to the end of the list
temp_pers = new_person;
temp_pers->count = temp_pers->count + 1;
temp_pers->next = NULL;
}
}
My printing method for testing
void printAll(){
temp_pers = head_pers;
temp_info = head_info;
while(temp_pers != NULL){
printf("%s, %s %d\n", temp_pers->name, temp_pers->descriptor, temp_pers->count);
while(temp_info != NULL){
printf("%d\t%s", temp_info->chapter, temp_info->text);
temp_info = temp_info->next;
}
temp_pers = temp_pers->next;
}
}
Main method
int main(){
add("Joe", "the King", "had a child.", 4);
add("Joe", "the King", "started a war", 7);
add("Sue", "the Queen", "poisoned Joe", 10);
printAll();
return 0;
}
Dealing with a LinkedList of LinkedLists is confusing me and maybe I'm just missing something really small, or maybe something really big, but any help would be great. Almost forgot, the code does compile and outputs this...
Joe, the King 2
4 had a child.
Because its printing out the count for Joe as 2, I think it's working some what.