Programme Java pour détecter la boucle dans une LinkedList

Dans cet exemple, nous allons apprendre à détecter s'il existe une boucle dans LinkedList en Java.

Pour comprendre cet exemple, vous devez avoir la connaissance des rubriques de programmation Java suivantes:

  • Liste LinkedList Java
  • Méthodes Java

Exemple: détecter une boucle dans une LinkedList

 class LinkedList ( // create an object of Node class // represent the head of the linked list Node head; // static inner class static class Node ( int value; // connect each node to next node Node next; Node(int d) ( value = d; next = null; ) ) // check if loop is present public boolean checkLoop() ( // create two references at start of LinkedList Node first = head; Node second = head; while(first != null && first.next !=null) ( // move first reference by 2 nodes first = first.next.next; // move second reference by 1 node second = second.next; // if two references meet // then there is a loop if(first == second) ( return true; ) ) return false; ) public static void main(String() args) ( // create an object of LinkedList LinkedList linkedList = new LinkedList(); // assign values to each linked list node linkedList.head = new Node(1); Node second = new Node(2); Node third = new Node(3); Node fourth = new Node(4); // connect each node of linked list to next node linkedList.head.next = second; second.next = third; third.next = fourth; // make loop in LinkedList fourth.next = second; // printing node-value System.out.print("LinkedList: "); int i = 1; while (i <= 4) ( System.out.print(linkedList.head.value + " "); linkedList.head = linkedList.head.next; i++; ) // call method to check loop boolean loop = linkedList.checkLoop(); if(loop) ( System.out.println("There is a loop in LinkedList."); ) else ( System.out.println("There is no loop in LinkedList."); ) ) )

Production

 LinkedList: 1 2 3 4 Il y a une boucle dans LinkedList.

Dans l'exemple ci-dessus, nous avons implémenté une LinkedList en Java. Nous avons utilisé l'algorithme de recherche de cycle de Floyd pour vérifier s'il y a une boucle dans LinkedList.

Notez le code à l'intérieur de la checkLoop()méthode. Ici, nous avons deux variables nommées première et seconde qui traversent les nœuds dans LinkedList.

  • premier - traverser avec 2 nœuds à une seule itération
  • seconde - traversée avec 1 nœud à une seule itération

Deux nœuds traversent à des vitesses différentes. Par conséquent, ils se rencontreront s'il y a une boucle LinkedList.

Articles intéressants...