Java教程

Java在单链列表的中间插入新节点

在此程序中,我们将创建一个单链列表,并在单链列表的中间添加一个新节点名单。为了完成此任务,我们将计算列表的大小,然后将其除以2,以得到列表中点,新节点需要插入该中间点。
Java将一个新节点单独插入中间链表
请参考上图;节点1代表原始列表的头。让node New是需要添加到列表中间的新节点。首先,我们计算大小(在这种情况下为4)。因此,要获得中点,我们将其除以2并将其存储在变量计数中。节点电流将指向头部。首先,我们遍历列表,直到当前指向中间位置。定义另一个节点temp,该节点指向当前节点旁边的节点。在当前和临时之间插入新节点

算法

创建一个具有两个属性的类Node: data和next。下一个是指向列表中下一个节点的指针。 创建另一个类InsertMid,它具有三个属性: head,tail和size,用于跟踪列表中存在的多个节点。 addNode()将一个新节点添加到列表中:
    创建一个新节点。 首先检查head是否等于null,这意味着列表为空。 如果列表为空,头和尾都将指向新添加的节点。 如果列表不为空,则新节点将被添加到列表的末尾,以使尾部的下一个指向新添加的节点。这个新节点将成为列表的新尾巴。
a.addInMid()将在列表的中间添加一个新节点:
首先检查head是否等于null,这意味着列表为空。 如果列表为空,头和尾都将指向新添加的节点。 如果列表不为空,则计算列表的大小并将其除以2以得到列表的中点。 定义将在列表中迭代的当前节点,直到当前节点指向中间节点为止。 定义另一个节点温度,该温度将指向当前节点旁边的节点。 新节点将在当前之后和temp之前插入,以便当前将指向新节点,而新节点将指向temp。
a.display()将显示列表中存在的节点:
定义一个当前将首先指向列表开头的节点。 遍历列表,直到当前指向null为止。 在每次迭代中通过使电流指向其旁边的节点来显示每个节点。

程序:

public class InsertMid {
    //Represent a node of the singly linked list
    class Node{
        int data;
        Node next;
        public Node(int data) {
            this.data = data;
            this.next = null;
        }
    }
    public int size;
    //Represent the head and tail of the singly linked list
    public Node head = null;
    public Node tail = null;
    //addNode() will add a new node to the list
    public void addNode(int data) {
        //Create a new node
        Node newNode = new Node(data);
        //Checks if the list is empty
        if(head == null) {
            //if list is empty, both head and tail will point to new node
            head = newNode;
            tail = newNode;
        }
        else {
            //newNode will be added after tail such that tail's next will point to newNode
            tail.next = newNode;
            //newNode will become new tail of the list
            tail = newNode;
        }
        //Size will count the number of nodes present in the list
        size++;
    }
    //this function will add the new node at the middle of the list.
    public void addInMid(int data){
        //Create a 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;
        }
        else {
            Node temp, current;
            //Store the mid position of the list
            int count = (size % 2 == 0) ? (size/2) : ((size+1)/2);
            //Node temp will point to head
            temp = head;
            current = null;
            //Traverse through the list till the middle of the list is reached
            for(int i = 0; i < count; i++) {
                //Node current will point to temp
                current = temp;
                //Node temp will point to node next to it.
                temp = temp.next;
            }
            //current will point to new node
            current.next = newNode;
            //new node will point to temp
            newNode.next = temp;
        }
        size++;
    }
    //display() will display all the nodes present in 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 pointer
            System.out.print(current.data + " ");
            current = current.next;
        }
        System.out.println();
    }
    public static void main(String[] args) {
        InsertMid sList = new InsertMid();
        //Adds data to the list
        sList.addNode(1);
        sList.addNode(2);
        System.out.println("08a0c785d8984645a0acd8f67497391d");
        sList.display();
        //Inserting node '3' in the middle
        sList.addInMid(3);
        System.out.println( "e58365378ef441a1bafc21604b66f814");
        sList.display();
        //Inserting node '4 in the middle
        sList.addInMid(4);
            System.out.println("Updated List: ");
            sList.display();
        }
    }
输出:
Original list:
1 2
Updated List:
1 3 2
Updated List:
1 3 4 2
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4