📌  相关文章
📜  数据结构示例-循环链表的中间插入新节点

📅  最后修改于: 2020-10-15 06:22:04             🧑  作者: Mango

问:程序在循环链表的中间插入一个新节点。

说明

在此程序中,我们创建一个循环链接列表,并在列表中间插入一个新节点。如果列表为空,头和尾都将指向新节点。如果列表不为空,则我们将计算列表的大小,然后将其除以2,以获得列表中点,需要在其中插入新节点。

在列表中间插入新节点之后。

考虑上图;新节点需要添加到列表的中间。首先,我们计算大小(在这种情况下为4)。因此,要获得中点,我们将其除以2并将其存储在变量计数中。我们将定义两个节点current和temp,这样temp将指向head,而current将指向temp之前的节点。我们通过将temp递增到temp来遍历列表直到到达中点.next,然后在current和temp之间插入新节点。当前的下一个节点将是新节点,而新的下一个节点将是临时节点。

算法

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

示例:

Python

#Represents the node of list.
class Node:
  def __init__(self,data):
    self.data = data;
    self.next = None;
 
class CreateList:
    #Declaring head and tail pointer as null.
    def __init__(self):
      self.head = Node(None);
      self.tail = Node(None);
      self.head.next = self.tail;
      self.tail.next = self.head;
      self.size = 0;
    
    #This function will add the new node to the list.
    def add(self,data):
       newNode = Node(data);
       #Checks if the list is empty.
       if self.head.data is None:
         #If list is empty, both head and tail would point to new node.
         self.head = newNode;
         self.tail = newNode;
         newNode.next = self.head;
       else:
         #tail will point to new node.
         self.tail.next = newNode;
         #New node will become new tail.
         self.tail = newNode;
         #Since, it is circular linked list tail will points to head.
         self.tail.next = self.head;
       #Size will count the number of element in the list
       self.size = self.size+1;
    
    #This function will add the new node at the middle of the list.
    def addInMid(self,data):
        newNode = Node(data);
        #Checks if the list is empty.
        if(self.head == None):
            #If list is empty, both head and tail would point to new node.
            self.head = newNode;
            self.tail = newNode;
            newNode.next = self.head;
        else:
            #Store the mid-point of the list
            count = (self.size//2) if (self.size % 2 == 0) else ((self.size+1)//2);
            #temp will point to head
            temp = self.head;
            for i in range(0,count):
                #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;
        self.size = self.size+1;
        
    #Displays all the nodes in the list
    def display(self):
      current = self.head;
      if self.head is None:
        print("List is empty");
        return;
      else:
          #Prints each node by incrementing pointer.
          print(current.data),
          while(current.next != self.head):
              current = current.next;
              print(current.data),
    
 
class CircularLinkedList:
    cl = CreateList();
    #Adds data to the list
    cl.add(1);
    cl.add(2);
    cl.add(3);
    cl.add(4);
    print("Original list: ");
    cl.display();
    #Inserting node '5' in the middle
    cl.addInMid(5);
    print("\nUpdated List: ");
    cl.display();
    #Inserting node '6' in the middle
    cl.addInMid(6);
    print("\nUpdated List: ");
    cl.display();

输出:

Original list: 
1 2 3 4
Updated List: 
1 2 5 3 4
Updated List: 
1 2 5 6 3 4

C

#include 
#include 
#include 
 
//Represents the node of list.
struct node{
    int data;
    struct node *next;
};
 
//Declaring head and tail pointer as null.
struct node *head = NULL;
struct node *tail = NULL;
int size = 0;
 
//This function will add the new node to the list.
void add(int data){
    //Create new node
    struct node *newNode = (struct node*)malloc(sizeof(struct node));
    newNode->data = 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 point to head.
        tail->next = head;
    }
  size++;
}
 
