簡體   English   中英

如何刪除鏈表中所有出現的項目?

[英]how to delete all occurences of an item in a linked list?

我是鏈表的初學者,這是我第一次嘗試編寫一個方法調用 removeall(N),它將刪除列表中某個元素的所有出現。 我遍歷列表,如果我看到一個等於 n 的元素,我會刪除鏈接,但是我收到一條錯誤消息

 Exception in thread "main" 22 at LinkList.removeAll(LinkList.java:34)
java.lang.NullPointerException 

有人可以解釋一下我做錯了什么嗎?

鏈接.java

class Link
   {
   public long iData;              // data item
   public Link next;              // next link in list
// -------------------------------------------------------------
   public Link(long id) // constructor
      {
      iData = id;                 // initialize data
                       // ('next' is automatically
      }                           //  set to null)
// -------------------------------------------------------------
   public void displayLink()      // display ourself
      {
      System.out.print("{" + iData  + "} ");
      }
   }  // end class Link

鏈表.java

class LinkList
{
    private Link first;            // ref to first item on list
    // -------------------------------------------------------------
    public LinkList()              // constructor
    { 
        first = null; }           // no items on list yet
    // -------------------------------------------------------------
    public boolean isEmpty()       // true if list is empty
    { 
        return (first==null); }
    // -------------------------------------------------------------
    public void insertFirst(long dd) // insert at start of list
    {                           // make new link
        Link newLink = new Link(dd);
        newLink.next = first;       // newLink --> old first
        first = newLink;            // first --> newLink
    }
    // -------------------------------------------------------------
    //public long deleteFirst()      // delete first item
    public Link deleteFirst()      // delete first item
    {                           // (assumes list not empty)
        Link temp = first;          // save reference to link
        first = first.next;         // delete it: first-->old next
        return temp;          // return deleted link
    }

    public void removeAll(int n) {

          Link current = first;       // start at beginning of list
          while(current != null)  {
              current = current.next; 

              if(current.iData==n) {
                  current.next=current.next.next;



              }




          }

    }
    // -------------------------------------------------------------
    public void displayList()
    {
        System.out.print("List (first-->last): ");
        Link current = first;       // start at beginning of list
        while(current != null)      // until end of list,
        {
             System.out.print("List (first-->last): "+ current.iData);    // print data
            current = current.next;  // move to next link
        }
        System.out.println("");
    }
    // -------------------------------------------------------------
}  // end class LinkList

以下是您的 removeAll 實施中的一些問題:

  1. if(current.iData==n)檢查當前列表項的值但current.next=current.next.next; 刪除下一項。
  2. 您不處理列表開頭的匹配項。

嘗試以下 removeAll 方法的實現:

        public void removeAll(int n) {
            // handling elements from the beginning
            while(first != null && first.iData == n){ 
                first = first.next;
            }

            // Removing matched elements from the rest of the list
            Link current = first;
            while (current != null && current.next != null ) {
                if (current.next.iData == n) {
                    current.next = current.next.next;
                } else {
                    current = current.next;
                }
            }
        }

暫無
暫無

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

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