📜  循环链表元素排序的Java程序

📅  最后修改于: 2021-09-05 08:30:39             🧑  作者: Mango

循环链表中,每个节点都指向序列中的下一个节点,但最后一个节点指向列表中的第一个节点。在这里,创建一个循环链表,并按升序对循环链表进行排序。

排序前的循环链表:

循环链表

排序后的循环链表:

排序循环链表

方法:

  1. 取两个指针: Current指向节点的头部, Temp指向Current 的下一个节点
  2. 现在,每次迭代比较当前指针的值温度指针的值
    这里出现两种情况

    情况一:如果当前指针的值大于Temp指针的值
    交换当前指针和临时指针的值。
    将临时指针移动到下一个节点
    情况二:如果当前指针的值小于等于Temp指针的值
    将临时指针移动到下一个节点

  3. 现在继续这样做直到temp.next !=head of the list
  4. 完成步骤 3 后,将 Current 移动到下一个节点并重复步骤 1,2,3 。
  5. 每次迭代都会将列表中最短的元素固定到正确的位置。
  6. 重复以上步骤直到Current。下一个 != 列表头。

让我们看看这对给定循环链表的第一个节点是如何工作的

下面是上述方法的实现:

Java
// Java Program to Sort the Elements
// of the Circular Linked List
  
import java.io.*;
  
public class GFG {
    // Stores Information about Node of List
    public class Node {
        int data;
        Node next;
        public Node(int data) { this.data = data; }
    }
  
    // Declaring Head of the Node
    public Node head_of_node = null;
  
    // A last pointer to help append values to our list
    public Node last = null;
  
    // Add method adds values to the end of the list
    public void add(int data)
    {
        Node newNode = new Node(data);
        if (head_of_node == null) {
            head_of_node = newNode;
            last = newNode;
            newNode.next = head_of_node;
        }
        else {
            last.next = newNode;
            last = newNode;
            last.next = head_of_node;
        }
    }
    // Sort_List method sorts the circular
    // linked list Using the algorithm
    public void Sort_List()
    {
  
        // current pointer pointing to the head of the list
        Node current = head_of_node;
  
        // a temp pointer
        Node temp = null;
  
        // variable value helps in swap of the values
        int value;
  
        // this is the Algorithm discussed above
        if (head_of_node == null) {
            System.out.println("Your list is empty");
        }
        else {
            while (current.next != head_of_node) {
                temp = current.next;
                while (temp != head_of_node) {
                    if (current.data > temp.data) {
                        value = current.data;
                        current.data = temp.data;
                        temp.data = value;
                    }
                    temp = temp.next;
                }
                current = current.next;
            }
        }
    }
    // Print_list method iterates through the list and
    // prints the values stored in the list
    public void Print_List()
    {
        Node current = head_of_node;
        if (head_of_node == null) {
            System.out.println("Your list is empty");
        }
        else {
            do {
                System.out.print(" " + current.data);
                current = current.next;
            } while (current != head_of_node);
            System.out.println();
        }
    }
  
    // Driver code
    public static void main(String[] args)
    {
  
        GFG circular_list = new GFG();
        circular_list.add(10);
        circular_list.add(6);
        circular_list.add(3);
        circular_list.add(8);
        circular_list.add(4);
  
        System.out.print("Original List -->     ");
        circular_list.Print_List();
        circular_list.Sort_List();
        System.out.print("List after Sorting--> ");
        circular_list.Print_List();
    }
}


输出
Original List -->      10 6 3 8 4
List after Sorting-->  3 4 6 8 10

时间复杂度: O(N 2 )