이 기사에서는 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