【问题标题】:Inserting data in a sorted Circular Linked List在排序的循环链表中插入数据
【发布时间】:2012-09-19 07:41:52
【问题描述】:

我想将给定的数据添加到已经排序的循环链接列表中,以便结果列表也被排序。已经提供了Node 的类,其中public int datapublic Node next 作为类成员。

将实现一个函数addNode(Node head),它将一个已知数据(9) 插入到列表中。 Node head是循环链表的头指针。

我考虑过以下案例

  1. 当列表为空时,创建节点,将其数据设置为 9 并将其 next 引用到自身。将新创建的节点作为头部。

  2. 当列表仅包含一项时。修改第一个节点的next指针指向新节点,新节点的next指针指向给定的头节点。让头节点指向值最低的节点。

  3. 当插入的数据是所有数据中最小的,即小于头节点所指向的节点的数据时,会被插入到头节点之前。

  4. 在两个节点之间插入数据时。所以我正在使用while循环,它将找到将插入新数据的节点并相应地修改节点的next指针。

当我提交代码时,它以某种方式失败了一个我无法找到的测试用例。有人可以帮助我找出我的逻辑中可能忽略的条件。

下面是实现的代码:

public static Node addElement(Node input1)
{
    //Write code here
Node result = new Node();
Node current = new Node();
current = input1;

Node value = new Node();
value.data = 10;

if(current == null){
    value.next = value;
    result = value;
}
else if(current.next == current){
    value.next = input1;
    current.next = value;
    result = current.data < value.data ? current : value;
}
else if(value.data < current.data){
    while(current.next != input1)
        current = current.next;     

    current.next = value;
    current.next.next = input1;
    result = current.next;
}   
else{
    while(current.next != input1 && current.next.data <= value.data)
        current = current.next;

    Node currentNext = current.next;
    current.next = value;
    current.next.next = currentNext;
    result = input1;
}

return result;
}

【问题讨论】:

  • 你是什么意思它失败了一个你找不到的测试用例?是否有一组特定的数据失败了?
  • @Christina 我想这是由许多 JUnit 测试检查的作业。看起来 OP 自己不知道哪个测试失败了。
  • 我认为你的情况2的逻辑有错误。如果你的新节点是最低值怎么办?您说新节点指向头部,头部指向具有最低值的节点。然后,您将丢失对旧节点的引用。编辑:scratch 其余部分,我刚刚看到 input1 应该是 head node
  • 这很奇怪,我自己实现并做了一些手动测试。然后我拿了你的代码并做了同样的测试。结果是相同的。所有基本操作都应该正常工作。有没有可能你没有考虑过的事情。例如,列表是否应该接受重复项?如果是,它们应该按什么顺序排列。是否应该在现有元素之前或之后插入副本。您确定您的数据是9 还是10 并不重要?如果检查订单或检查列表中的特定数字,则测试可能会失败。否则我看不出问题。
  • 作为旁注,您并不需要考虑所有这些情况。您真正需要的唯一情况是 1. list empty,2. list not empty(这实际上会将上面的案例 2-4 合并在一起)。也许通过最小化您尝试处理的情况并简化代码,您会发现我们现在没有看到的东西(值得我在上面的代码中也看不到任何错误)。

标签: java linked-list circular-list


【解决方案1】:
void sortedInsert(struct node** head_ref, struct node* new_node)
{
  struct node* current = *head_ref;

  // Case 1 of the above algo
  if (current == NULL)
  {
     new_node->next = new_node;
     *head_ref = new_node;
  }

  // Case 2 of the above algo
  else if (current->data >= new_node->data)
  {
    /* If value is smaller than head's value then
      we need to change next of last node */
    while(current->next != *head_ref)
        current = current->next;
    current->next = new_node;
    new_node->next = *head_ref;
    *head_ref = new_node;
  }

  // Case 3 of the above algo
  else
  {
    /* Locate the node before the point of insertion */
    while (current->next!= *head_ref && current->next->data < new_node->data)
      current = current->next;

    new_node->next = current->next;
    current->next = new_node;
  }
}

【讨论】:

  • 请考虑包含一些关于您的答案的信息,而不是简单地发布代码。我们试图提供的不仅仅是“修复”,而是帮助人们学习。您应该解释原始代码中的问题、您所做的不同之处以及您的更改为何有效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-23
相关资源
最近更新 更多