【问题标题】:Singly list insert function in c在c中单独列出插入函数
【发布时间】:2016-01-03 18:34:12
【问题描述】:

我正在尝试为简单的链表编写一些函数。这个应该在列表之间的某处添加一个节点。用户放入列表的头部,此时应该保存的数据“c”和应该保存的索引“x”。但它基本上什么都不做,我不知道为什么。也许有人能看出错误。

第一个 if 语句只是检查索引是否是列表的一部分。

DoubleNode *insert(DoubleNode *head, double c, int x)
{
    int i;
    DoubleNode *cursor, *tmp, *newl;
    cursor = head->next;
    newl = head;
    if (x > Index(newl) || x < 1)
    {
        printf("FAIL\n");
        return NULL;
    }
    else {
        while (cursor != NULL)
        {
            newl->next = cursor;
            cursor = cursor->next;
            if (i == x)
            {
                tmp = malloc(sizeof(DoubleNode));
                if (tmp == NULL)
                {
                    printf("Fail");
                    return NULL;
                }
                tmp->data = c;
                tmp->next = cursor;
                cursor = tmp;
            }
            i++;
        }
    }
    return newl;
}

【问题讨论】:

  • 我卡在while (cursor != NULL)cursor 对空列表有什么价值?
  • Index(newl) 是做什么的?
  • 函数 Index(newl) 是更大程序的一部分,它只计算列表中有多少节点。所以索引不能大于列表中的元素。

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


【解决方案1】:

声明

tmp->data=c;
tmp->next=cursor;
cursor=tmp;

实际上并没有将节点tmp 添加到列表中。你需要做例如

tmp->data = c;
tmp->next = cursor->next
cursor->next = tmp;
break;  // No need to loop more, we've inserted the node

【讨论】:

    【解决方案2】:

    首先你必须找到索引为 x 的节点的前驱。分配一个新节点并将其插入到找到的前驱节点的下一个节点。

    DoubleNode* insert(DoubleNode*head, double c, int x)
    {
        if ( x<1 )
        {
            printf("FAIL\n");
            return NULL;
        }
    
        // Find predecessor, start with head
        DoubleNode *pred= head;
        int i = 1;
        while ( i < x && pred->next != NULL )
        {
            pred = pred->next;
            i ++;
        }
    
        if ( i < x )
        {
            // list is to short
            printf("Fail");
            return NULL;
        }
    
        // allocate new node and insert it after predecessor
        DoubleNode *newl = malloc(sizeof(DoubleNode));
        newl->data = c;
        newl->next = pred->next; // successor of new node is successor of predecessor
        pred->next = newl; // successor of predecessor is now the new node
    
        return newl;
    }
    

    【讨论】:

      【解决方案3】:

      感谢您的提示。它现在正在工作。我想返回一个包含新节点的完整列表的新头。抱歉,我没有提到这个。

      DoubleNode*insert(DoubleNode*head, double c, int x)
      {
          DoubleNode *pred, *tmp;
              pred=head;
              tmp=head;
              int i = 1;
              while ( i < x && pred->next != NULL )
              {
                  pred = pred->next;
                  tmp->next=pred;
                  i ++;
              }
      
              if ( i < x )
              {
                  printf("Fail");
                  return NULL;
              }
              DoubleNode *newl = malloc(sizeof(DoubleNode));
              newl->data = c;
              newl->next = pred->next; 
              pred->next = newl; 
      
              return tmp;
      }
      

      【讨论】:

      • 我猜不需要看这个。一个简单的谢谢你接受正确的答案(和其他对你有帮助的答案)
      • 对不起,这里有点新。想如果有同样问题的人来这里想看看我是怎么结束的,不必问同样的问题。
      猜你喜欢
      • 2020-08-27
      • 2014-06-28
      • 1970-01-01
      • 2023-03-27
      • 2011-08-03
      • 1970-01-01
      • 2018-04-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多