简体   繁体   English

这个附加函数在C中有什么问题

[英]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. 我不知道我在这个追加函数中做错了什么,我要发送一个char数组和一个链表以存储另一个链表。 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 在else子句中,您的代码说

temp->head = p;

It should say: 应该说:

r->head = p;

r is the newly created node, so you want to set that node's head . r是新创建的节点,因此您要设置该节点的head What you are doing instead is overwriting an existing node's head field. 相反,您正在做的是覆盖现有节点的head字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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