簡體   English   中英

如何刪除雙鏈表中的特定條目?

[英]How to delete a specific entry in double linked list?

它向我顯示“線程“主”中的異常java.lang.NullPointerException”,並且錯誤出現在“ instead.prev.next =相反。next;”中 這里。

這是我寫的全部代碼

import javax.xml.soap.Node;

public class LinkedSet<T> {
private class DLNode {
    public T data;
    public DLNode prev, next;

    // Can add constructors and other methods
    public DLNode(T dataValue, DLNode PREV, DLNode NEXT) {
        data = dataValue;
        prev = PREV;
        next = NEXT;
    }
}

private DLNode head;
private int numberOfEntries;
private boolean initialized = false;

public LinkedSet() {
    head = null;
    numberOfEntries = 0;
    initialized = true;
}
// Add at beginning of linked list
public boolean add(T newEntry) {
    checkInitialization();
    DLNode doubleLinked = new DLNode (newEntry, null,head);


    head = doubleLinked;
    numberOfEntries++;
    return true;
}
public T[] toArray() {
    @SuppressWarnings("unchecked")
    T[] result = (T[]) new Object[numberOfEntries]; 

    int index = 0;
    DLNode currentNode = head;
    while((index < numberOfEntries)&& (currentNode != null)) {
        result [index] = currentNode.data;
        currentNode = currentNode.next;
        index ++;
    }
    return result;
}
public boolean isEmpty() {
    return numberOfEntries == 0;
}
public int getCurrentSize() {
    return numberOfEntries;
}
public boolean contains(T anEntry) {
    checkInitialization();
    int index = 0;

    DLNode currentNode = head;

    if(isEmpty()) {
        return false;
    }
    else {
    while ((currentNode.next.data!= null) && index > numberOfEntries) {
        if (currentNode.data.equals(anEntry))
        {

            return true;}
        else {
            index ++;
            currentNode = currentNode.next;
        }
    }
    }
    return false;
}



public void clear() {
    head = null;
    numberOfEntries = 0;
}
// remove in-place. That is, do not rearrange elements in the array.

public DLNode find (T anEntry) {
    DLNode currentNode = head;
    while (currentNode != null) {

        if (currentNode.data.equals(anEntry)) {
            return currentNode;

        }
        currentNode = currentNode.next;
    }return null;

}

public boolean remove(T anEntry) {
    DLNode instead = find(anEntry);
    if (isEmpty()) {
        return false;
    }
    else {
        if(instead ==null) {
        return false;
        }
        else {
            if (numberOfEntries ==1) {
                clear();

            }
            else {
        instead.prev.next = instead.next;
        if (instead.next != null) {
            instead.next.prev = instead.prev;
        }
        numberOfEntries--;


        }
        }return true;
    }
}
private void checkInitialization() {
    if (!initialized) {
        throw new SecurityException("Uninitialized object used " + "to call an ArrayBag method.");
    }
}
// Don't change this.
public int getCapacity() {
    return numberOfEntries;
}

public static void main(String[] args) {
    LinkedSet <String> abag = new LinkedSet<>();
    abag.add("A");
    abag.add("B");
    abag.add("C");
    abag.add("D");
    displayBag(abag);
    System.out.println(abag.isEmpty());
    System.out.println(abag.getCurrentSize());
    System.out.println(abag.contains("A"));
    abag.clear();
    displayBag(abag);
    abag.add("A");
    abag.add("B");
    abag.add("C");
    abag.add("D");

    displayBag(abag);
    System.out.println(abag.remove("C"));
}

private static void displayBag(LinkedSet<String> aBag) {
    System.out.println("The bag contains the following string(s):");
    Object[] bagArray = aBag.toArray();
    for (int index = 0; index < bagArray.length; index++) {
        System.out.print(bagArray[index] + " ");
    } // end for

    System.out.println();
}

要求我們不能更改此數組中元素的順序。 但是我不知道為什么是“ instead.prev.next =相反.next;”。 是錯的。

add方法中,您執行DLNode doubleLinked = new DLNode(newEntry, null, head); ,因此,您沒有提供對先前條目的引用。 這就是為什么在remove ,當您執行instead.prev.next instead.prev時的原因,因為instead.prevnull ,您會得到NPE。

使用調試器很容易檢測到。

這是您的add方法的外觀:

public boolean add(T newEntry) {
    checkInitialization();
    DLNode doubleLinked = new DLNode(newEntry, null, head);
    //code I added
    if (head != null) {
        head.prev = doubleLinked;
    }

    head = doubleLinked;
    numberOfEntries++;
    return true;
}

暫無
暫無

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

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