在Java中,如何从双链表中随机删除任何节点?

在Java中,如何从双链表中随机删除任何节点?,java,doubly-linked-list,Java,Doubly Linked List,我正在制作一个双链接列表,允许您在前面和后面插入,以及从列表中删除任何节点,只要它存在。问题是,它不工作并发出,或者发出NullPointerException,或者它只是说Integer不存在,即使它确实存在。代码是: public class Numbers { Node head = null; //Head of the list Node tail = null; //end of the doubly list int size = 0;

我正在制作一个双链接列表,允许您在前面和后面插入,以及从列表中删除任何节点,只要它存在。问题是,它不工作并发出,或者发出NullPointerException,或者它只是说Integer不存在,即使它确实存在。代码是:

public class Numbers {

    Node head = null; //Head of the list
    Node tail = null; //end of the doubly list    
    int size = 0;


    public void FrontInsert(int data) {
        Node n = new Node();
        if (head == null) {
            head = n;

        } else {
            n.prev = head;
            head.next = n;
            head = n;

        }
        size++;
    }

    public void RearInsert(int data) {
        Node n = new Node();
        if (head == null) {
            head = n;
            tail = n;

        } else {

            n.next = tail;
            tail.prev = n;
            tail = n;
        }
        size++;
    }

    public void Delete(int x) {

        if (size == 0) {
            System.out.println("The list is empty.");
        }
        if (head.data == x) {
            head = head.next;
            if (head != null) {
                head.prev = null;
            }
            size--;
            return;
        }

        tmp = head;

        while (tmp != null && tmp.data != x) {
            tmp = tmp.next;
        }

        if (tmp == null) {
            System.out.println("That integer does not exist.");
            return;
        }

        if (tmp.data == x) {
            tmp.prev.next = tmp.next;
            if (tmp.next != null) {
                tmp.next.prev = tmp.prev;
            }
        }
        size--;
    }
    public void printList() {
        while (head != null) {
            System.out.print(head.data + " ");
            head = head.prev;
        }
    }
    public static void main(String[] args) {
        Numbers nu = new Numbers();

    }
    class Node {
        Node prev;
        Node next;
        int data;

        public void Node(int data) {
            this.data = data;
            next = null;
            prev = null;
        }
    }
}    

试试这个并检查输出

公共课号{

Node head = null; 
Node tail = null; 
int size = 0;

public void FrontInsert(int data) {
    Node n = new Node(data);
    if (head == null) { // first insert
        head = n;
        tail = n;
    } else {
        n.next = head;
        head.prev = n;
        head = n;

    }
    size++;
}

public void RearInsert(int data) {
    Node n = new Node(data);
    if (head == null) {
        head = n;
        tail = n;

    } else {

        n.prev = tail;
        tail.next = n;
        tail = n;
    }
    size++;
}

public void Delete(int index) { // index is the position to be remove

    if (size == 0) {
        System.out.println("The list is empty."); return;
    }else if(index < 0 || index > size -1){
        System.out.println("Index outOf Bound."); return;
    }

    Node currentNode  = head;
    for(int i = 1; i <= index ; i++){
        currentNode = currentNode.next;
    }
    //remove
    if (index == 0) {
        currentNode.next.prev = null;
        head = currentNode.next;
    } else if (index == size - 1) {
        currentNode.prev.next = null;
        tail = currentNode.prev;
    } else {
        if (currentNode.prev != null) // Ensure its not header
            currentNode.prev.next = currentNode.next;
        if (currentNode.next != null) // Ensure its not tail
            currentNode.next.prev = currentNode.prev;
    }
    size--;
}

public void printList() {
    Node tmp = head;
    while (tmp != null) {
        System.out.print(tmp.data + "  ");
        tmp = tmp.next;
    }
    System.out.println();
}

public static void main(String[] args) {
    Numbers nu = new Numbers();
    nu.FrontInsert(1);nu.printList();
    nu.FrontInsert(2);nu.printList();
    nu.RearInsert(3);nu.printList();
    nu.FrontInsert(4);nu.printList();
    nu.RearInsert(3);nu.printList();
    nu.FrontInsert(4);nu.printList();
    nu.RearInsert(3);nu.printList();
    nu.RearInsert(3);nu.printList();
    nu.FrontInsert(4);nu.printList();
    System.out.println();
    nu.Delete(4);
    nu.printList();
}

class Node {
    Node prev;
    Node next;
    int data;

    public Node(int data) {
        this.data = data;
        next = null;
        prev = null;
    }
}
节点头=null;
节点尾=空;
int size=0;
公共void FrontInsert(整数数据){
节点n=新节点(数据);
如果(head==null){//第一次插入
水头=n;
尾=n;
}否则{
n、 下一个=头部;
head.prev=n;
水头=n;
}
大小++;
}
公共无效重新插入(整数数据){
节点n=新节点(数据);
if(head==null){
水头=n;
尾=n;
}否则{
n、 prev=尾部;
tail.next=n;
尾=n;
}
大小++;
}
public void Delete(int index){//index是要删除的位置
如果(大小==0){
System.out.println(“列表为空”);返回;
}else if(索引<0 | |索引>大小-1){
System.out.println(“索引超出界限”);返回;
}
节点当前节点=头;

对于(inti=1;i尝试此操作并检查输出

公共课号{

Node head = null; 
Node tail = null; 
int size = 0;

public void FrontInsert(int data) {
    Node n = new Node(data);
    if (head == null) { // first insert
        head = n;
        tail = n;
    } else {
        n.next = head;
        head.prev = n;
        head = n;

    }
    size++;
}

public void RearInsert(int data) {
    Node n = new Node(data);
    if (head == null) {
        head = n;
        tail = n;

    } else {

        n.prev = tail;
        tail.next = n;
        tail = n;
    }
    size++;
}

public void Delete(int index) { // index is the position to be remove

    if (size == 0) {
        System.out.println("The list is empty."); return;
    }else if(index < 0 || index > size -1){
        System.out.println("Index outOf Bound."); return;
    }

    Node currentNode  = head;
    for(int i = 1; i <= index ; i++){
        currentNode = currentNode.next;
    }
    //remove
    if (index == 0) {
        currentNode.next.prev = null;
        head = currentNode.next;
    } else if (index == size - 1) {
        currentNode.prev.next = null;
        tail = currentNode.prev;
    } else {
        if (currentNode.prev != null) // Ensure its not header
            currentNode.prev.next = currentNode.next;
        if (currentNode.next != null) // Ensure its not tail
            currentNode.next.prev = currentNode.prev;
    }
    size--;
}

public void printList() {
    Node tmp = head;
    while (tmp != null) {
        System.out.print(tmp.data + "  ");
        tmp = tmp.next;
    }
    System.out.println();
}

public static void main(String[] args) {
    Numbers nu = new Numbers();
    nu.FrontInsert(1);nu.printList();
    nu.FrontInsert(2);nu.printList();
    nu.RearInsert(3);nu.printList();
    nu.FrontInsert(4);nu.printList();
    nu.RearInsert(3);nu.printList();
    nu.FrontInsert(4);nu.printList();
    nu.RearInsert(3);nu.printList();
    nu.RearInsert(3);nu.printList();
    nu.FrontInsert(4);nu.printList();
    System.out.println();
    nu.Delete(4);
    nu.printList();
}

class Node {
    Node prev;
    Node next;
    int data;

    public Node(int data) {
        this.data = data;
        next = null;
        prev = null;
    }
}
节点头=null;
节点尾=空;
int size=0;
公共void FrontInsert(整数数据){
节点n=新节点(数据);
如果(head==null){//第一次插入
水头=n;
尾=n;
}否则{
n、 下一个=头部;
head.prev=n;
水头=n;
}
大小++;
}
公共无效重新插入(整数数据){
节点n=新节点(数据);
if(head==null){
水头=n;
尾=n;
}否则{
n、 prev=尾部;
tail.next=n;
尾=n;
}
大小++;
}
public void Delete(int index){//index是要删除的位置
如果(大小==0){
System.out.println(“列表为空”);返回;
}else if(索引<0 | |索引>大小-1){
System.out.println(“索引超出界限”);返回;
}
节点当前节点=头;

对于(inti=1;i好吧,你的头和尾是相互排斥的,我的意思是当你在列表的尾部添加一些东西时,你只给出了一面参考,而不是两面参考,你必须这样说

tail.next=n;n.prev=tail;tail=n

以下是工作代码:

public class Numbers {

Node head = null; //Head of the list
Node tail = null; //end of the doubly list    
int size = 0;


public void FrontInsert(int data) {
    Node n = new Node(data);
    if (head == null) {
        head = n;
        tail = head;
    } else {
        n.next = head;
        head.prev = n;
        head = n;

    }
    size++;
}

public void RearInsert(int data) {
    Node n = new Node(data);
    if (head == null) {
        head = n;
        tail = head;
    } else {
        n.next = null;
        tail.next = n;
        n.prev = tail;
        tail = n;
    }
    size++;
}

@SuppressWarnings("null")
public void Delete(int x) {

    if (size == 0) {
        System.out.println("The list is empty.");
        return;
    }
    if (head.data == x) {
        head = head.next;
        if (head != null) {
            head.prev = null;
        }
        size--;
        return;
    }

    Node tmp = head;


    while (true) {
        if(tmp == null)
            break;
        if(tmp.data == x)
            break;
        System.out.println(tmp.data);
        tmp = tmp.next;
    }

    if (tmp == null) {
        System.out.println("That integer does not exist.");
        return;
    }

    if (tmp.data == x) {
        tmp.prev.next = tmp.next;
        if (tmp.next != null) {
            tmp.next.prev = tmp.prev;
        }
    }
    size--;
}
public void printList() {
    while (head != null) {
        System.out.print(head.data + " ");
        head = head.next;
    }
}
public static void main(String[] args) {
    Numbers nu = new Numbers();
    nu.FrontInsert(2);
    nu.FrontInsert(3);   
    nu.FrontInsert(6);
    nu.RearInsert(8);
    nu.RearInsert(20);
    nu.Delete(8);
    nu.printList(); 
   // System.out.println(nu.head.data + "data");
 //   System.out.println(nu.head.next.data + "data");

}
class Node {
    Node prev;
    Node next;
    private int data;


    public Node(int data) {
        this.data = data;
        next = null;
        prev = null;
    }
}

}

好吧,你的头和尾巴是相互排斥的,我的意思是,当你在列表的末尾添加一些东西时,你只给出了一面,而不是两面,你必须这样说

tail.next=n;n.prev=tail;tail=n

以下是工作代码:

public class Numbers {

Node head = null; //Head of the list
Node tail = null; //end of the doubly list    
int size = 0;


public void FrontInsert(int data) {
    Node n = new Node(data);
    if (head == null) {
        head = n;
        tail = head;
    } else {
        n.next = head;
        head.prev = n;
        head = n;

    }
    size++;
}

public void RearInsert(int data) {
    Node n = new Node(data);
    if (head == null) {
        head = n;
        tail = head;
    } else {
        n.next = null;
        tail.next = n;
        n.prev = tail;
        tail = n;
    }
    size++;
}

@SuppressWarnings("null")
public void Delete(int x) {

    if (size == 0) {
        System.out.println("The list is empty.");
        return;
    }
    if (head.data == x) {
        head = head.next;
        if (head != null) {
            head.prev = null;
        }
        size--;
        return;
    }

    Node tmp = head;


    while (true) {
        if(tmp == null)
            break;
        if(tmp.data == x)
            break;
        System.out.println(tmp.data);
        tmp = tmp.next;
    }

    if (tmp == null) {
        System.out.println("That integer does not exist.");
        return;
    }

    if (tmp.data == x) {
        tmp.prev.next = tmp.next;
        if (tmp.next != null) {
            tmp.next.prev = tmp.prev;
        }
    }
    size--;
}
public void printList() {
    while (head != null) {
        System.out.print(head.data + " ");
        head = head.next;
    }
}
public static void main(String[] args) {
    Numbers nu = new Numbers();
    nu.FrontInsert(2);
    nu.FrontInsert(3);   
    nu.FrontInsert(6);
    nu.RearInsert(8);
    nu.RearInsert(20);
    nu.Delete(8);
    nu.printList(); 
   // System.out.println(nu.head.data + "data");
 //   System.out.println(nu.head.next.data + "data");

}
class Node {
    Node prev;
    Node next;
    private int data;


    public Node(int data) {
        this.data = data;
        next = null;
        prev = null;
    }
}

}

创建
节点时
没有传递
数据
变量。应为
节点n=新节点(数据)
对吗?我试过了,它对delete方法不起作用。我得到了numbers.Node的不兼容操作数类型int。您的代码中有几个逻辑错误。为了您自己的教育,您可以做的最好的事情是在IDE调试器中一行一行地逐步检查代码,在每个语句之后检查变量,看看发生了什么你可以调试代码,添加一些日志语句来理解它何时以及为什么抛出NullPointerException。你说temp=head,你能告诉我你在哪里声明了tmp,这是什么类型的变量吗?因为java不允许在创建
节点时声明没有类型的变量,所以你不能传递
数据
变量。应为
节点n=新节点(数据)
对吗?我试过了,它对delete方法不起作用。我得到了numbers.Node的不兼容操作数类型int。您的代码中有几个逻辑错误。为了您自己的教育,您可以做的最好的事情是在IDE调试器中一行一行地逐步检查代码,在每个语句之后检查变量,看看发生了什么你可以调试代码,添加一些日志语句来理解它何时以及为什么抛出NullPointerException。你说temp=head,你能告诉我你在哪里声明了tmp,这是什么类型的变量吗?因为java不允许声明没有类型的变量