So what I want to do in brief is to create a structure, fill it in, and after that create a linked list. What I have done so far is this: I have defined a linked list as below. Then I have filled it in with about 40 variables for PASSENGERS
typedef struct list1
{
char var1[10];
char var2[10];
struct list1 *next;
}LIST1;
Then I have dynamically allocated some memory to it (to host for example 40 structure items)
list = (LIST1 *) malloc ((40)*sizeof(LIST1));
Filled it in with about 40 variables for PASSENGERS by using for example
for (i=0;i<40;i++)
{
strcpy(list[i].var1,"AAAAAAAA");
strcpy(list[i].var2,"BBBBBBBB");
}
And what I need to do now is connect all these values, that are already there in a linked list, and print the result by using this linked list.
I am trying something like :
LINK1 *link, *start=NULL, *tmp;
for (i=0;i<40;i++)
{link->next = NULL;
if (start==NULL)
start = link;
else{
tmp=start;
while (tmp->next !=NULL) tmp=tmp->next;
tmp->next=link;
}
}
and then to print the result of var1
while (tmp!=NULL)
{
printf("%s",tmp->var1);
tmp=tmp->next;
}
The code runs, but it doesn't print out anything. This is just a part of an exercise I am trying to solve, and I started with something simple to see how it works. I don't want to allocate memory only for one element at a time. I am asked to allocate all the memory necessary, fill in the structure and then create the linked list.
malloclink->next = NULL;the local variableLINK1 *linkis uninitialised.