In Java, a linked list is a linear data structure where each element (node) contains a value and a reference (or link) to the next node in the sequence. The java.util.LinkedList class provides an implementation of the linked list data structure, offering various operations to manipulate the list. Below are the common operations on a linked list in Java:
1. Insertion
- Add at the Beginning (
addFirst(element)): Adds an element at the front of the list.
- Add at the End (
addLast(element)) or (add(element))**: Adds an element at the end of the list.
- Add at a Specific Index (
add(index, element)): Inserts an element at the specified position.
2. Deletion
- Remove First Element (
removeFirst()): Removes the first element from the list.
- Remove Last Element (
removeLast()): Removes the last element from the list.
- Remove Element by Value (
remove(element)): Removes the first occurrence of the specified element.
- Remove Element at a Specific Index (
remove(index)): Removes the element at the specified position.
3. Access
- Get First Element (
getFirst()): Returns the first element in the list.
- Get Last Element (
getLast()): Returns the last element in the list.
- Get Element at a Specific Index (
get(index)): Returns the element at the specified position.
4. Search
- Contains Element (
contains(element)): Checks if the list contains the specified element.
- Index of Element (
indexOf(element)): Returns the index of the first occurrence of the specified element.
- Last Index of Element (
lastIndexOf(element)): Returns the index of the last occurrence of the specified element.
5. Update
- Set Element at Index (
set(index, element)): Replaces the element at the specified position with the new element.
6. Size and Check
- Size (
size()): Returns the number of elements in the list.
- Is Empty (
isEmpty()): Checks if the list is empty.
7. Clear
- Clear (
clear()): Removes all elements from the list.
8. Traversal
- Iterator: Allows traversing the list using an iterator.
- ListIterator: Allows bidirectional traversal and modification of the list.
Example Code
import java.util.LinkedList;
public class LinkedListExample {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
// Add elements
list.add("A");
list.add("B");
list.addFirst("0");
list.addLast("C");
list.add(2, "1");
// Access elements
System.out.println("First element: " + list.getFirst());
System.out.println("Last element: " + list.getLast());
System.out.println("Element at index 2: " + list.get(2));
// Remove elements
list.removeFirst();
list.removeLast();
list.remove(1);
// Update element
list.set(1, "X");
// Check size and emptiness
System.out.println("Size: " + list.size());
System.out.println("Is list empty? " + list.isEmpty());
// Clear list
list.clear();
}
}
Custom Implementation
If you're implementing a linked list manually (without using java.util.LinkedList), you would typically define a Node class and a LinkedList class with methods to perform these operations. Here’s a basic structure:
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
class CustomLinkedList {
Node head;
// Methods for add, remove, get, etc.
// Example: Add at the beginning
public void addFirst(int data) {
Node newNode = new Node(data);
newNode.next = head;
head = newNode;
}
// Other methods...
}
These operations form the basis of working with linked lists in Java, whether using the built-in LinkedList class or implementing one manually.