簡體   English   中英

從單個鏈接列表中刪除特定節點

[英]Removing a specific node from a single linked list

昨天我發布了一個有關該程序重寫toString()的問題,但是現在我遇到了另一個問題。 應該使用removeItem()方法刪除具有給定數據值(在這種情況下為字符串名稱)的節點。 我在第64行上收到NullPointerException,無論出於什么原因,我似乎都無法弄清楚。 我的代碼在下面,在此先感謝您的幫助。

public class StudentRegistration<E>
{
    private static class Node<E> 
    {

        /** The data value. */
        private E data;
        /** The link */
        private Node<E> next = null;

        /**
         * Construct a node with the given data value and link
         * @param data - The data value 
         * @param next - The link
         */
        public Node(E data, Node<E> next) 
        {
            this.data = data;
            this.next = next;
        }

        /**
         * Construct a node with the given data value
         * @param data - The data value 
         */
        public Node(E data) 
        {
          this(data, null);
        }

        public Node getNext()
        {
          return next;
        }

        public E getData()
        {
          return data;
        }

        public void setNext(Node append)
        {
          next = append;
        }
    }
    /** A reference to the head of the list */
    private Node<E> head = null;
    /** The size of the list */
    private int size = 0;

    /** Helper methods */
    /** Remove the first occurance of element item.
    @param item the item to be removed
    @return true if item is found and removed; otherwise, return false.
*/
  public void removeItem(E item)
  {
    Node<E> position = head;
    Node<E> nextPosition1,
            nextPosition2;

    while (position != null)
    {
      if(position.getNext().getData() == item)         //NullPointerException
      {
        nextPosition1 = position.getNext();
        nextPosition2 = nextPosition1.getNext();
        position.setNext(nextPosition2);
      } 
      else
      {
        position = position.getNext();  
      }
    }   
  } 

 /** Insert an item as the first item of the list.
   * @param item The item to be inserted
   */
  public void addFirst(E item) 
  {
      head = new Node<E>(item, head);
      size++;
  }

   /**
     * Remove the first node from the list
     * @returns The removed node's data or null if the list is empty
     */
  public E removeFirst() 
  {
      Node<E> temp = head;
      if (head != null) 
      {
          head = head.next;
      }
      if (temp != null) 
      {
          size--;
          return temp.data;
      } else 
      {
          return null;
      }
  }
  /** Add a node to the end of the list
    *@param value The data for the new node
    */
  public void addLast(E value)
  {
    // location for new value
    Node<E> temp = new Node<E>(value,null);
    if (head != null)
    {
      // pointer to possible tail
      Node<E> finger = head;
      while (finger.next != null)
      {
        finger = finger.next;
      }
      finger.setNext(temp);
    } else head = temp;
  } 

  @Override
  public String toString()
  {
    StringBuilder sb = new StringBuilder();
    sb.append("[");
    Node<E> aux = this.head;
    boolean isFirst = true;
    while(aux != null)
    {
      if(!isFirst)
      {
        sb.append(", ");
      }
      isFirst = false;
      sb.append(aux.data.toString());
      aux=aux.next;
    }
  return sb.append("]").toString();
  } 
}

除非您練習過“可視化”頭腦中的數據結構,否則要了解情況的好方法是拿一張紙,並畫一個“框和指針”圖來表示數據結構中的節點(以及相關的字段)...圖表中的局部變量。 然后使用鉛筆和橡皮1進行“手動執行”。

不用擔心 眾所周知,鏈表的插入和刪除對於初學者來說是棘手的。 (這就是為什么在入門Java和算法類中通常將其設置為類練習的原因。)


1-請注意避免使用國際英語faux-pas :-)

到達終點時會有一個例外,沒有下一個值。 您應該像這樣檢查:

while (position.getNext() != null)

還可以使用equals()代替==運算符:

if(position.getNext().getData().equals(item))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM