diff --git a/src/main/java/com/thealgorithms/datastructures/lists/DoubleCircularLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/DoubleCircularLinkedList.java new file mode 100644 index 000000000000..2fe28fa2a321 --- /dev/null +++ b/src/main/java/com/thealgorithms/datastructures/lists/DoubleCircularLinkedList.java @@ -0,0 +1,101 @@ +package com.thealgorithms.datastructures.lists; + +public class DoubleCircularLinkedList { + + private static final class Node { + Node next; + Node previous; + E value; + + private Node(E value, Node next) { + this.value = value; + this.next = next; + } + } + + private int size; + private Node head = null; + private Node tail = null; // keeping a tail pointer to keep track of the end of the list + + public DoubleCircularLinkedList() { + // creation of the dummy node + head = new Node<>(null, null); + tail = head; // Initially, head and tail point to the dummy node + head.next = head; // Circular reference to itself + size = 0; + } + + public int getSize() { + return size; + } + + public void append(E value) { + if (value == null) { + throw new NullPointerException("Cannot add null element to the list"); + } + + Node newNode = new Node<>(value, head); // Create new node pointing to head + if (size == 0) { + head.next = newNode; // Update head's next to point to the new node + newNode.previous = head; // Update the new node's previous to point to head + tail = newNode; // Update tail to the new node + } else { + tail.next = newNode; // Previous tail points to the new node + newNode.previous = tail; + tail = newNode; + head.previous = tail; + } + size++; + } + @Override +public String toString() { + if (size == 0) { + return "[]"; + } + StringBuilder sb = new StringBuilder("[ "); + + Node current = head.next; + while (current != head) { + sb.append(current.value); + if (current.next != head) { + sb.append(", "); + } + current = current.next; + } + sb.append(" ]"); + return sb.toString();} + + public E remove(int pos) { + if (pos >= size || pos < 0) { + throw new IndexOutOfBoundsException("Position cannot be greater than size or negative"); + } + + Node before = head; + for (int i = 0; i < pos; i++) { + before = before.next; // Move to the node before the one to be removed + } + + Node destroy = before.next; // Node to be removed + E saved = destroy.value; // Save the value to return + + before.next = destroy.next; // Bypass the node to be removed + destroy.next.previous = before; + + if (destroy == tail) { // If the tail is being removed + tail = before; // Update the tail + } + + size--; + return saved; // Return the removed value + } + + public static void main(String[] args) + { + DoubleCircularLinkedList k = new DoubleCircularLinkedList<>(); + k.append("khalid"); + k.append("fahad"); + k.append("nora"); + System.out.println(k.getSize()); + System.out.println(k.toString()); + } +} \ No newline at end of file diff --git a/src/main/java/com/thealgorithms/datastructures/lists/README.md b/src/main/java/com/thealgorithms/datastructures/lists/README.md index ea389c0422ce..a7a64c76ed75 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/README.md +++ b/src/main/java/com/thealgorithms/datastructures/lists/README.md @@ -1,7 +1,7 @@ ## Linked List ### Description -LinkedList is a data structure in which data is stored in a linear manner. It usually contains a data field and a link to the memory location of the next mode. +LinkedList is a data structure in which data is stored in a linear manner. It usually contains a data field and a link to the memory location of the next node. ### Structure @@ -23,10 +23,11 @@ The `next` variable points to the next node in the data structure and value stor ### File descriptions: 1. `CircleLinkedList.java` : A circular linked list where next pointer of last node points to first node of linked list. -2. `SinglyLinkedList.java` : The classic case of single links. -3. `CountSinglyLinkedListRecursion.java`: Recursively counts the size of a list. -4. `CreateAndDetectLoop.java` : Create and detect a loop in a linked list. -5. `DoublyLinkedList.java` : A modification of singly linked list which has a `prev` pointer to point to the previous node. -6. `MergeKSortedLinkedlist.java` : Merges K sorted linked list with mergesort (mergesort is also the most efficient sorting algorithm for linked list). -7. `RandomNode.java` : Selects a random node from given linked list and diplays it. -8. `SkipList.java` : Data Structure used for storing a sorted list of elements with help of a Linked list hierarchy that connects to subsequences of elements. +2. `DoubleCircularLinkedList`: A circular doubly linked list is a type of linked list where each node is connected to both its previous and next nodes, and the last node links back to the first node. +3. `SinglyLinkedList.java` : The classic case of single links. +4. `CountSinglyLinkedListRecursion.java`: Recursively counts the size of a list. +5. `CreateAndDetectLoop.java` : Create and detect a loop in a linked list. +6. `DoublyLinkedList.java` : A modification of singly linked list which has a `prev` pointer to point to the previous node. +7. `MergeKSortedLinkedlist.java` : Merges K sorted linked list with mergesort (mergesort is also the most efficient sorting algorithm for linked list). +8. `RandomNode.java` : Selects a random node from given linked list and diplays it. +9. `SkipList.java` : Data Structure used for storing a sorted list of elements with help of a Linked list hierarchy that connects to subsequences of elements.