📜  Java程序从循环链表中间删除一个节点

📅  最后修改于: 2022-05-13 01:54:19.029000             🧑  作者: Mango

Java程序从循环链表中间删除一个节点

在本文中,我们将学习在Java中从循环链表中删除中间节点。对于这个程序,我们要遵循的方法是,首先我们计算列表中的节点数,然后将节点数除以 2 得到列表的中间节点。

删除中间元素之前删除中间元素后

算法

情况 1 :列表为空

  • 然后只需从函数返回。

情况二:List只有一个节点

  • 这意味着 head == tail 然后分配的 head 和 tail 等于 null。

案例 3 :List 只有两个节点

  • 它表示列表中第 1 个位置的中间节点。在这种情况下,我们还必须修改头部和尾部。头变成尾巴和尾巴。下一个也变成尾巴。

案例 4 :List 有两个以上的节点

  • 这意味着中间节点的位置大于 1。在这种情况下,我们定义了两个节点tempprev 。将头分配给温度。
  • 然后使用 temp 遍历列表直到中间节点。在每次迭代中分配prev = temptemp = temp.next
  • 现在 temp 指向中间节点, prev 指向中间节点的前一个节点。
  • 然后简单地设置 prev.next=temp.next 和 temp=null,
Java
// Java Program to Delete a 
// Node From the Middle of the
// Circular Linked List
  
class CLinkedList {
  
    class Node {
        int data;
        Node next;
    }
    private static int size;
  
    // head points first node and
    // tail points to last node
    private Node head, tail;
    CLinkedList()
    {
        this.head = null;
        this.tail = null;
        size = 0;
    }
    public void addNode(int d)
    {
        // create a empty node
        Node n = new Node();
  
        // list is empty
        if (this.head == null) {
            n.data = d;
            this.head = n;
            this.tail = n;
            n.next = this.head;
        }
  
        // list has one node or more than one nodes
        else {
            n.data = d;
            tail.next = n;
            tail = n;
            tail.next = head;
        }
        size++;
    }
    public void deleteNodeMiddle()
    {
        int loc;
        Node temp, prev;
  
        // calculating position of middle node
        if (size % 2 == 0) {
            loc = size / 2;
        }
        else {
            loc = (size / 2) + 1;
        }
  
        // list is empty
        if (head == null) {
            return;
        }
  
        // list contains only one node
        else if (head == tail) {
            head = null;
            tail = null;
        }
  
        // list contains only two nodes
        else if (loc == 1) {
            head = tail;
            tail.next = tail;
        }
  
        // list contains more than two nodes
        else {
            temp = head;
            prev = null;
            int i = 1;
            while (i < loc) {
                prev = temp;
                temp = temp.next;
                i++;
            }
            prev.next = temp.next;
            temp = null;
        }
  
        size--;
        if (size < 0) {
            size = 0;
        }
    }
    public void display()
    {
        // displaying list
        if (head == null) {
            System.out.println("List is empty");
        }
        else {
            Node temp = head;
            do {
                System.out.print(temp.data + " ");
                temp = temp.next;
            } while (temp != head);
            System.out.println();
        }
    }
}
class Test {
    public static void main(String args[])
    {
        CLinkedList c1 = new CLinkedList();
  
        // adding node in linkedlist
        c1.addNode(10);
        c1.addNode(20);
        c1.addNode(30);
        c1.addNode(40);
  
        // displaying nodes after adding
        System.out.print("List=");
        c1.display();
  
        // delete node in middle
        c1.deleteNodeMiddle();
  
        // displaying linkedlist after deleting middle node
        System.out.print(
            "List after deleting middle node=");
        c1.display();
  
        c1.deleteNodeMiddle();
  
        // displaying linkedlist after deleting middle node
        System.out.print(
            "List after deleting middle node=");
        c1.display();
  
        c1.deleteNodeMiddle();
  
        // displaying linkedlist after deleting middle node
        System.out.print(
            "List after deleting middle node=");
        c1.display();
  
        c1.deleteNodeMiddle();
  
        // displaying linkedlist after deleting middle node
        System.out.print(
            "List after deleting middle node=");
        c1.display();
    }
}


输出
List=10 20 30 40 
List after deleting middle node=10 30 40 
List after deleting middle node=10 40 
List after deleting middle node=40 
List after deleting middle node=List is empty