The following code is supposed to initialize a linked list of data, then generate all the possible combinations of the sub categories in the linked list. I then want to create a new linked list of data which contains the combinations (which point to the original data instead of copying the data itself).
The only problem is I'm only able to point to the intermediary array that temporarily stores the pointers to the original data. How can I create pointers to the original data Then again there could always be something else in the mix of things that I'm missing
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//////////////////////////////////////// Nested Structs / Linked lists ////////////////////////////////////////
//////////////////// Parent/Child Nested Structure ////////////////////
struct child
{
char name[15];
int number;
struct child *next;
};
struct parent
{
char *name;
struct child *head_child;
struct parent *next;
};
//////////////////// Combination List Structure ////////////////////
struct list
{
char *parent_name;
struct child *chosen_child;
};
struct list_collection
{
struct list_collection *next;
struct list *current; // will be an array
};
////////// Global Variables //////////
struct child *current_child;
struct parent *head_parent;
struct parent *current_parent;
struct list_collection *head_list;
struct list_collection *current_list;
//////////////////////////////////////// Functions ////////////////////////////////////////
///////////////////////// Display Function /////////////////////////
void display_list()
{
struct list_collection *temp;
struct list_collection *previous;
int c;
current_list = head_list;
temp = current_list;
printf("\n********************************************************\n");
while(current_list != NULL)
{
printf("\n----------------------------------------\n\n");
for(c = 0; c < 2; c++)
{
printf("Parent Name: %s\n", temp->current[c].parent_name);
printf("Child Name: %s\n", temp->current[c].chosen_child->name);
printf("Child Number: %d\n", temp->current[c].chosen_child->number);
}
previous = current_list;
current_list = current_list->next;
}
current_list = previous;
}
///////////////////////// Add Functions /////////////////////////
void add_parent(char *name, size_t size)
{
struct parent *temp;
temp = malloc(sizeof(struct parent));
temp->name = malloc(size);
strcpy(temp->name, name);
if (head_parent == NULL)
{
head_parent = temp;
head_parent->next = NULL;
head_parent->head_child = NULL;
current_parent = head_parent;
}
else
{
current_parent->next = temp;
current_parent->next->next = NULL;
current_parent->next->head_child = NULL;
current_parent = current_parent->next;
}
}
void add_child(char name[15], int num)
{
struct child *temp;
temp = malloc(sizeof(struct child));
strcpy(temp->name, name);
temp->number = num;
if (current_parent->head_child == NULL)
{
current_parent->head_child = temp;
current_parent->head_child->next = NULL;
current_child = current_parent->head_child;
}
else
{
current_child->next = temp;
current_child = current_child->next;
current_child->next = NULL;
}
}
void add_list(struct list *new_combination)
{
struct list_collection *temp;
temp = malloc(sizeof(struct list_collection));
temp->current = new_combination;
//*temp->current = *new_combination;
//memcpy(temp->current, new_combination, sizeof(struct list));
/*for(c = 0; c < 2; c++)
{
temp->current[c] = new_combination[c];
temp->current[c].parent_name = new_combination[c].parent_name;
temp->current[c].chosen_child = new_combination[c].chosen_child;
temp->current[c].chosen_child->number = new_combination[c].chosen_child->number;
}*/
if (head_list == NULL)
{
head_list = temp;
head_list->next = NULL;
current_list = head_list;
}
else
{
current_list->next = temp;
current_list->next->next = NULL;
current_list = current_list->next;
}
}
void generate_list()
{
int c = 0;
struct list temp[2];
struct parent *parent_t;
struct child *head[2];
// initialize the first list
parent_t = head_parent;
while(parent_t != NULL)
{
temp[c].parent_name = parent_t->name;
temp[c].chosen_child = parent_t->head_child;
*head[c] = *parent_t->head_child;
c++;
parent_t = parent_t->next;
}
////////// Genreate all possible parent/child combinations //////////
c = 0;
//////////////////// BELOW IS WHERE THE PROBLEM RESIDES ////////////////////
/*
Problem: each time a new list is added, since each list points to "temp" instead of a
direct pointer to the original data, each time temp changes here, all the data in list
changes to what "temp" is.
*/
while(temp[c].chosen_child != NULL)
{
while(temp[c+1].chosen_child != NULL)
{
add_list(temp);
temp[c+1].chosen_child = temp[c+1].chosen_child->next;
}
temp[c].chosen_child = temp[c].chosen_child->next;
temp[c+1].chosen_child = head[c+1];
}
//////////////////// END OF PROBLEM ////////////////////
}
void initialize()
{
char other_name[15], *name;
int num;
head_parent = NULL;
head_list = NULL;
name = malloc(4);
//////////////////// Initialization of dynamic structures with data ////////////////////
////////// First parent & child dynamic structure //////////
// Parent
strcpy(name, "one\0");
add_parent(name, strlen(name) + 1);
// Child
strcpy(other_name, "first\0");
num = 1;
add_child(other_name, num);
strcpy(other_name, "second\0");
num = 2;
add_child(other_name, num);
////////// Second parent & child dynamic structure //////////
// Parent
strcpy(name, "two\0");
add_parent(name, strlen(name) + 1);
// Child
strcpy(other_name, "first\0");
num = 1;
add_child(other_name, num);
strcpy(other_name, "second\0");
num = 2;
add_child(other_name, num);
strcpy(other_name, "third\0");
num = 3;
add_child(other_name, num);
}
//////////////////////////////////////// M A I N R O U T I N E ////////////////////////////////////////
int main()
{
initialize();
generate_list();
display_list();
return 0;
}
I believe the problem resides at Line 129 where I try to copy the array of pointers to original data, but it may start in the generate() routine starting at Line 158. I commented out in the add_list() routine other methods I tried without success (Line 131, Line 133, Line 135). Normally solve coding problems within day but this has taken me almost a week now plz excuse my noobyness, and thank you in advance