简体   繁体   English

Java-双链表实现

[英]Java - DoublyLinked List Implementation

I'm learning data structures. 我正在学习数据结构。 So I'm trying to implement doubly linked list in Java. 因此,我试图doubly linked list in Java.实现doubly linked list in Java. I'm trying to implement insertAtAnyPosition method 我正在尝试实现insertAtAnyPosition方法

Using this method, I can able to insert the value at any specified index. 使用此方法,我可以在任何指定的索引处插入值。

public class DoublyLinkedList {

    Node head = null;

    public void insert(int data) {
        Node node = new Node();
        node.data = data;
        node.next = null;

        if(head == null)
            head = node;
        else {
            Node n = head;
            while(n.next != null  ) {
                n = n.next;
            }
            n.next = node;
            node.previous = n.next;
        }
    }

    public void insertAtStart(int data) {

        Node node = new Node();
        node.previous = null;
        node.data = data;

        head.previous = node;
        node.next = head;
        head = node;
    }

    public void insertAtAnyPosition(int index, int data) {
        Node node = new Node();
        node.data = data;

        Node n = head;

        for (int i = 0; i < index-1; i++) {
            n = n.next;
        }
        Node n1 = n.next.previous;

        node.previous = n1;
        node.next = n.next;
        n.next = node;
        n1 = node;

    }

    public void display() {
        Node n = head;

        while(n.next != null) {
            System.out.println(n.data);
            n = n.next;
        }
        System.out.println(n.data);
    }

}

Node Class 节点类别

public class Node {

    int data;
    Node next;
    Node previous;
}

Main method 主要方法

public static void main(String[] args) {
        // TODO Auto-generated method stub

        DoublyLinkedList list = new DoublyLinkedList();

        list.insert(12);
        list.insert(4);
        list.insert(1);
        list.insert(30);
        list.insertAtAnyPosition(4,100); // Null Pointer Exception thrown when trying to insert at 4th Index.
        list.display();

    }

"Null Pointer Exception" is thrown when I try to insert element at 4th Index . 当我尝试在4th Index处插入元素时,将引发"Null Pointer Exception" And the exception is thrown in this line Node n1 = n.next.previous; Node n1 = n.next.previous;抛出异常Node n1 = n.next.previous; But the above code works, when i try to insert element at index 1,2,3 but not in 4. Can anyone please help me how to resolve this issue? 但是,当我尝试在索引1,2,3而不是在索引4中插入元素时,上述代码有效。有人可以帮助我如何解决此问题吗?

Culprit is n.next.privious which must be causing null pointer exception for you 罪魁祸首是n.next.privious,它必须为您造成空指针异常

public void insertAtAnyPosition(int index, int data) {
        Node node = new Node();
        node.data = data;

        Node n = head;

        for (int i = 0; i < index-1; i++) {
            n = n.next;
        }
        Node n1 ; 
        if(n.next== null){
.       n1 = n;
}else{
       n1 =n.next.previous;
}
        node.previous = n1;
        node.next = n.next;
        n.next = node;
        n1 = node;

n.next.previous is no different from just n , of course. n.next.previousn没什么不同。 Unless n is the very last node (which it is, if you insert in position 4), in which case n.next doesn't exist, which gives you the NullPointerException . 除非n是最后一个节点(如果在第4个位置插入,它就是最后一个节点),则在这种情况下n.next不存在,这将为您提供NullPointerException

Since your 4th position is the end you need to consider a case for end as well. 由于您的第四名是终点,因此您也需要考虑终点。 Where you'll have to set the new inserted node next to null and the previous node (that was last) and set it's next to the new node 在这里,您必须将新插入的节点设置为null和上一个节点(最后一个)旁边,并将其设置为在新节点旁边

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM