Skip to content

Create Circular Doubly Linked List #5834

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 1 commit into from
Closed
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,55 @@
class CircularDoublyLinkedList {
private Node head;

// Constructor
public CircularDoublyLinkedList() {
head = null;
}

// Insert a node at the end
public void insertEnd(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
head.next = head;
head.prev = head;
} else {
Node tail = head.prev;

tail.next = newNode;
newNode.prev = tail;
newNode.next = head;
head.prev = newNode;
}
}

// Display the list
public void display() {
if (head == null) {
System.out.println("List is empty.");
return;
}

Node current = head;
do {
System.out.print(current.data + " ");
current = current.next;
} while (current != head);
System.out.println();
}

// Delete a node by value
public void delete(int value) {
if (head == null) return;

Node current = head;

do {
if (current.data == value) {
if (current == head && current.next == head) {
head = null; // List is now empty
} else {
current.prev.next = current.next;
current.next.prev = current.prev;
if (current == head) {
head = current.next; // Move head
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

??? Where is the rest of the file?