ในบทความนี้ เราจะเข้าใจวิธีรับองค์ประกอบตรงกลางของ linkedList ในการทำซ้ำครั้งเดียว การดำเนินการคลาส java.util.LinkedList ดำเนินการที่เราคาดหวังได้สำหรับรายการที่เชื่อมโยงแบบทวีคูณ การดำเนินการที่จัดทำดัชนีลงในรายการจะข้ามผ่านรายการตั้งแต่ต้นหรือสิ้นสุด แล้วแต่ว่าสิ่งใดจะอยู่ใกล้ดัชนีที่ระบุ
ด้านล่างนี้เป็นการสาธิตสิ่งเดียวกัน -
สมมติว่าข้อมูลที่เราป้อนคือ −
Input linked list: 100 200 330
ผลลัพธ์ที่ต้องการจะเป็น −
The middle element of the list is: 200
อัลกอริทึม
Step 1 - START Step 2 - Declare a LinkedList namely input_list. Declare five node objects namely head, first_node, second_node, pointer_1, pointer_2. Step 3 - Define the values. Step 4 - Using a while loop, iterate over the linked list, get the middle element by traversing the list using pointer_1 and pointer_2 until pointer_1.next is not null. Step 5 - Display the pointer_2 value as result. Step 6 - Stop
ตัวอย่างที่ 1
ที่นี่ เราเชื่อมโยงการดำเนินการทั้งหมดเข้าด้วยกันภายใต้ฟังก์ชัน 'หลัก'
public class LinkedList {
Node head;
static class Node {
int value;
Node next;
Node(int d) {
value = d;
next = null;
}
}
public static void main(String[] args) {
LinkedList input_list = new LinkedList();
input_list.head = new Node(100);
Node second_node = new Node(200);
Node third_node = new Node(330);
input_list.head.next = second_node;
second_node.next = third_node;
Node current_node = input_list.head;
System.out.print("The linked list is defined as: " );
while (current_node != null) {
System.out.print(current_node.value + " ");
current_node = current_node.next;
}
Node pointer_1 = input_list.head;
Node pointer_2 = input_list.head;
while (pointer_1.next != null) {
pointer_1 = pointer_1.next;
if(pointer_1.next !=null) {
pointer_1 = pointer_1.next;
pointer_2 = pointer_2.next;
}
}
System.out.println("\nThe middle element of the list is: " + pointer_2.value);
}
} ผลลัพธ์
The linked list is defined as: 100 200 330 The middle element of the list is: 200
ตัวอย่างที่ 2
ในที่นี้ เราสรุปการดำเนินการเป็นฟังก์ชันที่แสดงการเขียนโปรแกรมเชิงวัตถุ
public class LinkedList {
Node head;
static class Node {
int value;
Node next;
Node(int d) {
value = d;
next = null;
}
}
static void get_middle_item(LinkedList input_list){
Node pointer_1 = input_list.head;
Node pointer_2 = input_list.head;
while (pointer_1.next != null) {
pointer_1 = pointer_1.next;
if(pointer_1.next !=null) {
pointer_1 = pointer_1.next;
pointer_2 = pointer_2.next;
}
}
System.out.println("\nThe middle element of the list is: " + pointer_2.value);
}
public static void main(String[] args) {
LinkedList input_list = new LinkedList();
input_list.head = new Node(100);
Node second_node = new Node(200);
Node third_node = new Node(330);
input_list.head.next = second_node;
second_node.next = third_node;
Node current_node = input_list.head;
System.out.print("The linked list is defined as: " );
while (current_node != null) {
System.out.print(current_node.value + " ");
current_node = current_node.next;
}
get_middle_item(input_list);
}
} ผลลัพธ์
The linked list is defined as: 100 200 330 The middle element of the list is: 200