代码之家  ›  专栏  ›  技术社区  ›  Walter

添加到列表功能

  •  0
  • Walter  · 技术社区  · 7 年前

    我试图编写一个函数,在列表的末尾插入节点。

    问题是列表最后一个节点的指针没有指向NULL,如果我显示列表,就会从系统中得到一个错误。

    struct node{
        int value;
        struct node *next;
    };
    
    struct node *AddToList (struct node *list, int n);
    
    int main()
    {
        struct node *node;
        node = AddToList(node, 30);
        node = AddToList(node, 20);
        node = AddToList(node, 10);
        showlist(node);
        return 0;
    }
    
    struct node *AddToList (struct node *list, int n){
        struct node *new_node;
        new_node=malloc(sizeof(struct node));
        new_node->value=n;
        new_node->next=list;
        return new_node;
    };
    
    3 回复  |  直到 7 年前
        1
  •  3
  •   user2736738    7 年前

    是的,这是因为您插入的第一个节点 next 具有价值 NULL .

    struct node *node = NULL; //<------
    node = AddToList(node, 30);
    node = AddToList(node, 20);
    node = AddToList(node, 10);
    showlist(node);
    

    这将解决问题。这样做的结果是,第一次插入节点时 下一个 将被指定值 无效的 . 因为第一次打电话给 AddToList 这个 list 无效的 .

    你做这件事的方式- node 包含某个不确定值(垃圾值)( 节点 是一个具有自动存储持续时间的变量),然后将其添加为 link 到第一个节点。这没有实际用途,因为现在您无法遍历列表,认为您将找到 无效的 值,该值是您应该停止的时间。

    来自标准章节 §6.7.9

    如果未初始化具有自动存储持续时间的对象 显然,它的值是不确定的。


    您应该检查 malloc . 万一失败了,你应该处理这个案子。并在使用完后释放动态分配的内存。

    也不确定您是如何尝试显示该列表的,但如果是在假设最后一个节点将指向 无效的 然后开始在其上循环-然后您在代码中获得了一个未定义的行为。

        2
  •  1
  •   Vlad from Moscow    7 年前

    '我试图编写一个函数,在 列表

    此函数

    struct node *AddToList (struct node *list, int n){
        struct node *new_node;
        new_node=malloc(sizeof(struct node));
        new_node->value=n;
        new_node->next=list;
        return new_node;
    };
    

    不在列表末尾插入节点。它将在列表开头的头节点之前插入一个节点。

    在列表末尾插入节点的函数可以如下所示。

    int AddToList ( struct node **list, int n )
    {
        struct node *new_node = malloc( sizeof( struct node ) );
        int success = new_node != NULL; 
    
        if ( success )
        {
            new_node->value = n;
            new_node->next = NULL;
    
            while ( *list != NULL ) list = &( *list )->next;
    
            *list = new_node;
        }
    
        return success;
    }
    

    函数的调用方式如下

    struct node *head = NULL;
    
    AddToList( &head, 10);
    AddToList( &head, 20);
    AddToList( &head, 30);
    

    如果需要,列表值的顺序将是10、20、30。

    问题是列表最后一个节点的指针 指向空值

    因为新节点是在第一个未初始化的节点之前插入的

    struct node *node;
    

    因此有一个不确定的值,列表中的最后一个节点不指向NULL。

    必须将初始指针设置为NULL。

    struct node *node = NULL;
    

    考虑到根据C标准,无参数的主要功能应声明如下

    int main( void )
    
        3
  •  -1
  •   K.Vani    7 年前

    当用户想在最后一次添加节点时,首先检查它是否为空列表,如果是,则表示将新节点添加为头节点。如果列表不是空的,请查看列表并通过检查tp找出最后一个节点->next=空,然后将新节点的地址存储在tp中->next并将新节点的下一个字段设为NULL。以下程序清楚地显示了该概念

    #include<stdio.h>
    #include<malloc.h>
    int item,count,pos;
    struct node
      {
       int info;
       struct node *next;
       }*head,*tp;
     void traversal()
          {
             if(head==NULL)
                printf("\n List is empty");
             else
                {
                  tp=head;
                  while(tp->next!=NULL)
                    {
                      printf("\n%d",tp->info);
                      tp=tp->next;
                    }
                  printf("\n%d",tp->info);
                }
          }
      void insertlast()
          {
            struct node *temp;
            temp=(struct node*) malloc(sizeof(temp));
            printf("\nEnter the element:");
            scanf("%d",&item);
            temp->info=item;
            temp->next=NULL;
            if(head==NULL)
               head=temp;
            else
             {
                tp=head;
                while(tp->next!=NULL)
                 {
                   tp=tp->next;
                 }
                tp->next=temp;
              }
        }
    int main()
    {
      printf("\n\t\t**********SINGLY LINKED LIST********");
      printf("\n\t------------------------------------------------------");
      printf("\n\tInsertion last:");
      printf("\n\t---------------------");
      insertlast();
      traversal();
      printf("\n\tInsertion last:");
      printf("\n\t---------------------");
      insertlast();
      traversal();
      printf("\n\n\tInsertion last:");
      printf("\n\t---------------------");
      insertlast();
      traversal();
      printf("\n\n\tInsertion last:");
      printf("\n\t---------------------");
      insertlast();
      traversal();
      return 0;
    }
    

    输出

        **********SINGLY LINKED LIST********                                                                                                                                 
        ------------------------------------------------------                                                                                                                       
                Insertion last:                                                                                                                                                             
             ---------------------                                                                                                                                                        
    Enter the element:12                                                                                                                                                                         
    
    12                                                                                                                                                                                           
           Insertion last: 
         ---------------------                                                                                                                                                        
    Enter the element:13                                                                                                                                                                         
    
    12                                                                                                                                                                                           
    13                                                                                                                                                                                           
    
            Insertion last:                                                                                                                                                              
          ---------------------                                                                                                                                                        
    Enter the element:14          
    
    12                                                                                                                                                                                           
    13                                                                                                                                                                                            
    14                                                                                                                                                                                           
    
             Insertion last:
          ---------------------                                                                                                                                                        
     Enter the element:15                                                                                                                                                                         
    
    12                                                                                                                                                                                           
    13                                                                                                                                                                                           
    14                                                                                                                                                                                           
    15                                                                                                                                                                                           
    

    希望你能理解。非常感谢。