এই আর্টিকেলে আমরা বুঝব কীভাবে LinkedList-এ লুপ সনাক্ত করতে হয়। একটি লিঙ্কড লিস্ট হলো ডেটা স্ট্রাকচারির একটি শৃঙ্খলা, যা লিংকসের মাধ্যমে একসাথে সংযুক্ত থাকে। লিঙ্কড লিস্ট হলো লিংকসের একটি শৃঙ্খলা যাতে আইটেম থাকে। প্রতিটি লিংক অন্য একটি লিংকের সাথে সংযোগ ধারণ করে।
ধরুন ইনপুট:
প্রোগ্রাম রান করুন
চাহিদা আউটপুট:
লিংকড লিস্টে লুপ বিদ্যমান
অ্যালগরিদম
Step 1 - START Step 2 - ভেরিয়েবল ডিক্লেয়ার Step 3 - মান ডিফাইন Step 4 - প্রাসঙ্গিক মেম্বারের সাথে ক্লাস ডিফাইন Step 5 - ক্লাসের ইনস্ট্যান্স তৈরি করুন এবং নোড ইনিশিয়ালাইজ Step 6 - লুপ চেক করার ফাংশন ডিফাইন Step 7 - HashSet তৈরি করুন এবং শীর্ষ নোডে এলিমেন্ট যোগ করুন Step 8 - প্রতিটি ইটারেশনে নোডকে পরবর্তী এলিমেন্টে পয়েন্ট করান Step 9 - main মেথডে ইনস্ট্যান্স তৈরি করুন এবং 'push' মেথড দিয়ে এলিমেন্ট যোগ করুন Step 10 - 'check_loop' মেথড কল করুন এবং কনসোলে মেসেজ দেখান Step 11 - STOP
উদাহরণ ১
এখানে ট্রাভার্সাল মেথড ব্যবহার করে লুপ খুঁজে বের করা হয়েছে।
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 s = new HashSet();
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
উদাহরণ ২
এখানে অপারেশনগুলো ফাংশনে এনক্যাপসুলেট করা হয়েছে অবজেক্ট-ওরিয়েন্টেড প্রোগ্রামিং প্রদর্শন করতে।
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