在C

时间:2016-02-19 04:18:32

标签: c linked-list nodes

我正在制作一个程序,在列表的开头,中间和结尾插入和删除节点。这就是我提出的,但每次尝试添加节点时它都会崩溃。任何类型的帮助将不胜感激

 #include <stdio.h>
 #include <stdlib.h>
 #include <conio.h>
 #include <ctype.h>
 #include <string.h>

 struct node {
    char name[20];
    int  leg;
    struct node *next;
 };

 struct node *head = (struct node *) NULL;
 struct node *end = (struct node *) NULL;
 struct node * initnode( char *name, int leg ){
    struct node *ptr;
    ptr = (struct node *) calloc( 1, sizeof(struct node ) );
    if( ptr == NULL )                       
        return (struct node *) NULL;        
    else {                                 
        strcpy( ptr->name, name );         
        ptr->leg = leg;                      
        return ptr;                        
    }
 }

 void printnode( struct node *ptr ){
    printf("Name ->%s\n", ptr->name );
    printf("legs   ->%d\n", ptr->leg );
 }



 void printlist( struct node *ptr ){
     while( ptr != NULL ){
        printnode( ptr );          
        ptr = ptr->next;            
     }
 }


 void add( struct node *new ){
     if( head == NULL )      
        head = new;         
     end->next = new;        
     new->next = NULL;       
     end = new;              
 }


 struct node * searchname( struct node *ptr, char *name ){
     while( strcmp( name, ptr->name ) != 0 ) {    
         ptr = ptr->next;                          
         if( ptr == NULL )                         
             break;                                
      }
     return ptr;                                  
 }                                                


 void deletenode( struct node *ptr ){
      struct node *temp, *prev;
      temp = ptr;    
      prev = head;  

      if( temp == prev ) {                   
          head = head->next;                  
          if( end == temp )                   
              end = end->next;                 
         free( temp );                       
      }
      else {                                  
         while( prev->next != temp ) {       
         prev = prev->next;              
         }
         prev->next = temp->next;            
         if( end == temp )                   
             end = prev;                     
         free( temp );                       
      }
  }


  void insertnode( struct node *new ){
      struct node *temp, *prev;               

      if( head == NULL ) {                     
          head = new;                         
          end = new;
          head->next = NULL;                   
          return;                              
      }

      temp = head;                            

      while( strcmp( temp->name, new->name) < 0 ) {
          temp = temp->next;              
          if( temp == NULL )               
              break;
      }


      if( temp == head ) {
          new->next = head;             
          head = new;                  
      }
     else {     
         prev = head;   
         while( prev->next != temp ) {
            prev = prev->next;
         }
    prev->next = new;            
    new->next = temp;
    if( end == prev )             
        end = new;                
   }
 }


 void deletelist( struct node *ptr ){
     struct node *temp;

     if( head == NULL ) 
         return;   

     if( ptr == head ) {     
         head = NULL;         
         end = NULL;                                          
     }
     else {
         temp = head;          
         while( temp->next != ptr )        
             temp = temp->next;
         end = temp;                        
     }

     while( ptr != NULL ) {   
     temp = ptr->next;     
     free( ptr );          
     ptr = temp;           
     }
   }


main(){
  char name[20];
  int leg, ch = 1;
  struct node *ptr;

  system("cls");
  while( ch != 0 ) {
     printf("1 add an animal \n");
     printf("2 delete a node \n");
     printf("3 list all nodes \n");
     printf("4 search for animal \n");
     printf("5 insert a node \n");
     printf("0 quit\n");
     scanf("%d", &ch );
     switch( ch ){
      case 1:  
               printf("Enter animal name -- ");
               scanf("%s", &name );
               printf("how many legs? -- ");
               scanf("%d", &leg );
               ptr = initnode( name, leg );
               add( ptr );
               break;
      case 2:  
               printf("Enter in animal -- ");
               scanf("%s", name );
               ptr = searchname( head, name );
               if( ptr ==NULL ) {
                   printf("animal %s not found\n", name );
               }
               else
                  deletenode( ptr );
               break;

      case 3:  
               printlist( head );
               break;

      case 4:  
               printf("Enter in animal -- ");
               scanf("%s", name );
               ptr = searchname( head, name );
               if( ptr ==NULL ) {
                   printf("animal %s not found\n", name );
               }
               else
                  printnode( ptr );
               break;
      case 5:  
               printf("Enter in animal -- ");
               scanf("%s", name );
               printf("Enter in legs -- ");
               scanf("%d", &leg );
               ptr = initnode( name, leg );
               insertnode( ptr );
               break;

      }
   }
   deletelist( head );
}

1 个答案:

答案 0 :(得分:1)

enter codevoid add( struct node *new ){
     if( head == NULL )      
        head = new;         
     end->next = new;        
     new->next = NULL;       
     end = new;              
}

你想使用&#39; else&#39;与&#39;如果&#39;。 当它为NULL时,它确保你没有取消引用结束指针。 (在第一次调用添加功能时)

void add( struct node *new ){
     if( head == NULL )      
        head = new;
     else  // Change    
        end->next = new;        
     new->next = NULL;       
     end = new;              
 }

我试了一下。通过此更改,添加&#39;和&#39;列出&#39;工作得很好。