কম্পিউটার

লিঙ্কডলিস্ট বাস্তবায়নের জন্য জাভা প্রোগ্রাম


এই নিবন্ধে, আমরা বুঝতে পারব কিভাবে লিঙ্কড-লিস্ট বাস্তবায়ন করতে হয়। java.util.LinkedList ক্লাসঅপারেশনের কাজগুলো আমরা দ্বিগুণ-লিঙ্কড তালিকার জন্য আশা করতে পারি। ক্রিয়াকলাপগুলি যেগুলি তালিকায় সূচী করে সেগুলি শুরু বা শেষ থেকে তালিকাকে অতিক্রম করবে, যেটি নির্দিষ্ট সূচকের কাছাকাছি।

নীচে একই -

এর একটি প্রদর্শন রয়েছে৷

ধরুন আমাদের ইনপুট হল

Run the program

কাঙ্খিত আউটপুট হবে

The elements of the linked list are:
100 150 200 250

অ্যালগরিদম

Step 1 - START
Step 2 - Create a class with the required members.
Step 3 - Define an ‘insert’ function to add elements to the list.
Step 4 - In the ‘main’ method, create a new instance of the class.
Step 5 - Create a list, and add elements to it using the ‘insert’ method.
Step 6 - Iterate over the list, and display the value present in the current node.
Step 7 - Move on to the next node and perform the same operation.
Step 8 - Do this until the end of the list is reached.
Step 9 - Display the result
Step 10 - Stop

উদাহরণ 1

এখানে, আমরা 'প্রধান' ফাংশনের অধীনে সমস্ত ক্রিয়াকলাপ একসাথে আবদ্ধ করি।

public class Demo {
   Node head;
   static class Node {
      int data;
      Node next_element;
      Node(int element){
      data = element;
      next_element = null;
   }
}
public static Demo insert(Demo input_list, int data){
   Node new_node = new Node(data);
   new_node.next_element = null;
   if (input_list.head == null) {
      input_list.head = new_node;
   }
   else {
      Node last = input_list.head;
      while (last.next_element != null) {
         last = last.next_element;
      }
      last.next_element = new_node;
   }
   return input_list;
   }
   public static void main(String[] args){
      Demo input_list = new Demo();
      System.out.print("A linked list is declared: \n");
      input_list = insert(input_list, 100);
      input_list = insert(input_list, 150);
      input_list = insert(input_list, 200);
      input_list = insert(input_list, 250);
      Node current_node = input_list.head;
      System.out.print("The elements of the linked list are: \n");
         while (current_node != null) {
         System.out.print(current_node.data + " ");
         current_node = current_node.next_element;
      }
   }
}

আউটপুট

A linked list is declared:
The elements of the linked list are:
100 150 200 250

উদাহরণ 2

এখানে, আমরা ক্রিয়াকলাপগুলিকে অবজেক্ট ওরিয়েন্টেড প্রোগ্রামিং প্রদর্শনকারী ফাংশনে অন্তর্ভুক্ত করি।

public class Demo {
   Node head;
   static class Node {
      int data;
      Node next_element;
      Node(int element){
         data = element;
         next_element = null;
      }
   }
   public static Demo insert(Demo input_list, int data){
      Node new_node = new Node(data);
      new_node.next_element = null;
      if (input_list.head == null) {
         input_list.head = new_node;
      }
      else {
         Node last = input_list.head;
         while (last.next_element != null) {
            last = last.next_element;
         }
         last.next_element = new_node;
      }
      return input_list;
   }
   public static void print_list(Demo input_list){
      Node current_node = input_list.head;
      System.out.print("The elements of the linked list are: \n");
      while (current_node != null) {
         System.out.print(current_node.data + " ");
         current_node = current_node.next_element;
      }
   }
   public static void main(String[] args){
      Demo input_list = new Demo();
      System.out.print("A linked list is declared: \n");
      input_list = insert(input_list, 100);
      input_list = insert(input_list, 150);
      input_list = insert(input_list, 200);
      input_list = insert(input_list, 250);
      print_list(input_list);
   }
}

আউটপুট

A linked list is declared:
The elements of the linked list are:
100 150 200 250

  1. জাভা প্রোগ্রাম একটি তালিকা থেকে একটি সাবলিস্ট সরাতে

  2. জাভা প্রোগ্রাম একটি তালিকাকে দুই ভাগে ভাগ করে

  3. প্রাইভেট কনস্ট্রাক্টর বাস্তবায়নের জন্য জাভা প্রোগ্রাম

  4. একাধিক উত্তরাধিকার বাস্তবায়নের জন্য জাভা প্রোগ্রাম