Java双循环链表删除

时间:2021-07-18 17:51:37

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

我创建了一个双向循环链表,想删除一个给定值的节点。如果我们假设有一个循环双向链表,节点值为 11,12,13,14,其中 11 是头节点。那么要删除值为 11 的节点,我们需要将前一个指针 12 设置为指向 14,节点 14 的下一个指针必须设置为 12,然后将 12 设为头节点。所以,现在如果我们选择显示列表答案应该是 12,13,14 但出乎意料的答案是 12,13,14,11.And 当我添加行时。 19(下面提到的删除函数)到代码中,显示了预期的输出。因此,我想知道逻辑是否正确,我们已经更改了所有上一个和下一个指针值,那么为什么输出不同。如果我想要删除第一个节点,即头节点,那么为什么我们要把下一个值设置为空(如第19行(下面提到的删除函数)?

节点结构:

public class Node{
int data;
Node next,prev;   //next and previous pointers for a node
}

删除函数

1     public void del(int x)
2    {
3       Node n=head;
4      do{
5          if(n.data==x){break;}
6           n=n.next;
7       }
8     while(n.data!=x);
9       if(n.data==x && n!=head)  //for deleting any other node except first node
10       {
11         n.next.prev=n.prev;
12         n.prev.next=n.next;
13       }
14       else if(n.data==x && n==head)  //for first node deletion
15       {
16           head.next.prev=head.prev;
17           head.prev.next=head.next;
18           head=head.next;
19        // n.next=null;
20       }
21       else{System.out.println("Element not found");}
22    }

PS:我还尝试打印节点和下一个值,即使在将节点 14 的下一个指针从 11 更改为 12(即删除 11)后,仍然 node.next 打印 11 作为节点的下一个14.为什么? 显示功能代码如下:

public void display()
    {
        Node n=head;
       
        if (head != null)
    {  
        do
        {
            System.out.println(n.data+" and its next is "+n.next.data);
            n=n.next;
        }while(n!=head);
        System.out.println("");
    }
        else{System.out.println("Please add an entry to the linked list");}
    }

1 个答案:

答案 0 :(得分:0)

我尽量保持简单,希望对您有所帮助。 (很难阅读您的代码,所以我只是从头开始编写)

public class DoublyLinkedList {
    // no null state
    DoublyLinkedList prev = this, next = this;
    int value;

    public DoublyLinkedList(int value) {
        this.value = value;
    }

    // add node to the back of the list
    public void add(int value) {
        DoublyLinkedList node = new DoublyLinkedList(value);
        node.next = this;
        node.prev = this.prev;
        this.prev.next = node;
        this.prev = node;
    }

    // remove the node
    public void remove() {
        prev.next = next;
        next.prev = prev;
    }

    // find the node with given value
    public DoublyLinkedList find(int value) {
        DoublyLinkedList 
            end = this.prev,
            current = this;
        while(current != null) {
            if(current.value == value) return current;
            if(current == end) break; // to prevent infinite cycle
            current = current.next;
        }
        return null;
    }

    // dump the list
    public void dump() {

        DoublyLinkedList 
            end = this.prev,
            current = this;
        while(current != null) {
            System.out.print(current.value + " ");
            if (current == end) break;
            current = current.next;
        }
        System.out.println();
    }

    // testing
    public static void main(String[] args) {
        DoublyLinkedList list = new DoublyLinkedList(1);
        list.add(2);
        list.add(3);
        list.add(4);

        list.dump();

        list.find(3).remove();

        list.dump();
    }
}
相关问题