Linked-list Program
JAVA LINKED LIST-10 basic programs
In the previous chapter, you learned about the ArrayList class. The LinkedList class is almost identical to the ArrayList:
Program 1:Simple java list program of adding elements.
CODE:
// Import the LinkedList class
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
LinkedList<String> cars = new LinkedList<String>();
cars.add(“Volvo”);
cars.add(“BMW”);
cars.add(“Ford”);
cars.add(“Mazda”);
System.out.println(cars);
}
}
OUTPUT:
When To Use
Use an ArrayList for storing and accessing data, and LinkedList to manipulate data.
Different method in linked list.
Program 2: Adds an item to the beginning of the list.
CODE:
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
LinkedList<String> cars = new LinkedList<String>();
cars.add(“Volvo”);
cars.add(“BMW”);
cars.add(“Ford”);
// Use addFirst() to add the item to the beginning
cars.addFirst(“Mazda”);
System.out.println(cars);
}
}
OUTPUT:
[Mazda, Volvo, BMW, Ford]
Program 3: Add an item to the end of the list
CO
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
LinkedList<String> cars = new LinkedList<String>();
cars.add(“Volvo”);
cars.add(“BMW”);
cars.add(“Ford”);
// Use addLast() to add the item to the end
cars.addLast(“Mazda”);
System.out.println(cars);
}
}
OUTPUT:
[Volvo, BMW, Ford, Mazda]
Program 4: Remove an item from the beginning of the list.
CODE:
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
LinkedList<String> cars = new LinkedList<String>();
cars.add(“Volvo”);
cars.add(“BMW”);
cars.add(“Ford”);
cars.add(“Mazda”);
// Use removeFirst() remove the first item from the list
cars.removeFirst();
System.out.println(cars);
}
}
OUTPUT:
[BMW, Ford, Mazda]
Program 5: Remove an item from the end of the list
CODE:
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
LinkedList<String> cars = new LinkedList<String>();
cars.add(“Volvo”);
cars.add(“BMW”);
cars.add(“Ford”);
cars.add(“Mazda”);
// Use removeLast() remove the last item from the list
cars.removeLast();
System.out.println(cars);
}
}
OUTPUT:
[Volvo, BMW, Ford]
Program 6: Get the item at the beginning of the list
CODE:
OUTPUT:
Program 7: Get the item at the end of the list
CODE:
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
LinkedList<String> cars = new LinkedList<String>();
cars.add(“Volvo”);
cars.add(“BMW”);
cars.add(“Ford”);
cars.add(“Mazda”);
// Use getLast() to display the last item in the list
System.out.println(cars.getLast());
}
}
OUTPUT:
Mazda
Program 8:Cheking particular element is present or not.
CODE:
// Java code to illustrate boolean contains()
import java.io.*;
import java.util.LinkedList;
public class LinkedListDemo {
public static void main(String args[]) {
// Creating an empty LinkedList
LinkedList<String> list = new LinkedList<String>();
// Use add() method to add elements in the list
list.add(“Somnath”);
list.add(“for”);
list.add(“Somnath”);
list.add(“10”);
list.add(“20”);
// Output the list
System.out.println(“LinkedList:” + list);
// Check if the list contains “Hello”
System.out.println(“\nDoes the List contains ‘Hello’: “
+ list.contains(“Hello”));
// Check if the list contains “20”
System.out.println(“Does the List contains ‘20’: “
+ list.contains(“20”));
// Check if the list contains “Geeks”
System.out.println(“Does the List contains ‘Somnath’: “
+ list.contains(“Somnath”));
}
}
OUTPUT:
LinkedList:[Somnath, for, Somnath, 10, 20]
Does the List contains ‘Hello’: false
Does the List contains ‘20’: true
Does the List contains ‘Somnath’: true
Program 9:Iterating element
Syntax:ListIterator new_list = LinkedList.listIterator(int index);
CODE:
// Java code to illustrate listIterator()
import java.io.*;
import java.util.LinkedList;
import java.util.ListIterator;
public class LinkedListDemo {
public static void main(String args[])
{
// Creating an empty LinkedList
LinkedList<String> list = new LinkedList<String>();
// Use add() method to add elements in the list
list.add(“Sachin”);
list.add(“for”);
list.add(“Sachin”);
list.add(“100”);
list.add(“70”);
// Displaying the linkedlist
System.out.println(“LinkedList:” + list);
// Setting the ListIterator at a specified position
ListIterator list_Iter = list.listIterator(2);
// Iterating through the created list from the position
System.out.println(“The list is as follows:”);
while(list_Iter.hasNext()){
System.out.println(list_Iter.next());
}
}
}
OUTPUT:
LinkedList:[Somnath, for, Somnath, 100, 70]
The list is as follows:
Geeks
100
70
LinkedList indexOf() method in Java
The Java.util.LinkedList.indexOf(Object element) method is used to check and find the occurrence of a particular element in the list. If the element is present then the index of the first occurrence of the element is returned otherwise -1 is returned if the list does not contain the element.
Program 10:Generating index of the given element
CODE:
// Java code to illustrate indexOf()
import java.io.*;
import java.util.LinkedList;
public class LinkedListDemo {
public static void main(String args[]) {
// Creating an empty LinkedList
LinkedList<String> list = new LinkedList<String>();
// Use add() method to add elements in the list
list.add(“Virat”);
list.add(“Anushka”);
list.add(“Virushka”);
list.add(“33”);
list.add(“32”);
// Displaying the list
System.out.println(“LinkedList:” + list);
// The first position of an element
// is returned
System.out.println(“The first occurrence of Geeks is at index:”
+ list.indexOf(“Virushka”));
System.out.println(“The first occurrence of 10 is at index: “
+ list.indexOf(“32”));
}
}
OUTPUT:
LinkedList:[Virat, Anushka, Virushka, 33, 32]
The first occurrence of Geeks is at index:2
The first occurrence of 10 is at index: 4
Thank you ,there is another some method you can directly use.when u understood that’s method you will automatically know.Apart from there is doubly linked list and circular linked list topic is there do practise more on that