Java
 Computer >> コンピューター >  >> プログラミング >> Java

LinkedListのループを検出するJavaプログラム


この記事では、LinkedListのループを検出する方法を理解します。リンクリストは、リンクを介して相互に接続された一連のデータ構造です。リンクリストは、アイテムを含む一連のリンクです。各リンクには、別のリンクへの接続が含まれています。

以下は同じのデモンストレーションです-

入力がであると仮定します −

Run the program

必要な出力は

The loop exists in the linked list

アルゴリズム

Step 1 - START
Step 2 - Declare namely
Step 3 - Define the values.
Step 4 - Define the class with the relevant members.
Step 5 - Create an instance of the class, and initialize the nodes.
Step 6 - Define functions to check if it is a loop.
Step 7 - For this, create a HashSet, and add elements to the topmost node.
Step 8 - Point the node to the next element after every iteration.
Step 9 - In the main method, create an instance and add elements to the list using the ‘push’ method.
Step 10 - Call the ‘check_loop’ method, and display the relevant message on the console.
Step 11 - Stop

例1

ここでは、トラバーサルメソッドを使用してループを見つけます。

import java.util.*;
public class Demo {
   static Node head;
   static class Node {
      int data;
      Node next;
      Node(int d){
         data = d;
         next = null;
      }
   }
   static public void push(int new_data){
      Node new_node = new Node(new_data);
      new_node.next = head;
      head = new_node;
   }
   static boolean check_loop(Node head){
      HashSet<Node> s = new HashSet<Node>();
      while (head != null) {
         if (s.contains(head))
            return true;
         s.add(head);
         head = head.next;
      }
      return false;
   }
   public static void main(String[] args){
      System.out.println("The required packages have been imported");
      Demo input_list = new Demo();
      input_list.push(45);
      input_list.push(60);
      input_list.push(75);
      input_list.push(90);
      input_list.head.next.next.next.next = input_list.head;
      if (check_loop(head))
         System.out.println("The loop exists in the linked list");
      else
         System.out.println("The loop doesnot exists in the linked list");
      }
   }

出力

The required packages have been imported
The loop exists in the linked list

例2

ここでは、操作をオブジェクト指向プログラミングを示す関数にカプセル化します。

public class Demo {
Node head;
static class Node {
int value;
Node next;
Node(int d) {
value = d;
next = null;
}
}
public boolean check_loop() {
Node first_node = head;
Node second_node = head;
while(first_node != null && first_node.next !=null) {
first_node = first_node.next.next;
second_node = second_node.next;
if(first_node == second_node) {
return true;
}
}
return false;
}
public static void main(String[] args) {
Demo input_list = new Demo();
input_list.head = new Node(45);
Node second_node = new Node(60);
Node third_node = new Node(75);
Node fourth_node = new Node(90);
input_list.head.next = second_node;
second_node.next = third_node;
third_node.next = fourth_node;
fourth_node.next = second_node;
System.out.print("The elements of the linked list are: ");
int i = 1;
while (i <= 4) {
System.out.print(input_list.head.value + " ");
input_list.head = input_list.head.next;
i++;
}
boolean loop = input_list.check_loop();
if(loop) {
System.out.println("\nThere is a loop in the linked list.");
}
else {
System.out.println("\nThere is no loop in the linked list.");
}
}
}

出力

The required packages have been imported
The loop exists in the linked list

  1. 正方形の領域を見つけるJavaプログラム

    この記事では、正方形の面積を見つける方法を理解します。正方形の面積は、次の式を使用して計算されます- side*side i.e. s2 以下は同じのデモンストレーションです- 正方形の辺がsの場合、正方形の面積はs 2で与えられます。 − 入力 入力が-であると仮定します Length of the side : 4 出力 必要な出力は-になります Area of the square : 16 アルゴリズム Step 1 - START Step 2 - Declare 2 integer values namely my_side and my_area. S

  2. 回文をチェックするJavaプログラム

    回文数は、逆にしたときに同じままの数です。たとえば、121、313、525などです。 例 回文をチェックする例を見てみましょう- public class Palindrome {    public static void main(String[] args) {       int a = 525, revVal = 0, remainder, val;       val = a;       System.out.println("Number to be che