views:

80

answers:

1

My Struct Definitions.

typedef struct inner_list {char word[100]; inner_list*next;} inner_list;
typedef struct outer_list
{ char word [100];
inner_list * head;
outer_list * next; } outer_list;

And The problem part:

void append(outer_list **q,char num[100],inner_list *p)
{    outer_list *temp,*r;
     temp = *q;

     char *str;
     if(*q==NULL)
     {   temp = (outer_list *)malloc(sizeof(outer_list));
          strcpy(temp->word,num);
          temp->head = p;
          temp->next=NULL;
          *q=temp;
     }
     else
     {  temp = *q;
         while(temp->next !=NULL)
         {  temp=temp->next;
         }
         r = (outer_list *)malloc(sizeof(outer_list));
         strcpy(r->word,num);
         temp->head = p;
         r->next=NULL;
         temp->next=r;
     }
}

I don't know what is i'm doing wrong in this append function i'm sending a char array and a linked list to be stored another linked list. But i can't store the linked list in another linked list. I couldn't figure out the problem. Any ideas?

A: 

In the else clause, where your code says

temp->head = p;

It should say:

r->head = p;

r is the newly created node, so you want to set that node's head. What you are doing instead is overwriting an existing node's head field.

interjay
perfect man. I must be blind or sth. Thanks so much. 8 hours coding made me blind i guess :D. Thanks so much
LuckySlevin