【问题标题】:Add to list function添加到列表功能
【发布时间】:2018-06-29 13:58:06
【问题描述】:

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

问题是列表的最后一个节点的指针不指向 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;
};

【问题讨论】:

    标签: c list pointers linked-list singly-linked-list


    【解决方案1】:

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

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

    这将解决问题。现在作为这样做的结果 - 第一次插入节点时,它的 next 将被分配值 NULL。因为第一次调用AddToList 时,listNULL

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

    来自标准章节§6.7.9

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


    您应该检查malloc 的返回值。万一失败 - 你应该处理这种情况。并在使用完动态分配的内存后释放它。

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

    【讨论】:

    • @Walter.: 因为那将是最后一个节点的下一个值。拿起铅笔和纸,一步一步地画出来。
    • @Walter.:你明白了吗?
    【解决方案2】:

    '正在尝试编写一个在末尾插入节点的函数 列表。

    这个函数

    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。

    问题是链表最后一个节点的指针没有 指向NULL

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

    struct node *node;
    

    因此列表中最后一个节点的值不确定。

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

    struct node *node = NULL;
    

    考虑到根据 C 标准,不带参数的函数 main 应声明为

    int main( void )
    

    【讨论】:

      【解决方案3】:

      当用户想在最后添加节点时,首先检查它是否为空列表,如果是,则意味着将新节点添加为头节点。如果列表不为空,则遍历列表并通过检查 tp- 找出最后一个节点>next=NULL 然后将新节点的地址存储在 tp->next 中,并将新节点的 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                                                                                                                                                                                           
      

      希望你能理解。谢谢

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-01-04
        • 2018-12-11
        • 2017-08-18
        • 1970-01-01
        • 2012-11-16
        • 2020-04-17
        • 2012-04-24
        相关资源
        最近更新 更多