//This function will add the new node at the middle of the list.
void addInMid(int data){
    int i;
    struct node *newNode = (struct node*)malloc(sizeof(struct node));
    newNode->data = 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{
           struct node *temp, *current = NULL;
        //Store the mid-point of the list
        int count = (size % 2 == 0) ? (size/2) : ((size+1)/2);    
        //temp will point to head
        temp = head;
        for(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++;
}
 
//This function will display the nodes of circular linked list
void display(){
    struct node *current = head;
    if(head == NULL){
        printf("List is empty");
    }
    else{
         do{
          //Prints each node by incrementing pointer.
            printf("%d ", current->data);
            current = current->next;
        }while(current != head);
        printf("\n");
    }
}
    
int main()
{
  //Adds data to the list
    add(1);
    add(2);
    add(3);
    add(4);
    
    printf("Original list:\n ");
    display();
    
    //Inserting node '5' in the middle
    addInMid(5);
    printf("Updated List:\n ");
    display();
    
    //Inserting node '6' in the middle
    addInMid(6);
    printf("Updated List:\n ");
    display();
   
   return 0;
}

输出:

Original list: 
1 2 3 4
Updated List: 
1 2 5 3 4
Updated List: 
1 2 5 6 3 4

JAVA

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

C#

using System; 
namespace CircularLinkedList 
{                     
    public class Program
    {
        //Represents the node of list.
        public class Node{
            public T data;
            public Node next;
            public Node(T value) {
                data = value;
                next = null;
            }
        }
        
        public class CreateList{
            protected Node head = null;             
             protected Node tail = null;
            int size = 0;
            
            //This function will add the new node to the list.
            public void add(T data){
                //Create new node
                Node newNode = new Node(data);
                //Checks if the list is empty.
                if(head == null){
                    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++;
            }
            
            //This function will add the new node at the middle of the list.
            public void addInMid(T 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){
                    Console.WriteLine("List is empty");
                }
                else{
                    do{
                        //Prints each node by incrementing pointer.
                        Console.Write(" "+ current.data);
                        current = current.next;
                    }while(current != head);
                }    
            }
        }
        
        public static void Main()
        {
            
        CreateList cl = new CreateList();
            //Adds data to the list
            cl.add(1);
            cl.add(2);
            cl.add(3);
            cl.add(4);
            
            Console.WriteLine("Original list: ");
            cl.display();
            
            //Inserting node '5' in the middle
            cl.addInMid(5);
            Console.WriteLine();
            Console.WriteLine("Updated List: ");
            cl.display();
            
            //Inserting node '6' in the middle
            cl.addInMid(6);
            Console.WriteLine();
            Console.WriteLine("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

PHP:




data = $data;
        $this->next = NULL;
    }
}
class CreateList{
    //Declaring head and tail pointer as null.
    private $head;
    private $tail;
    private $size;
    function __construct(){
        $this->head = NULL;
        $this->tail = NULL;
        $this->size = 0;
    }
    
    //This function will add the new node to the list.
    function add($data){
        //Create new node
        $newNode = new Node($data);
        //Checks if the list is empty.
        if($this->head == NULL){
            //If list is empty, both head and tail would point to new node.
            $this->head = $newNode;
            $this->tail = $newNode;
            $newNode->next = $this->head;
        }
        else{
            //tail will point to new node.
            $this->tail->next = $newNode;
            //New node will become new tail.
            $this->tail = $newNode;
            //Since, it is circular linked list tail will point to head.
            $this->tail->next = $this->head;
        }
        $this->size++;
    }
    
    //This function will add the new node at the middle of the list.
    function addInMid($data){
        $newNode = new Node($data);
        //Checks if the list is empty.
        if($this->head == NULL){
            //If list is empty, both head and tail would point to new node.
            $this->head = $newNode;
            $this->tail = $newNode;
            $newNode->next = $this->head;
        }
        else{
            //Store the mid-point of the list
            $count = ($this->size % 2 == 0) ? ($this->size/2):(($this->size+1)/2);    
            //temp will point to head
            $temp = $this->head;
            for($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;
        }
        $this->size++;
    }
    
    //Displays all the nodes in the list
    function display() {
        $current = $this->head;
        if($this->head == NULL) {
            echo "List is empty";
        }
        else {
             do{
                echo(" $current->data");
                $current = $current->next;
            }while($current != $this->head);
            echo "
"; } } } $cl = new CreateList(); //Adds data to the list $cl->add(1); $cl->add(2); $cl->add(3); $cl->add(4); echo "Original list:
"; $cl->display(); //Inserting node '5' in the middle $cl->addInMid(5); echo "Updated List:
"; $cl->display(); //Inserting node '6' in the middle $cl->addInMid(6); echo "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