如何删除双向链表的第一个节点

How to delete the first node of doubly linked list

我有一个双向链表的实现,我正在尝试删除给定位置的特定节点。我设法删除了第二个节点到最后一个节点,但是当我尝试删除第一个节点时它失败了,我想知道我的代码有什么问题。

我已经试过了,但还是不行

head.next.previous = null;
head = head.next;

This is my code

public class Proses {
    private class Node{
        String Matkul;
        int NilaiUts;
        int NilaiUAS;
        Node previous;  
        Node next;  

        public Node(String Matkul, int Nilai, int NilaiUAS) {
            this.Matkul = Matkul;
            this.NilaiUts = Nilai;
            this.NilaiUAS = NilaiUAS;
        }  
    }  

    Node head, tail = null;    
    public void addNode(String matkul, int Nilai, int NilaiUAS) {   
        Node newNode = new Node(matkul, Nilai, NilaiUAS);   
        if(head == null) {   
            head = tail = newNode;    
            head.previous = null;  
            tail.next = null;  
        } else {  
            tail.next = newNode;   
            newNode.previous = tail;  
            tail = newNode;  
            tail.next = null;  
        }  
    }  

    public void delete(int position){
        if (head == null || n <= 0) 
            return; 
        Node current = head; 
        int i; 
        for (i = 1; current != null && i < position; i++) 
        { 
            current = current.next; 
        } 
        if (current == null) 
            return; 
        deleteNode(head, current); 
    }

    //delete function
    public Node deleteNode(Node head, Node del){
        if (head == null || del == null){
            return null; 
        }
        if (head == del){
            head = del.next;
            del.next.previous = null;
        }
        if (del.next != null){
            del.next.previous = del.previous; 
        }
        if (del.previous != null){
            del.previous.next = del.next; 
        }
        del = null; 
        return head; 
    }
}

使用您的代码,如果场景以 1 个节点结束(head 将指向该节点)并且您想删除该节点(即 head),代码将失败并出现 NullPointerException at

del.next.previous = null;

as del.next is NULL;

使用可以看看下面的代码从双向链表中删除一个节点

    // Function to delete a node in a Doubly Linked List. 
    // head_ref --> pointer to head node pointer. 
    // del --> data of node to be deleted. 
    void deleteNode(Node head_ref, Node del) 
    { 

        // Base case 
        if (head == null || del == null) { 
            return; 
        } 

        // If node to be deleted is head node 
        if (head == del) { 
            head = del.next; 
        } 

        // Change next only if node to be deleted 
        // is NOT the last node 
        if (del.next != null) { 
            del.next.prev = del.prev; 
        } 

        // Change prev only if node to be deleted 
        // is NOT the first node 
        if (del.prev != null) { 
            del.prev.next = del.next; 
        } 

        // Finally, free the memory occupied by del 
        return; 
    } 

代码参考:https://www.geeksforgeeks.org/delete-a-node-in-a-doubly-linked-list/

您的代码的问题在于,head 在 deleteNode 函数中没有发生变化,因为它是按值传递的。考虑以下情况:

  1. 你删除的是位置1,head指向的是node1,所以 它存储 node1 的地址。假设它是 1001.
  2. 现在您调用 deleteNode 带有头引用和 currentNode 的函数,因此头引用作为按值传递传递给函数参数。所以在函数参数 head 中包含地址 1001.
  3. 现在您执行删除操作,因此函数的 head 正在将其位置更改为下一个节点。但是,您的 class 成员的 head 仍然指向第一个位置。
  4. 为了克服这个问题,您可以再次设置 head,因为您是从 deleteNode 函数返回它的。喜欢:

修改代码如下

public void delete(int position){
        if (head == null || n <= 0) 
            return; 
        Node current = head; 
        int i; 
        for (i = 1; current != null && i < position; i++) 
        { 
            current = current.next; 
        } 
        if (current == null) 
            return; 
        head = deleteNode(head, current); 
    }