Skip to content

Commit

Permalink
Update CircularDoublyLinkedList.js
Browse files Browse the repository at this point in the history
  • Loading branch information
debnath003 authored Oct 9, 2023
1 parent 42b8013 commit 9355002
Showing 1 changed file with 67 additions and 75 deletions.
142 changes: 67 additions & 75 deletions Data-Structures/Linked-List/CircularDoublyLinkedList.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,21 @@ class Node {
* Creates a new Node with the given element.
* @param {*} element - The element to be stored in the node.
*/
constructor (element) {
this.element = element
this.next = null
this.prev = null
constructor(element) {
this.element = element;
this.next = null;
this.prev = null;
}
}

class CircularDoublyLinkedList {
/**
* Creates an empty Circular Doubly Linked List.
*/
constructor () {
this.length = 0
this.head = null
this.tail = null
}

/**
* Appends an element to the end of the list.
* @param {*} element - The element to be appended.
*/
append (element) {
const node = new Node(element)

if (!this.head) {
this.head = node
this.tail = node
node.next = node // Circular reference
node.prev = node // Circular reference
} else {
node.prev = this.tail
node.next = this.head
this.tail.next = node
this.head.prev = node
this.tail = node
}

this.length++
constructor() {
this.length = 0;
this.head = null;
this.tail = null;
}

/**
Expand All @@ -51,98 +28,113 @@ class CircularDoublyLinkedList {
* @param {*} element - The element to be inserted.
* @returns {boolean} - True if the insertion was successful, false otherwise.
*/
insert (position, element) {
insertAt(position, element) {
if (position >= 0 && position <= this.length) {
const node = new Node(element)
let current = this.head
let previous = null
let index = 0
const node = new Node(element);
let current = this.head;
let previous = null;
let index = 0;

if (position === 0) {
node.next = current
node.prev = this.tail
this.head = node
current.prev = node
this.tail.next = node
if (!this.head) {
this.head = node;
this.tail = node;
node.next = node; // Circular reference
node.prev = node; // Circular reference
} else {
node.next = current;
node.prev = this.tail;
this.head = node;
current.prev = node;
this.tail.next = node;
}
} else {
while (index++ < position) {
previous = current
current = current.next
previous = current;
current = current.next;
}
node.next = current
node.prev = previous
previous.next = node
current.prev = node
node.next = current;
node.prev = previous;
previous.next = node;
current.prev = node;

if (position === this.length) {
this.tail = node
this.tail = node;
}
}

this.length++
return true
this.length++;
return true;
} else {
return false
return false;
}
}

/**
* Appends an element to the end of the list.
* @param {*} element - The element to be appended.
*/
append(element) {
return this.insertAt(this.length, element);
}

/**
* Removes and returns the element at the specified position.
* @param {number} position - The position of the element to be removed.
* @returns {*} - The removed element, or null if the position is invalid.
*/
removeAt (position) {
removeAt(position) {
if (position >= 0 && position < this.length) {
let current = this.head
let previous = null
let index = 0
let current = this.head;
let previous = null;
let index = 0;

if (position === 0) {
this.head = current.next
this.head.prev = this.tail
this.tail.next = this.head
this.head = current.next;
this.head.prev = this.tail;
this.tail.next = this.head;
if (this.length === 1) {
this.tail = null
this.tail = null;
}
} else {
while (index++ < position) {
previous = current
current = current.next
previous = current;
current = current.next;
}
previous.next = current.next
current.next.prev = previous
previous.next = current.next;
current.next.prev = previous;

if (position === this.length - 1) {
this.tail = previous
this.tail = previous;
}
}

this.length--
return current.element
this.length--;
return current.element;
} else {
return null
return null;
}
}

/**
* Iterator over the elements in the list.
*/
*elements() {
let currentNode = this.head
if (!currentNode) return
let currentNode = this.head;
if (!currentNode) return;
do {
yield currentNode.element
currentNode = currentNode.next
} while (currentNode !== this.head)
yield currentNode.element;
currentNode = currentNode.next;
} while (currentNode !== this.head);
}

/**
* Checks if the list is empty.
* @returns {boolean} - True if the list is empty, false otherwise.
*/
isEmpty () {
return this.length === 0
isEmpty() {
return this.length === 0;
}
}

export { CircularDoublyLinkedList }
export { CircularDoublyLinkedList };

0 comments on commit 9355002

Please sign in to comment.