【问题标题】:InsertItem at Index 0 - DoublyLinkedList索引 0 处的 InsertItem - DoubleLinkedList
【发布时间】:2017-04-30 00:01:42
【问题描述】:

我目前正在创建一个使用尾递归的 DoublyLinkedList。

我已经设法让我的所有方法都完全正常工作,除了我的插入项。

它适用于在除 0 之外的任何索引处插入。我尝试编写一个处理此问题的 if 语句,但我的代码仍然运行,尽管它增加了 noOfItems。它永远不会将该项目添加到我的列表中。

问题可能出在我的 toString 上吗?还是我在 if 案例中遗漏了什么?

这是我的 DLLNode 类:

    public class DLLNode
{
    private DLLNode previous;
    public DLLNode next;
    private String value;

    public DLLNode(String value)
    {
        this.value = value;
        this.previous = previous;
        this.next = next;
    }

    public DLLNode(String value, DLLNode next, DLLNode previous)
    {
        this.value = value;
        this.next = next;
        this.previous = previous;
    }

  public String GetDataItem()
  {
    return value;
  }

  public void setDataItem()
  {
      this.value = value;
  }


  public DLLNode GetPreviousNode()
  {
    return previous;
  }

  public void setPrevious(DLLNode previous)
  {
      this.previous = previous;
  }


  public DLLNode GetNextNode()
  {
    return next;
  }

  public void setNextNode(DLLNode next)
  {
      this.next = next;
  }

  public void addItem(String value) {
     if(this.next == null) {
          DLLNode newNode = new DLLNode(value);
          this.next = newNode;
     } else {
          this.next.addItem(value);
     }
}


  public void InsertItemHelper(String value, int indexToInsert, int current, DLLNode currNode)
  {
      if (indexToInsert == 0)
      {
          DLLNode newNode = new DLLNode(value);
          currNode.GetNextNode().setPrevious(newNode);
      }
      else if (current == indexToInsert-1)
      {
          DLLNode newNode = new DLLNode(value); 
          newNode.setNextNode(currNode.GetNextNode());
          currNode.setNextNode(newNode);
          currNode.GetNextNode().setPrevious(newNode);
          newNode.setPrevious(currNode);          
      }
      else
      {
          InsertItemHelper(value, indexToInsert, current+1, currNode.GetNextNode());
      }
  }

    public void DeleteItemHelper(int indexToDelete, int current, DLLNode currNode)
  {
      if (current == indexToDelete-1)
      {
          currNode.setNextNode(currNode.GetNextNode().GetNextNode());
      }
      else
      {
          DeleteItemHelper(indexToDelete, current+1, currNode.GetNextNode());
      }
  }

}

这是我的 DoubleLinkedList 类:

    public class DoublyLinkedList
{
    private int noOfItems;
    private DLLNode head;
    private DLLNode tail;
  // Default constructor
  public DoublyLinkedList()
  {
     head = null;
     tail = null;
     this.noOfItems = 0;

  }

  public int GetNoOfItems()
  {
    return noOfItems;
  }

  public String GetItemByIndex(int index)
  {
    int count = 0;
    while (count < index)
    {
        head = head.GetNextNode();
        count++;
    }
    return head.GetDataItem();

  }

  public DLLNode GetNodeByIndex(int index)
  {
      int count = 0;
    while (count < index)
    {
        head = head.GetNextNode();
        count++;
    }
    return head;
  }

  public void AddItem(String value)
  {
      if (head == null)
      {
          DLLNode newNode = new DLLNode(value);
          head = newNode;
          noOfItems++;
      }
      else
      {
      head.addItem(value);
      noOfItems++;
      }
       }



  public void InsertItem(int index, String value)
  {
      if (index > noOfItems)
      {
          AddItem(value);
      }
      else {
        head.InsertItemHelper(value, index, 0, head); 
        noOfItems++;
      }


  }

  public void DeleteItem(int index)
  {

          if (index ==0)
          {
              System.out.println("Out of Bounds");
          }
          if (index > noOfItems)
          {
             System.out.println("Out of Bounds");
          }
          if (head == null)
          {
              System.out.println("No Item to remove");
          }
          else if (index == 1)
          {
              head = head.GetNextNode();
              noOfItems--;
          }
          else
          {
              head.DeleteItemHelper(index, 0, head);
              noOfItems--;
          }

  }

  public int getNoOfItems()
  {
      return this.noOfItems;
  }

  public boolean isEmpty()
  {
      return (head == null);
  }




  public String toString()
  {
    DLLNode currentNode = head;
    StringBuilder sb = new StringBuilder();
    while (currentNode != null) {
        sb.append(currentNode.GetDataItem());

        if (currentNode.GetNextNode() != null)
        {
            sb.append(",");
        }
        currentNode = currentNode.GetNextNode();
    }
        return sb.toString();
    }

}

我已将以下代码添加到我的插入项中:

   if (index ==0)
  {
      DLLNode newNode = new DLLNode(value);
      head.setNextNode(head);
     // newNode.next= head.GetNextNode();
      head = newNode;
      noOfItems++;
  }

如果我包含注释掉的行,我会收到与字符串生成器有关的错误。

注释掉的行添加到链表中的位置 0,但不添加任何其余部分。但是,它确实会正确增加 noOfItems。

出现以下错误:

线程“主”java.lang.OutOfMemoryError 中的异常:Java 堆空间 在 java.util.Arrays.copyOf(Arrays.java:2367) 在 java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:130) 在 java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:114) 在 java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:415) 在 java.lang.StringBuilder.append(StringBuilder.java:132) 在 ads2.DoublyLinkedList.toString(DoublyLinkedList.java:155) 在 java.lang.String.valueOf(String.java:2847) 在 java.lang.StringBuilder.append(StringBuilder.java:128) 在 ads2.Main.printList(Main.java:62) 在 ads2.Main.main(Main.java:38) Java 结果:1 构建成功(总时间:1 秒)

如果您需要有关错误的更多详细信息,请告诉我。

【问题讨论】:

  • 向我们展示完整的错误

标签: java algorithm doubly-linked-list


【解决方案1】:

在位置0添加时需要更新头部

因为你不更新头部,所以旧头部仍然链接在列表对象中,并且它的 next() 返回应该在新列表对象索引 1 中的内容,所以你最终得到了相同的列表

您可以通过确认来确认这一点

list.getNodeByIndex(0) != list.getNodeByIndex(1).previousNode();

【讨论】:

  • 在您的帮助下,我在底部的问题中添加了更多内容。如果您能提供进一步的帮助,我们将不胜感激。
  • 您好,我现在可以正常工作了,谢谢您的帮助。
猜你喜欢
  • 2021-02-01
  • 1970-01-01
  • 2021-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-23
  • 2018-04-04
相关资源
最近更新 更多