简体   繁体   中英

What is wrong with this append func in C

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM