Java 实现单链表

Java 实现单链表,java,Java,我试图学习数据结构,但我遇到了可怕的NullPointerException,我不知道如何修复它 我的SinglyLinkedList类实现了一个接口,LinkedList,在这里我重新定义了一些方法,比如,add()、get()、contains(),等等 当我使用clear()方法时,会发生NullPointerException。它指向节点before.setNext(null)下的方法removeLast()。它还指向remove(head.getElement())下的clear()方

我试图学习数据结构,但我遇到了可怕的
NullPointerException
,我不知道如何修复它

我的
SinglyLinkedList
类实现了一个接口,
LinkedList
,在这里我重新定义了一些方法,比如,
add()、get()、contains()
,等等

当我使用
clear()
方法时,会发生
NullPointerException
。它指向
节点before.setNext(null)
下的方法
removeLast()
。它还指向
remove(head.getElement())
下的
clear()
方法

另外,如果我的代码中有什么可以改进的地方,请告诉我

public class SinglyLinkedList<E> implements LinkedList<E> {

    private class Node<E> {

        public Node<E> next;
        public E element;

        public Node(E element) {

            this.element = element;
        }

        public Node (E element, Node<E> next) {

            this.element = element;
            this.next = next;
        }

        public E getElement() {

            return element;
        }

        public Node<E> getNext() {

            return next;
        }

        public void setElement(E element) {

            this.element = element;
        }

        public void setNext(Node<E> next) {

            this.next = next;
        }

        public String toString() {

            return ("[" + element + "] ");
        }
    }

    public Node<E> head;
    public Node<E> tail;
    public int total;      

    public SinglyLinkedList() {

        this.head = null;
        this.tail = null; 
        this.total = 0;
    }

    public E get(int index) {

        if (index < 0 || index > size()) {
            return null;
        }

        if (index == 0) {
            return head.getElement();
        }

        Node<E> singly = head.getNext();

        for (int i = 1; i < index; i ++) {

            if (singly.getNext() == null) {
              return null;
            }       

            singly = singly.getNext();      
        }

        System.out.println("\n" + singly.getElement());

        return singly.getElement(); 
    }

    public void add(E element) {
        Node<E> singlyAdd = new Node<E>(element);

        if (tail == null) {
            head = singlyAdd;
            tail = singlyAdd;
        } else {
            tail.setNext(singlyAdd);
            tail = singlyAdd;
        }     

        total++;
    }             

    public void display() {
        if (head == null) {
            System.out.println("empty list");
        } else {
            Node<E> current = head;
            while (current != null) {
                System.out.print(current.toString());
                current = current.getNext();
            }
        }

    }

    public boolean contains(E data) {

        if (head == null) {
            return false;
        }

        if (head.getElement() == data) {
            System.out.println(head);
            return true;                                
        }

        while (head.getNext() != null) {
            head = head.getNext();

            if (head.getElement() == data) {
                System.out.println(head);                
                return true;                               
            }             

        } 

        return false;         
    }       

    private Node<E> removeFirst() {
        if (head == null) {
            System.out.println("We cant delete an empty list");
        }    

        Node<E> singly = head;            
        head = head.getNext();
        singly.setNext(null);
        total--;

        return singly;     
    } 

    private Node<E> removeLast() {

        Node<E> nodeBefore;
        Node<E> nodeToRemove;     

        if (size() == 0) {
            System.out.println("Empty list");
        }    

        nodeBefore = head;

        for (int i = 0; i < size() - 2; i++) {
          nodeBefore = nodeBefore.getNext();
        }    

        nodeToRemove = tail;    

        nodeBefore.setNext(null);
        tail = nodeBefore;
        total--;

        return nodeToRemove;
    }       

    public E remove(int index) {      

        E hold = get(index);     

        if (index < 0 || index >= size()) {
            return null;
        } else if (index == 0) { 

            removeFirst();    
            return hold;
        } else {

            Node<E> current = head;
            for (int i = 1; i < index; i++) {                
                current = current.getNext();
            }  

            current.setNext(current.getNext().getNext());
            total--; 
            return hold;
        }       
    }       

    public int size() {
        return getTotal();
    }

    public boolean remove(E data) {      

        Node<E> nodeBefore, currentNode; 

        if (size() == 0) {
            System.out.println("Empty list");
        }            

        currentNode = head;

        if (currentNode.getElement() == data) {
            removeFirst();
        }

        currentNode = tail;
        if (currentNode.getElement() == data) {
            removeLast();
        }

        if (size() - 2 > 0) {
            nodeBefore = head;
            currentNode = head.getNext();
            for (int i = 0; i < size() - 2; i++) {
                if (currentNode.getElement() == data) {

                    nodeBefore.setNext(currentNode.getNext());
                    total--;
                    break;
                }

                nodeBefore = currentNode;
                currentNode = currentNode.getNext();
            } 
        } 

        return true;
    }

    public void clear() {

        while (head.getNext() != null) {    
            remove(head.getElement());    
        }

        remove(head.getElement());    
    }

    private int getTotal() {
        return total;
    } 
}
public类SinglyLinkedList实现LinkedList{
私有类节点{
公共节点下一步;
公共电子元素;
公共节点(E元素){
this.element=元素;
}
公共节点(E元素,下一个节点){
this.element=元素;
this.next=next;
}
公共E getElement(){
返回元素;
}
公共节点getNext(){
下一步返回;
}
公共无效集合元素(E元素){
this.element=元素;
}
公共void setNext(节点next){
this.next=next;
}
公共字符串toString(){
返回(“[”+元素+“]”);
}
}
公共节点头;
公共节点尾部;
公共整数合计;
公共单链接列表(){
this.head=null;
this.tail=null;
这个总数=0;
}
公共E-get(int索引){
如果(索引<0 | |索引>大小()){
返回null;
}
如果(索引==0){
返回head.getElement();
}
Node single=head.getNext();
对于(int i=1;i=size()){
返回null;
}如果(索引==0){
移除第一个();
返回保持;
}否则{
节点电流=头;
对于(inti=1;i0){
nodeBefore=头部;
currentNode=head.getNext();
对于(int i=0;i
从clear方法中调用
remove(head.getElement())表示您正在尝试调用LinkedList的remove方法。既然你被超越了
    public boolean remove(Object o) {
    if (o==null) {
        for (Entry<E> e = header.next; e != header; e = e.next) {
            if (e.element==null) {
public void clear() {
    this.head = null;
    this.tail = null; 
    this.total = 0;
}
while (head.getNext() != null) {
        head = head.getNext();

        if (head.getElement() == data) {
            System.out.println(head);                
            return true;                               
        }             
    } 
public void clear() {
    this.head = null;
    this.tail = head;
    this.total = 0;
}