Scientech Easy Java LinkedList Example Programs | Scientech Easy

Friday, December 7, 2018

Java LinkedList Example Programs

In the previous tutorial, we have learned the basic of LinkedList in Java. In this tutorial, we will see various Java LinkedList example programs related to all methods of linked list in a simple and step by step. 

How to add an Element to the LinkedList in Java?

In this example program, we will add the element in different ways using add(), add(int index, Object o), addFirst(Object o), addLast(Object o), addAll() methods of list interface and Deque interface.
Program source code 1:
    package linkedListExample; import java.util.ArrayList; import java.util.LinkedList; import java.util.Vector; public class AddingExample { public static void main(String[] args) { // Create a LinkedList object. LinkedList list=new LinkedList(); // Call add() method for adding Heterogeneous element using reference variable list. list.add("One"); list.add(2); list.add(null); // null elements are allowed in the linked list. list.add("Four"); System.out.println("Initial LinkedList order: " +list); // Adding an element at the specified in the linked list. list.add(3, "Three"); // Adding an element at the first position of the linked list using addFirst() method of Deque interface. System.out.println("LinkedList Elements after adding the first element"); list.addFirst("Zero"); System.out.println(list); // Adding an element at the end of the list using addLast() method of Deque interface. System.out.println("LinkedList Elements after adding the last element"); list.addLast("Five"); System.out.println(list); // Adding all the elements from existing an ArrayList collection to the end of the LinkedList. // Create an ArrayList object. ArrayList al=new ArrayList(); al.add("Six"); al.add(7); al.add("Eight"); // Call addAll() method to add all elements to the end of the linked list. list.addAll(al); System.out.println("LinkedList Elements after adding all elements from ArrayList"); System.out.println(list); // Adding all elements from an existing Vector collection at the specified position of the LinkedList. Vector v=new Vector(); v.add(7.5); v.add(7.8); list.addAll(9, v); System.out.println("Linked list Elements after adding all elements from the vector"); System.out.println(list); } }
    Output: Initial LinkedList order: [One, 2, null, Four] LinkedList Elements after adding the first element [Zero, One, 2, null, Three, Four] LinkedList Elements after adding the last element [Zero, One, 2, null, Three, Four, Five] LinkedList Elements after adding all elements from ArrayList [Zero, One, 2, null, Three, Four, Five, Six, 7, Eight] Linked list Elements after adding all elements from the vector [Zero, One, 2, null, Three, Four, Five, Six, 7, 7.5, 7.8, Eight]

How to remove an Element from LinkedList in Java?

We will remove elements from the list in different ways using methods such as remove(), removeFirst(), removeLast().
Program source code 2:
    package linkedListExample; import java.util.ArrayList; import java.util.LinkedList; public class RemovingElementExample { public static void main(String[] args) { // Create a generic LinkedList object with the type of String. LinkedList<String> list=new LinkedList<String>(); int size=list.size(); System.out.println("Size of the Linked list: " +size); // Adding elements of String type. list.add("Zero"); list.add("First"); list.add("Second"); list.add(null); // null elements are allowed in the linked list. list.add("Fourth"); list.add("25"); System.out.println("Initial LinkedList order: " +list); // Removing the first element from the linked list using removeFirst() method. list.removeFirst(); System.out.println("LinkedList Elements after removing the first element"); System.out.println(list); // Removing the last element from the list using removeLast() method. System.out.println("LinkedList Elements after removing the last element"); list.removeLast(); System.out.println(list); // Removing element at the specified position from the list. list.remove(2); System.out.println("LinkedList Elements after removing the element at index position 2 "); System.out.println(list); // Adding all the elements from existing an ArrayList collection to the end of the LinkedList. // Creating a generic ArrayList object of String type. ArrayList<String> al=new ArrayList<String>(); al.add("Third"); al.add("Fourth"); list.addAll(2, al); list.removeLastOccurrence("Fourth"); System.out.println("LinkedList Elements after removing last occurrence"); System.out.println(list); } }
Java LinkedList Example, LinkedList example program in Java.
    Output: Size of the Linked list: 0 Initial LinkedList order: [Zero, First, Second, null, Fourth, 25] LinkedList Elements after removing the first element [First, Second, null, Fourth, 25] LinkedList Elements after removing the last element [First, Second, null, Fourth] LinkedList Elements after removing the element at index position 2 [First, Second, Fourth] LinkedList Elements after removing last occurrence [First, Second, Third, Fourth]

How to get an Element from LinkedList in Java?

Here, we will get an element from the list in different ways using get(), getFirst(), and getLast() method. Let's see an example program.
Program source code 3: 
    package linkedListExample; import java.util.LinkedList; public class GetElementExample { public static void main(String[] args) { // Create a linked list object. LinkedList<Integer> list=new LinkedList<Integer>(); // Adding even numbers from 0 to 20 as elements in the list. for(int i=0 ; i<=20; i++){ if(i%2==0) // It will check even number. list.add(i); } // Call getFirst() method to get first even number. Object firstEvenNumber=list.getFirst(); // Return type of getFirst() methods is an Object. System.out.println("First even number: " +firstEvenNumber); Object lastEvenNumber=list.getLast(); System.out.println("Last even number: " +lastEvenNumber); Object getElement=list.get(5); System.out.println("Even number at index 5: " +getElement); } }
    Output: First even number: 0 Last even number: 20 Even number at index 5: 10

How to retrieve and remove an element in the Java LinkedList?

We can retrieve and remove an element from the LinkedList using peekFirst(), peekLast(), pollFirst(), and pollLast() method.
peekFirst(): This method retrieves the first element from the list but does not remove the element from the list.
peekLast(): This method retrieves the last element from the list but does not remove.
pollFirst(): This method retrieves and removes the first element from the list.
pollLast(): This method retrieves and removes the last element from the list.

Program source code 4:
    package linkedListExample; import java.util.LinkedList; public class PeakPollExample { public static void main(String[] args) { // Create a LinkedList object. LinkedList<String> list=new LinkedList<String>(); // Adding elements in the list. list.add("INDIA"); list.add("USA"); list.add("JAPAN"); list.add("UK"); list.add("CANADA"); System.out.println("Initial LinkedList order"); System.out.println(list); // Call peek() method to retrieve the first element from the list. Object firstElement=list.peekFirst(); // Return type of this method is an Object. System.out.println("Retrieve the first element: " +firstElement); Object lastElement=list.peekLast(); System.out.println("Retrieve the last element: " +lastElement); // Call pollLast() to retrieve and remove the last element from the list. Object element1=list.pollLast(); System.out.println("Retrieve and remove the last element: " +element1); System.out.println("LinkedList Element after using pollLast() method"); System.out.println(list); } }
    Output: Initial LinkedList order [INDIA, USA, JAPAN, UK, CANADA] Retrieve the first element: INDIA Retrieve the last element: CANADA Retrieve and remove the last element: CANADA LinkedList Element after using pollLast() method [INDIA, USA, JAPAN, UK]

How to Pop and Push an element from and onto the stack in the Java LinkedList?

Pop() method: This method is equivalent to removeFirst() method that removes and returns the first element from the list. In other words, It pops an element from the stack represented by the list.
Push() method: This method is equivalent to addFirst() method that adds an element at the first position of the list. In other words, It pushes an element onto the stack represented by the list.
Let's see an example program.
Program source code 5: 
    package linkedListExample; import java.util.LinkedList; public class PeakPollExample { public static void main(String[] args) { // Create a LinkedList object. LinkedList<Character> list=new LinkedList<Character>(); // Adding elements in the list. list.add('A'); list.add('B'); list.add('C'); list.add('D'); list.add('E'); System.out.println("Initial LinkedList order"); System.out.println(list); Object element=list.pop(); // Return type of this method is an Object. System.out.println("Pops Element: " +element); list.push('B'); System.out.println("LinkedList Element after pushing"); System.out.println(list); } }
    Output: Initial LinkedList order [A, B, C, D, E] Pops Element: A LinkedList Element after pushing [B, B, C, D, E]
Final words 
We hope that this article has covered all the practical example programs related to methods of the Java LinkedList. All the Java LinkedList example programs are very important to clear concepts.


Popular