LinkedList'in orta öğesini tek bir yinelemede almak için Java Programı

Bu örnekte, bağlantılı listenin orta öğesini Java'da tek bir yinelemede almayı öğreneceğiz.

Bu örneği anlamak için, önce aşağıdaki öğreticileri ziyaret ettiğinizden emin olun,

  • Java LinkedList Sınıfı
  • LinkedList Veri Yapısı

Örnek 1: LinkedList'in orta öğesini tek bir aramada alın

 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; ) ) 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); // connect each node of linked list to next node linkedList.head.next = second; second.next = third; // print the linked list Node pointer = linkedList.head; System.out.print("LinkedList: " ); while (pointer != null) ( System.out.print(pointer.value + " "); pointer = pointer.next; ) // Find the middle element Node ptr1 = linkedList.head; Node ptr2 = linkedList.head; while (ptr1.next != null) ( // increase the ptr1 by 2 and ptr2 by 1 // if ptr1 points to last element // ptr2 will points to middle element ptr1 = ptr1.next; if(ptr1.next !=null) ( ptr1 = ptr1.next; ptr2 = ptr2.next; ) ) System.out.println("Middle Element: " + ptr2.value); ) )

Çıktı

 LinkedList: 1 2 3 Orta Eleman: 2

Yukarıdaki örnekte, bağlantılı liste veri yapısını Java'da uyguladık. Daha sonra bağlantılı listenin orta öğesini tek bir döngüde buluruz. Koda dikkat edin,

  while (ptr1.next != null) ( // increase the ptr1 by 2 and ptr2 by 1 // if ptr1 points to last element // ptr2 will points to middle element ptr1 = ptr1.next; if(ptr1.next !=null) ( ptr1 = ptr1.next; ptr2 = ptr2.next; ) )

Burada ptr1 ve ptr2 olmak üzere iki değişkenimiz var. Bu değişkenleri bağlantılı listeyi yinelemek için kullanırız.

Her yinelemede, ptr1 iki düğüme erişecek ve ptr2, bağlantılı listenin tek düğümüne erişecektir.

Şimdi, ptr1 bağlantılı listenin sonuna ulaştığında, ptr2 ortada olacaktır. Bu şekilde, bağlantılı listenin ortasını tek bir iterasyonda alabiliyoruz.

Örnek 2: LinkedList sınıfını kullanarak LinkedList'in orta öğesini alın

 import java.util.LinkedList; class Main ( public static void main(String() args)( // create a linked list using the LinkedList class LinkedList animals = new LinkedList(); // Add elements to LinkedList animals.add("Dog"); animals.addFirst("Cat"); animals.addLast("Horse"); System.out.println("LinkedList: " + animals); // access middle element String middle = animals.get(animals.size()/2); System.out.println("Middle Element: " + middle); ) )

Çıktı

 LinkedList: (Kedi, Köpek, At) Orta Öğe: Köpek

Yukarıdaki örnekte, LinkedListbağlantılı liste veri yapısını uygulamak için sınıfı kullandık . İfadeye dikkat edin,

 animals.get(animals.size()/2)
  • size () / 2 - ortadaki elemanın konumunu döndürür
  • get () - ortadaki elemanı döndürür

Ilginç makaleler...