从循环链表末尾删除节点的 Java 程序

原文:https://www . geesforgeks . org/Java-program-to-delete-从循环链表的末尾删除一个节点/

在本文中,我们将学习如何从循环链表的结尾删除一个节点。考虑如下所示的链接列表:

Circular LinkedList

示例:

Input : 5->3->4->(head node)
Output: 5->3->(head node)

我们将首先初始化列表,并使用 addNode()方法向其中添加一些数据,然后按照以下方法继续:

情况 1 :列表为空。

如果列表是空的,我们将简单地返回。

情况 2 :列表不为空

  • 删除():
    • 如果没有节点,则从函数返回
    • 如果只有一个节点,则将头和尾都设置为空
    • 如果它有一个以上的节点,那么它将删除前一个头节点,头将指向列表中的下一个节点,尾将指向新的头。
  • printNode()将打印列表中的所有节点,如下所示:
    • 节点电流被定义为指向头部
    • 打印当前值,直到它再次指向头部
    • 在每次迭代中,它将指向下一个节点

代码:

Java 语言(一种计算机语言,尤用于创建网站)

// Java Program to Delete a Node From the Ending of the
// Circular Linked List

public class Main {
    // Represents the node of list.
    public class Node {
        int val;
        Node next;
        public Node(int val) { this.val = val; }
    }

    // Initialising head and tail pointers
    public Node head = null;
    public Node tail = null;

    // add new node to the end
    public void addNode(int val)
    {
        // Creating new node
        Node node = new Node(val);

        // head and tail will point to new node
        // if list is empty
        if (head == null) {
            head = node;
            tail = node;
            node.next = head;
        }
        // otherwise tail point to new node and
        else {
            tail.next = node;
            tail = node;
            tail.next = head;
        }
    }

    // Deletes node from the beginning of the list
    public void delete()
    {
        // returns if list is empty
        if (head == null) {
            return;
        }
        // otherwise head will point to next element in the
        // list and tail will point to new head
        else {
            // if list contains more than one element
            // then loop will iterate
            // till second last element
            // is pointing to tail
            if (head != tail) {
                Node current = head;
                while (current.next != tail) {
                    current = current.next;
                }
                // Second last element is the new tail
                tail = current;
                tail.next = head;
            }

            // if the list contains only one element
            // then both head and tail will point to null
            else {
                head = tail = null;
            }
        }
    }

    // displaying the nodes
    public void printNode()
    {
        Node current = head;
        if (head == null) {
            System.out.println("List is empty");
        }
        else {
            do {
                System.out.print(" " + current.val);
                current = current.next;
            } while (current != head);
            System.out.println();
        }
    }

    public static void main(String[] args)
    {
        Main list = new Main();

        // Adds data to the list
        list.addNode(5);
        list.addNode(3);
        list.addNode(4);

        // Printing original list
        System.out.println("Original List: ");
        list.printNode();

        // deleting node from ending
        // till the list is empty and
        // displaying the Updated list
        while (list.head != null) {

            list.delete();

            // Printing updated list
            System.out.println("Updated List: ");
            list.printNode();
        }
    }
}

Output

Original List: 
 5 3 4
Updated List: 
 5 3
Updated List: 
 5
Updated List: 
List is empty