Last Updated : 10 Dec, 2018
Linked list class offers the functionality to "
look into" the first and last elements of the list and hence can be useful in cases where
only the retrieval is requiredand not necessarily the deletion is required. Three functionalities are present and all are discussed in this article.
1. peek() :This method
retrieves, but does not remove, the head (first element) of this list.
Declaration : public E peek() Return Value : This method returns the head of this list, or null if this list is empty.Java
// Java code to demonstrate the working
// of peek() in LinkedList
import java.util.*;
public class LinkedPeek1 {
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 list is :" + list);
// peek at the head of the list
// Prints 1st element, "Geeks"
System.out.println("Head of the list : " + list.peek());
}
}
Output:
The initial list is :[Geeks, 4, Geeks, 8] Head of the list : Geeks
This method
retrieves, but does not remove, the first element of this list, or returns null if this list is empty. This works similar to peek().
Declaration : public E peekFirst() Return Value : This method returns the first element of this list, or null if this list is emptyJava
// Java code to demonstrate the working
// of peekFirst() in LinkedList
import java.util.*;
public class LinkedPeek2 {
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 list is :" + list);
// peek at the first element of the list
// Prints 1st element, "Geeks"
System.out.println("First element of the list is : " + list.peekFirst());
}
}
Output:
The initial list is :[Geeks, 4, Geeks, 8] First element of the list is : Geeks
This method
retrieves, but does not remove, the
last elementof this list, or returns null if this list is empty.
Declaration public E peekLast() Return Value This method returns the last element of this list, or null if this list is emptyJava
// Java code to demonstrate the working
// of peekLast() in LinkedList
import java.util.*;
public class LinkedPeek3 {
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 list is :" + list);
// peek at the last element of the list
// Prints last element, 8
System.out.println("Last element of the list is : " + list.peekLast());
}
}
Output:
The initial list is :[Geeks, 4, Geeks, 8] Last element of the list is : 8
The practical application that can be thought of is that this can be used in potentially the game of cards where the
individuals can peek the first or last elementof the deck on asking which element they want to see. Code below explains the working.
Java
// Java code to demonstrate the application
// of peek()
import java.util.*;
public class LinkedPeekApp {
public static void main(String[] args)
{
// declaring a LinkedList
LinkedList list = new LinkedList();
// adding elements in deck
list.add(5);
list.add(4);
list.add("Jack");
list.add(8);
list.add("King");
// printing the list
System.out.println("The initial deck is :" + list);
String d = "upper";
System.out.println("The element chosen to peek is : " + d);
if (d == "upper")
System.out.println("The Upper element is : " + list.peekFirst());
else
System.out.println("The Lower element is : " + list.peekLast());
}
}
Output :
The initial deck is :[5, 4, Jack, 8, King] The element chosen to peek is : upper The Upper element is : 5
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4