索引0处的InsertItem - DoublyLinkedList

时间:2016-12-14 15:28:50

标签: java algorithm doubly-linked-list

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

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

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

问题可能出在我的toString上吗?或者我在if case中遗漏了什么?

这是我的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());
      }
  }

}

这是我的DoublyLinkedList类:

    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的链接列表中添加,但不会添加任何其余内容。但它确实正确地增加了noOfItem。

出现以下错误:

线程“main”中的异常java.lang.OutOfMemoryError:Java堆空间     at java.util.Arrays.copyOf(Arrays.java:2367)     at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:130)     at 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)     at 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秒)

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

1 个答案:

答案 0 :(得分:0)

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

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

enter image description here

您可以通过确认

来确认

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