📜  Java.util.LinkedList.offer()、offerFirst()、offerLast() 在Java中

📅  最后修改于: 2022-05-13 01:55:24.336000             🧑  作者: Mango

Java.util.LinkedList.offer()、offerFirst()、offerLast() 在Java中

链表还有一个函数可以灵活地添加元素并帮助在列表的前后添加,这些函数字面意思是“提供”设施并命名为 offer()。三种类型可用,并在下面的这篇文章中讨论。

1. offer(E e) :该方法将指定元素添加为该列表的尾部(最后一个元素)。

Declaration : 
   public boolean offer(E e)
Parameters : 
    e: the element to add
Return Value : 
     This method returns true 
Java
// Java code to demonstrate the working
// of offer(E e) in linked list
import java.util.*;
public class LinkedListoffer1 {
 
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);
 
        // offering a new element
        // adds element at tail.
        list.offer("Astha");
 
        // printing the new list
        System.out.println("LinkedList after insertion using offer() : " + list);
    }
}


Java
// Java code to demonstrate the working
// of offerFirst(E e) in linked list
import java.util.*;
public class LinkedListOfferFirst {
 
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);
 
        // offering a new element
        // adds element at head.
        list.offerFirst("Astha");
 
        // printing the new list
        System.out.println("LinkedList after insertion using offerFirst() : " + list);
    }
}


Java
// Java code to demonstrate the working
// of offerLast(E e) in linked list
import java.util.*;
public class LinkedListOfferLast {
 
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);
 
        // offering a new element
        // adds element at end.
        list.offerLast("Astha");
 
        // printing the new list
        System.out.println("LinkedList after insertion using offerLast() : " + list);
    }
}


Java
// Java code to demonstrate the application
// of offer() in linked list
import java.util.*;
public class LinkedListOfferLast {
 
public static void main(String[] args)
    {
 
        // Declaring LinkedLists
        LinkedList list = new LinkedList();
        LinkedList prioList = new LinkedList();
 
        // adding elements
        list.add(12);
        list.add(4);
        list.add(8);
        list.add(10);
        list.add(3);
        list.add(15);
 
        // declaring threshold
        int thres = 10;
 
        // printing the list
        System.out.println("The initial Linked list is : " + list);
 
        while (!list.isEmpty()) {
 
            int t = list.poll();
 
            // adding >=10 numbers at front rest at back
            if (t >= 10)
                prioList.offerFirst(t);
            else
                prioList.offerLast(t);
        }
 
        // The resultant list is
        System.out.println("The prioritized Linked list is : " + prioList);
    }
}


输出 :

The initial Linked list is : [Geeks, 4, Geeks, 8]
LinkedList after insertion using offer() : [Geeks, 4, Geeks, 8, Astha]

2. offerFirst(E e) :此方法在此列表的前面插入指定元素。

Declaration : 
   public boolean offerFirst(E e)
Parameters : 
    e : the element to add
Return Value :  
    This method returns true

Java

// Java code to demonstrate the working
// of offerFirst(E e) in linked list
import java.util.*;
public class LinkedListOfferFirst {
 
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);
 
        // offering a new element
        // adds element at head.
        list.offerFirst("Astha");
 
        // printing the new list
        System.out.println("LinkedList after insertion using offerFirst() : " + list);
    }
}

输出 :

The initial Linked list is : [Geeks, 4, Geeks, 8]
LinkedList after insertion using offerFirst() : [Astha, Geeks, 4, Geeks, 8]

3. offerLast(E e) :此方法在此列表的末尾插入指定元素。

Declaration : 
     public boolean offerLast(E e)
Parameters : 
    e:the element to add
Return Value : 
    This method returns true

Java

// Java code to demonstrate the working
// of offerLast(E e) in linked list
import java.util.*;
public class LinkedListOfferLast {
 
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);
 
        // offering a new element
        // adds element at end.
        list.offerLast("Astha");
 
        // printing the new list
        System.out.println("LinkedList after insertion using offerLast() : " + list);
    }
}

输出 :

The initial Linked list is : [Geeks, 4, Geeks, 8]
LinkedList after insertion using offerLast() : [Geeks, 4, Geeks, 8, Astha]

实际应用:这些功能的这种“灵活添加”质量可以在元素具有更大编号的队列中优先添加的情况下完成。必须在小于阈值的元素之前处理比阈值。下面的一小段代码讨论了这一点。

Java

// Java code to demonstrate the application
// of offer() in linked list
import java.util.*;
public class LinkedListOfferLast {
 
public static void main(String[] args)
    {
 
        // Declaring LinkedLists
        LinkedList list = new LinkedList();
        LinkedList prioList = new LinkedList();
 
        // adding elements
        list.add(12);
        list.add(4);
        list.add(8);
        list.add(10);
        list.add(3);
        list.add(15);
 
        // declaring threshold
        int thres = 10;
 
        // printing the list
        System.out.println("The initial Linked list is : " + list);
 
        while (!list.isEmpty()) {
 
            int t = list.poll();
 
            // adding >=10 numbers at front rest at back
            if (t >= 10)
                prioList.offerFirst(t);
            else
                prioList.offerLast(t);
        }
 
        // The resultant list is
        System.out.println("The prioritized Linked list is : " + prioList);
    }
}

输出 :

The initial Linked list is : [12, 4, 8, 10, 3, 15]
The prioritized Linked list is : [15, 10, 12, 4, 8, 3]