Saturday, December 28, 2024
Google search engine
HomeLanguagesJavaJava.util.LinkedList.offer(), offerFirst(), offerLast() in Java

Java.util.LinkedList.offer(), offerFirst(), offerLast() in Java

Linked list also has a function that does the work of flexible addition of elements and helps addition both at front and back of the list, these functions literally “offer” the facility and named offer(). Three types are available and are discussed in this very article below. 

1. offer(E e) : This method adds the specified element as the tail (last element) of this list. 

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


Output : 

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

2. offerFirst(E e) : This method inserts the specified element at the front of this list.  

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


Output : 

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

3. offerLast(E e) : This method inserts the specified element at the end of this list. 

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


Output : 

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

Practical Application : This quality of “flexible addition” of these functions can be done in cases of priority addition in queues where elements having a greater no. than threshold have to be handled before the elements lesser than that. Small piece of code below discusses this.  

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<Integer> list = new LinkedList<Integer>();
        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);
    }
}


Output : 

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

This article is contributed by Astha Tyagi. If you like Lazyroar and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the Lazyroar main page and help other Geeks. 
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 

RELATED ARTICLES

Most Popular

Recent Comments