2016-12-22 83 views
0

这是我的第一个问题! 我使用链接列表和函数。指针,链表和函数

我已经创建了这个函数,它将结构(Passenger)中的值复制到链表LIST1。

typedef struct 
    { 
    char fillname[40] 
    }PASSENGERS; 


typedef struct list1 
    { 
    char fullname[40]; 
    struct list1 *next; 
    }LIST1; 



    //COPYLIST 

copylist(LIST1 *list1, PASSENGERS *passenger) 
    { 

    LIST1 *start=NULL; 
    for (i=0;i<40;i++) 
     { 
     list1 = (LIST1 *) malloc (sizeof(LIST1)); 
     list1->next = NULL; 
     strcpy(list1->fullname,passenger[i].fullname); 

     if (start ==NULL) 
      start = list1; 
     else //add new node at the beginning of list 
      { 
      list1->next = start; 
      start = list1; 
      } 
     } 
    } 

里面主要我调用该函数用下面的语句

int main() 
PASSENGERS *passenger; 
int h; 

LIST1 *list1; 
list1=copylist(list1,passenger); 

但是我在印刷时得到什么:

LIST1 *current = list1; 


    while (current !=NULL) 
     { 
     printf("%s",current->fullname); 
     current = current->next; 

如果我不使用的功能和移动主一切内的代码工作正常,所以可能这是一个指针问题,我仍然试图使用! 谢谢

+1

你没回从copylist()函数的任何信息。 –

回答

1

修改您的copylist功能像: -

LIST1 *copylist(LIST1 *list1, PASSENGERS *passenger) 
    { 

     LIST1 *start=NULL; 
     int i=0; 
     for (i=0;i<40;i++) 
      { 
      list1 = (LIST1 *) malloc (sizeof(LIST1)); 
      list1->next = NULL; 
      strcpy(list1->fullname,passenger[i].fullname); 

      if (start ==NULL) 
       start = list1; 
      else //add new node at the beginning of list 
       { 
       list1->next = start; 
       start = list1; 
       } 
      } 
      return start; 
    }