Java Java.util.LinkedList.poll(), pollFirst(), pollLast()的示例

Java Java.util.LinkedList.poll(), pollFirst(), pollLast()的示例

Java的LinkedList类提供了一个函数,允许 “基于队列 “的工作,称为poll()。这个函数不仅可以返回删除的第一个元素,还 可以 在删除时 显示 它们,因此在日常生活问题和竞争性编程中可以有很多用途。poll()有 3种 变体,本文将讨论这三种变体。

1. poll() :该方法 检索删除 该列表的 头部(第一个元素)

声明:
public E poll()
返回值 :
该方法返回该列表的第一个元素,如果该列表为空,则返回空。

// 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。

声明:
public E pollFirst()
返回值 :
这个方法返回这个列表的第一个元素,如果这个列表是空的,则返回空。

// 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。

声明:
public E pollLast()
返回值 :
该方法返回该列表的最后一个元素,如果该列表为空,则返回空。

// 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 <-- 

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程