📌  相关文章
📜  Java程序在循环链表的开头插入一个新节点

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

Java程序在循环链表的开头插入一个新节点

循环链表:循环链表是一个元素序列,其中每个元素都指向序列中的下一个元素,最后一个元素与第一个元素相连。这意味着循环链表类似于单链表,除了最后一个节点指向列表中的第一个节点。

在循环链表的开头插入一个新节点:

如果链表为空,则 head 和 tail 都将指向新添加的节点。如果列表不为空,则将最后一个节点指向新添加的节点,将新添加的节点指向头节点,最后将新添加的节点作为头节点。

在开头添加节点 0在开头添加节点 0

算法 :

  1. 创建一个代表列表中节点的 Node 类。它有两个变量 data 和 next 指针(指向下一个节点)。
  2. 创建另一个用于创建循环链表的类,它有两个节点,即头和尾。
  3. 当向列表中添加新节点时,我们将首先检查头部是否为空。如果列表为空或 head 为空,那么我们将插入节点作为头,尾也指向新添加的节点。
  4. 如果列表不为空,则新添加的节点将指向头部,尾部将指向新添加的节点,并将创建新节点作为头节点。

代码片段:

Java
// Java Program to Insert a nodes at the Beginning of the
// Circular Linked List
  
public class AddAtBeginning {
  
    // Represents the node of list.
    public class Node {
        char data;
        Node next;
        public Node(char data) { this.data = data; }
    }
  
    // Declaring head and tail pointer as null.
    // head indicates the starting node and tail indicates
    // last node.
    public Node head = null;
    public Node tail = null;
  
    // This function will add the new node at the Beginning
    // of the circular linked list.
    public void addNode(char data)
    {
        // Create new node
        Node newNode = new Node(data);
  
        // Checks if the list is empty.
        if (head == null) {
  
            // make newnode as both head and tail node.
            head = newNode;
            tail = newNode;
  
            // point the tail to head node.
            tail.next = head;
        }
        else {
  
            // point the newnode to head
            newNode.next = head;
  
            // point the rail to new node.
            tail.next = newNode;
  
            // make the newnode as head.
            head = newNode;
        }
    }
  
    // printLinkedList prints all the nodes in the list
    public void printLinkedList()
    {
        Node presentNode = head;
        if (head == null) {
            System.out.println("List is empty");
        }
        else {
            System.out.println("\n");
  
            // here with out checking anything we will print
            // the first node, And print the rest of the
            // nodes till the pointer again reaches the head
            // node.
            do {
                System.out.print(" " + presentNode.data);
  
                // incrementing the nodes using next
                // property.
                presentNode = presentNode.next;
  
                // if present node is head, stop printing
                // the nodes.
            } while (presentNode != head);
        }
    }
  
    public static void main(String[] args)
    {
        AddAtBeginning obj = new AddAtBeginning();
  
        System.out.println(
            "Adding nodes at the beginning of the list: ");
        obj.addNode('s');
        obj.printLinkedList();
  
        // add k at the beginning
        obj.addNode('k');
        obj.printLinkedList();
  
        // add e at the beginning
        obj.addNode('e');
        obj.printLinkedList();
  
        // add e at the beginning
        obj.addNode('e');
        obj.printLinkedList();
  
        // add G at the beginning
        obj.addNode('G');
        obj.printLinkedList();
    }
}


输出
Adding nodes at the beginning of the list: 


 s

 k s

 e k s

 e e k s

 G e e k s

时间复杂度: O(1)