将节点添加到链表时,它会覆盖前一个节点

时间:2018-10-19 09:00:56

标签: c

在添加了几个具有不同字符串的节点后,print()仅输出最后插入的节点的值。如果我插入了10个不同的节点,而最后一个节点包含“ FirstString”和“ SecondString”,它将打印出10次。我假设我的insertLast出问题了,它用新的覆盖了所有先前的节点。

LinkedList* newLinkedList()                                                     
{                                                                               
   LinkedList* list;                                                            
   list = (LinkedList*)malloc(sizeof(LinkedList));                              
   (*list).head = NULL;                                                         
   (*list).tail = NULL;                                                         
   return list;                                                                 
} 



void insertLast( struct LinkedList* list, char* inCommand, char* inValue )
{                                                                               
   LinkedListNode* newNode;                                                     

   printf( "Command:%s Value:%s\n", inCommand, inValue );                       
   newNode = (LinkedListNode*)malloc(sizeof(LinkedListNode));                   
   (*newNode).command = malloc( 10 * sizeof( char ) );                          
   (*newNode).value = malloc( 3 * sizeof( char ) );                             
   (*newNode).command = inCommand;                                              
   (*newNode).value = inValue;                                                  
   newNode->next = NULL;                                                        

   if( list->head == NULL )                                                     
   {                                                                            
      list->head = newNode;                                                     
      list->tail = newNode;                                                     

   }                                                                            
   else                                                                         
   {                                                                            
      list->tail->next = newNode;                                               
      list->tail = newNode;                                                     
   }                                                                            
   printf( "Start:%s %s \n", list->head->command, list->head->value );          
   printf( "End:%s %s \n", list->tail->command, list->tail->value );            

}
void print( struct LinkedList* list )                                           
{                                                                               
   LinkedListNode* current = list->head;                                        
   while( current!= NULL )                                                      
   {                                                                            
      printf( "\n%s : %s \n", current->command, current->value );
      current = current->next;
   }                                                                            
}

2 个答案:

答案 0 :(得分:1)

将我的insertLast()更改为使用strcpy,现在看来工作正常。 :)感谢您的答复。

void insertLast( struct LinkedList* list, char* inCommand, char* inValue )
{                                                                               
   LinkedListNode* newNode;                                                     

   newNode = (LinkedListNode*)malloc(sizeof(LinkedListNode));                   
   newNode->command = malloc( 10 * sizeof( char ) );                            
   newNode->value = malloc( 3 * sizeof( char ) );                               
   strcpy( newNode->command, inCommand );                                       
   strcpy( newNode->value, inValue );                                           
   newNode->next = NULL;                                                        

   if( list->head == NULL )                                                     
   {                                                                            
      list->head = newNode;                                                     
      list->tail = newNode;                                                     

   }                                                                            
   else                                                                         
   {                                                                            
      list->tail->next = newNode;                                               
      list->tail = newNode;                                                     
   }                                                                            
}

答案 1 :(得分:-2)

尝试使用

代替(*newNode).command = inCommand
memcpy(newNode->command, inCommand, strlen(inCommand));
newNode->command[strlen(inCommand)] = 0;

这将复制该值,而不是指向与inCommand相同的值。

因此,如果您在主代码中使用相同的变量为inCommand分配值,则按照您现在的方式,所有节点都将指向相同的值。每当您更改该变量的值时,它们都会全部更改!