Java教程

Java从双向链接列表的中间删除一个新节点

在此程序中,我们将创建一个双向链接列表,并从双向链接列表的中间删除一个节点清单。如果列表为空,则显示消息"列表为空"。如果列表不为空,我们将计算列表的大小,然后将其除以2得到列表的中点。电流将指向头节点。我们将遍历列表直到到达中点。现在,电流将指向中间节点。我们删除中间节点,以使当前的上一个节点指向当前的下一个节点。
Java从双向链接列表的中间删除一个新节点
考虑上面的示例,上方列表的中间点是3.从头到中点迭代电流。现在,当前指向需要删除的中间节点。在这种情况下,节点new是需要删除的中间节点。可以通过使节点2(当前的上一个节点)指向节点3(当前的下一个节点)来删除New。将current设置为null。

算法

定义一个代表列表中节点的Node类。它将具有三个属性: 数据,前一个将指向上一个节点,下一个将指向下一个节点。 定义另一个用于创建双向链表的类,它有两个节点: head和tail。最初,头和尾将指向null。 deleteFromMid()将从列表中间删除一个节点: 首先检查head是否为null(空列表),然后由于列表中没有节点,它将从函数返回。 如果列表不为空,它将检查列表是否只有一个节点。 如果列表具有多个节点,则它将计算列表的大小。将大小除以2,然后将其存储在变量中。 遍历列表,直到当前指向列表的中间节点。 将当前的上一个节点连接到当前的下一个节点。 通过将其设置为null删除当前节点。
a.display()将显示列表中存在的所有节点。
定义一个新节点"当前",该节点将指向头部。 打印current.data直到current指向null。 当前每次迭代将指向列表中的下一个节点。

程序:

class DeleteMid {
    //Represent a node of the doubly linked list
    class Node{
        int data;
        Node previous;
        Node next;
        public Node(int data) {
            this.data = data;
        }
    }
    public int size = 0;
    //Represent the head and tail of the doubly linked list
    Node head, tail = null;
    //addNode() will add a node to the list
    public void addNode(int data) {
        //Create a new node
        Node newNode = new Node(data);
        //if list is empty
        if(head == null) {
            //Both head and tail will point to newNode
            head = tail = newNode;
            //head's previous will point to null
            head.previous = null;
            //tail's next will point to null, as it is the last node of the list
            tail.next = null;
        }
        else {
            //newNode will be added after tail such that tail's next will point to newNode
            tail.next = newNode;
            //newNode's previous will point to tail
            newNode.previous = tail;
            //newNode will become new tail
            tail = newNode;
            //As it is last node, tails next will point to null
            tail.next = null;
        }
        //Size will count the number of nodes present in the list
        size++;
    }
    //deleteFromMid() will delete a node from middle of the list
    public void deleteFromMid() {
        //Checks whether list is empty
        if(head == null) {
            return;
        }
        else {
            //current will point to head
            Node current = head;
            //Store the mid position of the list
            int mid = (size % 2 == 0) ? (size/2) : ((size+1)/2);
            //Iterate through list till current points to mid position
            for(int i = 1; i < mid; i++){
                current = current.next;
            }
            //if middle node is head of the list
            if(current == head) {
                head = current.next;
            }
            //if middle node is tail of the list
            else if(current == tail) {
                tail = tail.previous;
            }
            else {
                current.previous.next = current.next;
                current.next.previous = current.previous;
            }
            //Delete the middle node
            current = null;
        }
        size--;
    }
    //display() will print out the nodes of the list
    public void display() {
        //Node current will point to head
        Node current = head;
        if(head == null) {
            System.out.println("List is empty");
            return;
        }
        while(current != null) {
            //Prints each node by incrementing the pointer.
            System.out.print(current.data + " ");
            current = current.next;
        }
        System.out.println();
    }
    public static void main(String[] args) {
        DeleteMid dList = new DeleteMid();
        //Add nodes to the list
        dList.addNode(1);
        dList.addNode(2);
        dList.addNode(3);
        dList.addNode(4);
        dList.addNode(5);
        //Printing original list
        System.out.println("Original List: ");
        dList.display();
        while(dList.head != null) {
            dList.deleteFromMid();
            //Printing updated list
            System.out.println("Updated List: ");
            dList.display();
        }
    }
}
输出:
Original List:
1 2 3 4 5
Updated List:
1 2 4 5
Updated List:
1 4 5
Updated List:
1 5
Updated List:
5
Updated List:
List is empty
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4