Program Java na získanie stredného prvku LinkedList v jednej iterácii

V tomto príklade sa naučíme získať prostredný prvok prepojeného zoznamu v jednej iterácii v prostredí Java.

Ak chcete porozumieť tomuto príkladu, najskôr navštívte nasledujúce výukové programy,

  • Trieda Java LinkedList
  • Dátová štruktúra LinkedList

Príklad 1: Získajte prostredný prvok LinkedList v jednom vyhľadávaní

 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); ) )

Výkon

 LinkedList: 1 2 3 Stredný prvok: 2

Vo vyššie uvedenom príklade sme implementovali dátovú štruktúru prepojeného zoznamu v prostredí Java. Potom nájdeme prostredný prvok prepojeného zoznamu v jednej slučke. Všimnite si kód,

  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; ) )

Tu máme dve premenné ptr1 a ptr2. Tieto premenné používame na iteráciu v prepojenom zozname.

V každej iterácii bude mať ptr1 prístup k dvom uzlom a ptr2 bude mať prístup k jednému uzlu prepojeného zoznamu.

Teraz, keď ptr1 dosiahne koniec prepojeného zoznamu, bude ptr2 v strede. Týmto spôsobom sme schopní dostať stred prepojeného zoznamu v jednej iterácii.

Príklad 2: Získajte prostredný prvok LinkedList pomocou triedy LinkedList

 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); ) )

Výkon

 LinkedList: (Mačka, pes, kôň) Stredný prvok: pes

Vo vyššie uvedenom príklade sme LinkedListtriedu použili na implementáciu dátovej štruktúry prepojeného zoznamu. Všimnite si výraz,

 animals.get(animals.size()/2)
  • size () / 2 - vráti pozíciu stredného prvku
  • get () - vráti prvok v strednej polohe

Zaujímavé články...