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
import java.util.*;
public class LinkedListoffer1 {
public static void main(String[] args)
{
LinkedList list = new LinkedList();
list.add( "Geeks" );
list.add( 4 );
list.add( "Geeks" );
list.add( 8 );
System.out.println( "The initial Linked list is : " + list);
list.offer( "Astha" );
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
import java.util.*;
public class LinkedListOfferFirst {
public static void main(String[] args)
{
LinkedList list = new LinkedList();
list.add( "Geeks" );
list.add( 4 );
list.add( "Geeks" );
list.add( 8 );
System.out.println( "The initial Linked list is : " + list);
list.offerFirst( "Astha" );
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
import java.util.*;
public class LinkedListOfferLast {
public static void main(String[] args)
{
LinkedList list = new LinkedList();
list.add( "Geeks" );
list.add( 4 );
list.add( "Geeks" );
list.add( 8 );
System.out.println( "The initial Linked list is : " + list);
list.offerLast( "Astha" );
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
import java.util.*;
public class LinkedListOfferLast {
public static void main(String[] args)
{
LinkedList<Integer> list = new LinkedList<Integer>();
LinkedList prioList = new LinkedList();
list.add( 12 );
list.add( 4 );
list.add( 8 );
list.add( 10 );
list.add( 3 );
list.add( 15 );
int thres = 10 ;
System.out.println( "The initial Linked list is : " + list);
while (!list.isEmpty()) {
int t = list.poll();
if (t >= 10 )
prioList.offerFirst(t);
else
prioList.offerLast(t);
}
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 GeeksforGeeks 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 GeeksforGeeks 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.