简体   繁体   English

如何删除双链表中的特定条目?

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

it shows me that "Exception in thread "main" java.lang.NullPointerException" and the error is in "instead.prev.next = instead.next;" 它向我显示“线程“主”中的异常java.lang.NullPointerException”,并且错误出现在“ instead.prev.next =相反。next;”中 here. 这里。

here is the whole code i have written 这是我写的全部代码

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();
}

} It was required that we cannot change the order of the elements in this array. 要求我们不能更改此数组中元素的顺序。 But I do not know why "instead.prev.next = instead.next;" 但是我不知道为什么是“ instead.prev.next =相反.next;”。 is wrong. 是错的。

In the add method you do DLNode doubleLinked = new DLNode(newEntry, null, head); add方法中,您执行DLNode doubleLinked = new DLNode(newEntry, null, head); , so, you don't provide a reference to your previous entry. ,因此,您没有提供对先前条目的引用。 This is why in remove , when you do instead.prev.next since instead.prev is null , you get your NPE. 这就是为什么在remove ,当您执行instead.prev.next instead.prev时的原因,因为instead.prevnull ,您会得到NPE。

This is simple to detect with a debugger. 使用调试器很容易检测到。

This is how your add method should look: 这是您的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