Here's a basic implementation of a singly linked list in
Java. This custom implementation includes common operations such as adding
elements, removing elements, and displaying the list.
Custom Singly Linked List Implementation
java
public class
CustomLinkedList<E> { private Node<E> head; private int size; // Node class to represent each element
in the list private static class Node<E> { E data; Node<E> next; Node(E data, Node<E> next) { this.data = data; this.next = next; } } // Default constructor public CustomLinkedList() { head = null; size = 0; } // Add element to the end of the list public void add(E element) { if (head == null) { head = new Node<>(element,
null); } else { Node<E> current = head; while (current.next != null) { current = current.next; } current.next = new
Node<>(element, null); } size++; } // Add element at a specific index public void add(int index, E element) { checkIndexForAdd(index); if (index == 0) { head = new Node<>(element,
head); } else { Node<E> previous =
getNode(index - 1); Node<E> newNode = new
Node<>(element, previous.next); previous.next = newNode; } size++; } // Get element at a specific index public E get(int index) { checkIndex(index); return getNode(index).data; } // Set element at a specific index public void set(int index, E element) { checkIndex(index); getNode(index).data = element; } // Remove element at a specific index public void remove(int index) { checkIndex(index); if (index == 0) { head = head.next; } else { Node<E> previous =
getNode(index - 1); previous.next =
previous.next.next; } size--; } // Get the size of the list public int size() { return size; } // Check if the list is empty public boolean isEmpty() { return size == 0; } // Display the list public void printList() { Node<E> current = head; while (current != null) { System.out.print(current.data +
" -> "); current = current.next; } System.out.println("null"); } // Get the node at a specific index private Node<E> getNode(int index)
{ Node<E> current = head; for (int i = 0; i < index; i++) { current = current.next; } return current; } // Check if index is within bounds for
add operations private void checkIndexForAdd(int index)
{ if (index < 0 || index > size)
{ throw new
IndexOutOfBoundsException("Index: " + index + ", Size: "
+ size); } } // Check if index is within bounds for
get and remove operations private void checkIndex(int index) { if (index < 0 || index >= size)
{ throw new
IndexOutOfBoundsException("Index: " + index + ", Size: "
+ size); } } // Main method for testing public static void main(String[] args) { CustomLinkedList<String> list =
new CustomLinkedList<>(); list.add("A"); list.add("B"); list.add("C"); list.add(1, "D"); // Add
"D" at index 1 System.out.println("List after
additions:"); list.printList(); // Output: A ->
D -> B -> C -> null System.out.println("Element at
index 2: " + list.get(2)); // Output: Element at index 2: B list.set(2, "E"); System.out.println("List after
setting index 2 to 'E':"); list.printList(); // Output: A ->
D -> E -> C -> null list.remove(1); System.out.println("List after
removing element at index 1:"); list.printList(); // Output: A ->
E -> C -> null System.out.println("Size of the
list: " + list.size()); // Output: Size of the list: 3 } } |
Key Components
- Node
Class: Represents each element in the linked list with a data field
and a next reference.
- Add
Methods:
- add(E
element): Adds an element to the end of the list.
- add(int
index, E element): Adds an element at a specific index.
- Get
Method: Retrieves the element at a specified index.
- Set
Method: Updates the element at a specified index.
- Remove
Method: Removes the element at a specified index.
- Size
and Empty Check: Provides methods to get the size of the list and
check if it is empty.
- Print
List: Displays the contents of the list.
- Index
Checking: Methods to check for index validity, ensuring operations do
not go out of bounds.
This implementation provides a basic overview of how singly
linked lists operate, including node management, indexing, and size tracking.
You can expand this implementation to include additional functionalities or
optimizations as needed.
Custom SinglyLinkList |
0 Comments