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

时间:2019-10-14 04:01:06

标签: java linked-list doubly-linked-list

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

我已经尝试过了,但是仍然无法正常工作

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

这是我的代码

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

2 个答案:

答案 0 :(得分:1)

使用您的代码,如果这种情况导致它以1个节点结束(头将指向此节点),并且您要删除此节点(即头),则代码将失败,并出现NullPointerException,位于

del.next.previous = null;
  

因为del.next为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/

答案 1 :(得分:0)

您的代码的问题在于,head函数中的head不会被更改,因为它是通过值传递的。请考虑以下情形:

  1. 您要删除位置1。Head指向node1,因此 它存储node1的地址。假设是1001。
  2. 现在,您使用头参考和currentNode调用head函数,因此头参考作为按值传递被传递给函数参数。因此在函数参数head中包含地址1001。
  3. 现在您执行删除操作,因此函数的head会将其位置更改为下一个节点。但是,您班级成员的deleteNode仍指向第一个位置。
  4. 要解决此问题,您可以再次设置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); } ,因为您是从npm install gson 函数返回它的。喜欢:

按如下所示更改代码

const GSON= require('gson');

const gsonObj = GSON.decode('["test","{\\"ID\\":0}]');