LinkedList
LinkedList
- LinkedList is an implementation of a list interface where one can add any element at any index and remove any element from any index or position.
- Here, elements are doubly linked to one another which makes it easy to add or remove elements from both ends.
Various operations performed on the LinkedList:
1. Addition of elements in the LinkedList:
Example:
import java.util.*;
public class LinkedList {
public static void main(String args[]) {
LinkedList<Integer> linkedList =
new LinkedList<>(); // Instantiation of LinkedList
linkedList.add(10);
linkedList.add(20);
linkedList.add(30); // Adding Element to the LinkedList
linkedList.add(40);
linkedList.add(50);
System.out.println(linkedList);
}
}
Output:
[10, 20, 30, 40, 50]
2. Addition of element at particular index:
Example:
import java.util.*;
public class LinkedList {
public static void main(String args[]) {
LinkedList<Integer> linkedList =
new LinkedList<>(); // Instantiation of LinkedList
linkedList.add(10);
linkedList.add(20);
linkedList.add(30);
linkedList.add(40);
linkedList.add(50);
linkedList.add(2,60); // Adding Element at particular index
System.out.println(linkedList);
}
}
Output:
[10, 20, 60, 30, 40, 50]
3. Addition of a LinkedList in another LinkedList:
Example:
import java.util.*;
public class LinkedList {
public static void main(String args[]) {
LinkedList<Integer> linkedList1 =
new LinkedList<>(); // Instantiation of LinkedList1
linkedList1.add(10);
linkedList1.add(20);
linkedList1.add(30); // Adding Element in linkedList1
LinkedList<Integer> linkedList2 =
new LinkedList<>(); // Instantiation of LinkedList2
linkedList2.add(40);
linkedList2.add(50);
linkedList2.add(60); // Adding Element in linkedList2
linkedList1.addAll(linkedList2); // Adding linkedList2 in linkedList1
System.out.println(linkedList1);
}
}
Output:
[10, 20, 30, 40, 50, 60]
4. Removing a single element from LinkedList:
Example:
import java.util.*;
public class LinkedList {
public static void main(String args[]) {
LinkedList<Integer> linkedList =
new LinkedList<>(); // Instantiation of LinkedList
linkedList.add(10);
linkedList.add(20);
linkedList.add(30); // Adding Element in LinkedList
linkedList.add(40);
linkedList.add(50);
linkedList.remove(); // Removing single element from LinkedList
System.out.println(linkedList);
}
}
Output:
[20, 30, 40, 50]
5. Removing elements from a particular index:
Example:
import java.util.*;
public class LinkedList {
public static void main(String args[]) {
LinkedList<Integer> linkedList =
new LinkedList<>(); // Instantiation of LinkedList
linkedList.add(10);
linkedList.add(20);
linkedList.add(30); // Adding Element in LinkedList
linkedList.add(40);
linkedList.add(50);
linkedList.remove(1); // Removing element from index
System.out.println(linkedList);
}
}
Output:
[10, 30, 40, 50]
6. Removing all elements from LinkedList:
Example:
import java.util.*;
public class LinkedList {
public static void main(String args[]) {
LinkedList<Integer> linkedList =
new LinkedList<>(); // Instantiation of LinkedList1
linkedList.add(10);
linkedList.add(20);
linkedList.add(30); // Adding Element in linkedList
linkedList.add(40);
linkedList.add(50);
System.out.println("LinkedList before removal: "+linkedList);
linkedList.clear(); // removal of all element
System.out.println("LinkedList after removal: "+linkedList);
}
}
Output:
LinkedList before removal: [10, 20, 30, 40, 50]
LinkedList after removal: []
7. Updating element at particular index:
Example:
import java.util.*;
public class LinkedList {
public static void main(String args[]) {
LinkedList<Integer> linkedList =
new LinkedList<>(); // Instantiation of LinkedList1
linkedList.add(10);
linkedList.add(20);
linkedList.add(30); // Adding Element in linkedList
linkedList.add(40);
linkedList.add(50);
linkedList.set(2,25); // Updation of element
System.out.println(linkedList);
}
}
Output:
[10, 20, 25, 40, 50]
8. Check whether the given element is present inside the list or not:
Example:
import java.util.*;
public class LinkedList {
public static void main(String args[]) {
LinkedList<Integer> linkedList =
new LinkedList<>(); // Instantiation of LinkedList1
linkedList.add(10);
linkedList.add(20);
linkedList.add(30); // Adding Element in linkedList
linkedList.add(40);
linkedList.add(50);
System.out.println(linkedList.contains(50));
System.out.println(linkedList.contains(0));
}
}
Output:
true
false
9. Traversing:
Example:
import java.util.*;
public class LinkedList {
public static void main(String args[]) {
LinkedList<Integer> linkedList =
new LinkedList<>(); // Instantiation of LinkedList1
linkedList.add(10);
linkedList.add(20);
linkedList.add(30); // Adding Element in linkedList
linkedList.add(40);
linkedList.add(50);
// using for loop
System.out.println("Traversing using for loop: ");
for(int i=0;i<linkedList.size();i++)
{
System.out.print(linkedList.get(i)+" ");
}
// using foreach loop
System.out.println("\nTraversing using foreach loop: ");
for(int list:linkedList)
{
System.out.print(list+" ");
}
}
}
Output:
Traversing using for loop:
10 20 30 40 50
Traversing using foreach loop:
10 20 30 40 50
10. Iteration:
Example:
import java.util.*;
import java.util.Iterator;
public class Main {
public static void main(String args[]) {
LinkedList<Integer> linkedList =
new LinkedList<>(); // Instantiation of LinkedList1
linkedList.add(10);
linkedList.add(20);
linkedList.add(30); // Adding Element in linkedList
linkedList.add(40);
linkedList.add(50);
Iterator it = linkedList.iterator(); // Iteration
while(it.hasNext())
{
System.out.println(it.next());
}
}
}
Output:
10
20
30
40
50