Java教程

Java在循环链接列表的中间插入新节点

在此程序中,我们创建一个循环链接列表,并在循环链接列表的中间插入新节点清单。如果列表为空,头和尾都将指向新节点。如果列表不为空,则我们将计算列表的大小,然后将其除以2,以得到列表中点,该点需要在其中插入新节点。
Java程序在中间插入一个新节点循环链接列表的列表
在列表中间插入新节点之后。
Java程序用于在Circular Linked List的中间插入一个新节点
考虑上图;新节点需要添加到列表的中间。首先,我们计算大小(在这种情况下为4)。因此,要获得中点,我们将其除以2并将其存储在变量计数中。我们将定义两个节点current和temp,这样temp将指向head,而current将指向temp之前的节点。我们通过将temp递增到temp来遍历列表直到到达中点.next,然后在current和temp之间插入新节点。当前的下一个节点将是新节点,而新的下一个节点将是临时节点。

算法

定义一个代表列表中节点的Node类。它有两个属性数据,下一个将指向下一个节点。 定义另一个用于创建循环链表的类,它具有两个节点: head和tail。可变大小存储列表的大小。它有两种方法: addInMid()和display()。 addInMid()会将节点添加到列表的中间: 首先检查head是否为空(空列表),然后将节点插入为head。 头部和尾部都将指向新添加的节点。 如果列表不为空,则我们计算大小并将其除以2以得到中点。 定义将指向头的节点温度,当前将指向温度之前的节点。 通过将temp递增到temp.next,遍历列表直到到达列表的中间。 新节点将在当前之后和temp之前插入,以便当前将指向新节点,而新节点将指向temp。
a.display()将显示列表中存在的所有节点。
定义一个新节点"当前",该节点将指向头部。 打印current.data直到电流再次指向头。 当前每次迭代将指向列表中的下一个节点。

程序:

public class InsertInMid {
    //Represents the node of list.
        public class Node{
        int data;
        Node next;
        public Node(int data) {
            this.data = data;
        }
    }
    public int size;
    //Declaring head and tail pointer as null.
        public Node head = null;
    public Node tail = null;
    //this function will add the new node to the list.
        public void add(int data){
        //Create new node
            Node newNode = new Node(data);
        //Checks if the list is empty.
            if(head == null) {
            //if list is empty, both head and tail would point to new node.
                head = newNode;
            tail = newNode;
            newNode.next = head;
        }
        else {
            //tail will point to new node.
                tail.next = newNode;
            //New node will become new tail.
                tail = newNode;
            //Since, it is circular linked list tail will points to head.
                tail.next = head;
        }
        //Size will count the number of element in the list
            size++;
    }
    //this function will add the new node at the middle of the list.
        public void addInMid(int data){
        Node newNode = new Node(data);
        //Checks if the list is empty.
            if(head == null){
            //if list is empty, both head and tail would point to new node.
                head = newNode;
            tail = newNode;
            newNode.next = head;
        }
        else{
            Node temp,current;
            //Store the mid-point of the list
                int count = (size % 2 == 0) ? (size/2) : ((size+1)/2);
            //temp will point to head
                temp = head;
            current= null;
            for(int i = 0; i < count; i++){
                //Current will point to node previous to temp.
                    current = temp;
                //Traverse through the list till the middle of the list is reached
                    temp = temp.next;
            }
            //current will point to new node
                current.next = newNode;
            //new node will point to temp
                newNode.next = temp;
        }
        size++;
    }
    //Displays all the nodes in the list
        public void display() {
        Node current = head;
        if(head == null) {
            System.out.println("List is empty");
        }
        else {
            do{
                //Prints each node by incrementing pointer.
                    System.out.print(" "+ current.data);
                current = current.next;
            }
            while(current != head);
            System.out.println();
        }
    }
    public static void main(String[] args) {
        InsertInMid cl = new InsertInMid();
        //Adds data to the list
            cl.add(1);
        cl.add(2);
        cl.add(3);
        cl.add(4);
        System.out.println("Original list: ");
        cl.display();
        //Inserting node '5' in the middle
            cl.addInMid(5);
        System.out.println( "Updated List: ");
        cl.display();
        //Inserting node '6' in the middle
            cl.addInMid(6);
        System.out.println("Updated List: ");
        cl.display();
    }
}
输出:
Original list:
1 2 3 4
Updated List:
1 2 5 3 4
Updated List:
1 2 5 6 3 4
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4