繁体   English   中英

Java:单链表,虚拟节点,插入

[英]Java : singly linked list, dummy node, insert

public class A<E> extend AbstractList<E>{

private SLNode<E> Head = new SLNode<E>
private int length = 0;     // length of the list

 // I will skip the class of SLNode<E>
 // Head's element and successor is initialized as null in the class SLNode<E>

     public void add(int index, E element)  // insert an element in the list 
     {
         // if index is less than 0 or greater than the length
          if( (index < 0) || (index > length ) )
               throw new IndexOutOfBoundsException();

          if(index ==0)
          {
              SLNode<E> newnode = new SLNode<E>(element, null);  // make new node
              newnode.setSuccessor(Head.getSuccessor());  
              Head.setSuccessor( newnode);
              length++;
          }
       }

Q1。 这是在列表的最前面添加元素的正确方法吗? (使用虚拟标题节点,但没有尾部)Q2。 列表为空还是非空都一样吗?

尽管使用“虚拟”节点作为“头部”有点不寻常,但这并不是一个坏方法。

但这具有一个优势,您可以将“ Head”替换为“ current”,将其初始化为“ Head”,然后“爬网”列表index节点并进行插入,而不必对零进行特殊处理案件。

 public void add(int index, E element)  // insert an element in the list 
 {
     // if index is less than 0 or greater than the length
      if( (index < 0) || (index > length ) ) {
           throw new IndexOutOfBoundsException();
      }

      // Find insertion point
      SLNode<E> current = Head;
      for (int i = 0; i < index; i++) {
          current = current.getSuccessor();
      }

      // Create & insert new node
      SLNode<E> newnode = new SLNode<E>(element, null);
      newnode.setSuccessor(current.getSuccessor());  
      current.setSuccessor( newnode);
      length++;
  }

(但请注意,标准命名约定是将名称以大写形式保留给类名。)

假设这是您的head插入方法,我不明白您为什么需要传递索引。理想情况下,我只希望有一种插入方法。 但是由于您特别要求插入头。

public void addToHead(E element)  // insert an element at head
     {
         if(length==0)
             throw new Exception("head does not exist");

         element.setSuccessor(head);
         head = element;


         length++;

       }

Q1。 这是在列表的最前面添加元素的正确方法吗? (使用虚拟标头节点,但没有尾部)

编辑:重新阅读您的问题,并尝试您的代码后,我会说是的。

Q2。 列表为空还是非空都一样吗?

编辑:是的,它似乎在一个空列表上工作。

dummy_header_list<String> theList = new dummy_header_list<String>();
System.out.println(theList);
theList.add(0,"first add");
System.out.println(theList);
theList.add(0,"second add");
System.out.println(theList);
theList.add(0,"third add");
System.out.println(theList);

给出:

[]
[first add]
[second add,first add]
[third add,second add,first add]

与此toString:

public String toString()
{
   String output = "[";
   SLNode<E> temp = Head.getSuccessor();
   while ( temp != null ) {
      if ( output.length() == 1 ) {
         output = output + temp.toString();
      }
      else {
          output = output + "," + temp.toString(); 
      }
      temp = temp.getSuccessor();
   }
   output = output + "]";
   return output;
}

我认为您真的不需要在列表的开头插入特殊情况。 在此列表的最前面插入与在列表的其他位置插入相同。

public void add(int index, E element)
{
    // if index is less than 0 or greater than the length
    if ((index < 0) || (index > length))
        throw new IndexOutOfBoundsException();

    SLNode<E> previousNode = head;
    for ( int i = 0; i < index; i++ ) {
        previousNode = previousNode.getSuccessor();
    }

    SLNode<E> newNode = new SLNode<E>(element, previousNode.getSuccessor());
    previousNode.setSuccessor(newNode);
    length++;
}

基本上,这只是遍历列表以找到要插入的正确节点-如果索引为0,则立即停在头。 将节点插入之后,请执行以下三个步骤:

  1. 创建一个新节点并将其后继者设置为上一个节点的后继者
  2. 将上一个节点的后继者设置为新节点
  3. 在清单长度上加一

我对此进行了一些小测试,它似乎工作正常。

请注意,这种方法几乎是假定head实际上不会被视为列表中的元素。 它实际上只是一个开始列出清单的地方。 那样的话,我的意思是head.getElement()将始终返回null-它实际上不是列表的一部分。 我不确定这是否是您想要的实现,但是当您说以head元素开始列表时,即使列表应该为空,这似乎也是最有意义的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM