Java.util.LinkedList.poll()、pollFirst()、pollLast() 以及Java中的示例
Java 的链表类提供了一个允许“基于队列”工作的函数,称为 poll()。该函数不仅返回删除的第一个元素,而且在删除的同时显示它们,因此在日常生活问题和竞技编程中也有很多用途。 poll() 有3种变体,本文讨论了这三种变体。
1. poll() :此方法检索并删除此列表的头部(第一个元素) 。
Declaration :
public E poll()
Return Value :
This method returns the first element of this list, or null if this list is empty.
// Java code to demonstrate the working
// of poll() in linked list
import java.util.*;
public class LinkedListPoll {
public static void main(String[] args)
{
// Declaring a LinkedList
LinkedList list = new LinkedList();
// adding elements
list.add("Geeks");
list.add(4);
list.add("Geeks");
list.add(8);
// printing the list
System.out.println("The initial Linked List is : " + list);
// using poll() to retrieve and remove the head
// removes and displays "Geeks"
System.out.println("Head element of the list is : " + list.poll());
// printing the resultant list
System.out.println("Linked List after removal using poll() : " + list);
}
}
输出 :
The initial Linked List is : [Geeks, 4, Geeks, 8]
Head element of the list is : Geeks
Linked List after removal using poll() : [4, Geeks, 8]
2. pollFirst() :此方法检索并删除此列表的第一个元素,如果此列表为空,则返回 null。
Declaration :
public E pollFirst()
Return Value :
This method returns the first element of this list, or null if this list is empty
// Java code to demonstrate the working
// of pollFirst() in linked list
import java.util.*;
public class LinkedListPollFirst {
public static void main(String[] args)
{
// Declaring a LinkedList
LinkedList list = new LinkedList();
// adding elements
list.add("Geeks");
list.add(4);
list.add("Geeks");
list.add(8);
// printing the list
System.out.println("The initial Linked List is : " + list);
// using pollFirst() to retrieve and remove the head
// removes and displays "Geeks"
System.out.println("Head element of the list is : " + list.pollFirst());
// printing the resultant list
System.out.println("Linked List after removal using pollFirst() : " + list);
}
}
输出 :
The initial Linked List is : [Geeks, 4, Geeks, 8]
Head element of the list is : Geeks
Linked List after removal using pollFirst() : [4, Geeks, 8]
3. pollLast() :此方法检索并删除此列表的最后一个元素,如果此列表为空,则返回 null。
Declaration :
public E pollLast()
Return Value :
This method returns the last element of this list, or null if this list is empty.
// Java code to demonstrate the working
// of pollLast() in linked list
import java.util.*;
public class LinkedListPollLast {
public static void main(String[] args)
{
// Declaring a LinkedList
LinkedList list = new LinkedList();
// adding elements
list.add("Geeks");
list.add(4);
list.add("Geeks");
list.add(8);
// printing the list
System.out.println("The initial Linked List is : " + list);
// using pollLast() to retrieve and remove the tail
// removes and displays 8
System.out.println("Tail element of the list is : " + list.pollLast());
// printing the resultant list
System.out.println("Linked List after removal using pollLast() : " + list);
}
}
输出 :
The initial Linked List is : [Geeks, 4, Geeks, 8]
Tail element of the list is : 8
Linked List after removal using pollLast() : [Geeks, 4, Geeks]
实际应用:此函数在“队列管理”系统以及可以想到的“第一淘汰”游戏中具有潜在用途。下面讨论前一个例子。
// Java code to demonstrate the practical
// application of poll() in linked list
import java.util.*;
public class LinkedListPollApp {
public static void main(String[] args)
{
// Declaring a LinkedList
LinkedList list = new LinkedList();
// adding queue entry of people
// in order
list.add("Astha");
list.add("Shambhavi");
list.add("Nikhil");
list.add("Manjeet");
// printing the list
System.out.println("The initial queue is : " + list);
System.out.print("The order of exit is : ");
while (!list.isEmpty()) {
// using poll() to display the order of exit from queue
System.out.print(list.poll() + " <-- ");
}
}
}
输出 :
The initial queue is : [Astha, Shambhavi, Nikhil, Manjeet]
The order of exit is : Astha <-- Shambhavi <-- Nikhil <-- Manjeet <--