Skip to content

The addition of DoubleCircularLinkedList.java #6023

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.thealgorithms.datastructures.lists;

public class DoubleCircularLinkedList<E> {

private static final class Node<E> {
Node<E> next;
Node<E> previous;
E value;

private Node(E value, Node<E> next) {
this.value = value;
this.next = next;
}
}

private int size;
private Node<E> head = null;
private Node<E> 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<E> 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<E> 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<E> before = head;
for (int i = 0; i < pos; i++) {
before = before.next; // Move to the node before the one to be removed
}

Node<E> 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<String> k = new DoubleCircularLinkedList<>();
k.append("khalid");
k.append("fahad");
k.append("nora");
System.out.println(k.getSize());
System.out.println(k.toString());
}
}
17 changes: 9 additions & 8 deletions src/main/java/com/thealgorithms/datastructures/lists/README.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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.
Loading