Skip to content
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

Added CircularDoublyLinkedList.js alongwith it's test code #1414

Open
wants to merge 36 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
fba34e6
Create RecursiveLinearSearch.js
debnath003 Oct 3, 2023
c865c35
Create RecursiveLinearSearch.test.js
debnath003 Oct 3, 2023
00c13af
Update RecursiveLinearSearch.js
debnath003 Oct 3, 2023
3185e53
Update RecursiveLinearSearch.js
debnath003 Oct 3, 2023
8e77a5e
Update RecursiveLinearSearch.js
debnath003 Oct 3, 2023
a6dd54f
Update RecursiveLinearSearch.js
debnath003 Oct 3, 2023
c1961f1
Update RecursiveLinearSearch.js
debnath003 Oct 3, 2023
a839184
Update RecursiveLinearSearch.test.js
debnath003 Oct 3, 2023
c1b4f0c
Delete Recursive/RecursiveLinearSearch.js
debnath003 Oct 3, 2023
dd8cb8f
Delete Recursive/test/RecursiveLinearSearch.test.js
debnath003 Oct 3, 2023
aaef3a8
Create Rectangle.js
debnath003 Oct 3, 2023
82e00cb
Update Rectangle.js
debnath003 Oct 3, 2023
2109aa6
Create Rectangle.test.js
debnath003 Oct 3, 2023
72765bd
Update Rectangle.js
debnath003 Oct 3, 2023
197cf3c
Delete Geometry/Rectangle.js
debnath003 Oct 3, 2023
3e36938
Delete Geometry/Test/Rectangle.test.js
debnath003 Oct 3, 2023
860dda7
Create CircularDoublyLinkedList.js
debnath003 Oct 3, 2023
9bdaf7d
Update CircularDoublyLinkedList.js
debnath003 Oct 3, 2023
834cd1b
Update CircularDoublyLinkedList.js
debnath003 Oct 3, 2023
12ec96e
Create CircularDoublyLinkedList.test.js
debnath003 Oct 3, 2023
b1f967a
Update CircularDoublyLinkedList.test.js
debnath003 Oct 3, 2023
808d740
Update CircularDoublyLinkedList.js
debnath003 Oct 3, 2023
393e591
Update CircularDoublyLinkedList.test.js
debnath003 Oct 3, 2023
46b572e
Update CircularDoublyLinkedList.js
debnath003 Oct 3, 2023
476aae1
Update CircularDoublyLinkedList.test.js
debnath003 Oct 3, 2023
4997d01
Update CircularDoublyLinkedList.test.js
debnath003 Oct 3, 2023
627c3ff
Update CircularDoublyLinkedList.js
debnath003 Oct 3, 2023
6eb5856
Update CircularDoublyLinkedList.test.js
debnath003 Oct 3, 2023
21c42c9
Clean up tests a bit
appgurueu Oct 4, 2023
eac5a0f
missed one "Check"
appgurueu Oct 4, 2023
1424735
createIterator -> elements
appgurueu Oct 4, 2023
aba8600
Replace toArray with elements iterator
appgurueu Oct 4, 2023
ab99a65
Remove `toArray` test
appgurueu Oct 4, 2023
42b8013
Remove usages of `toArray` in other tests
appgurueu Oct 4, 2023
9355002
Update CircularDoublyLinkedList.js
debnath003 Oct 9, 2023
3a2de53
Update CircularDoublyLinkedList.test.js
debnath003 Oct 9, 2023
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
140 changes: 140 additions & 0 deletions Data-Structures/Linked-List/CircularDoublyLinkedList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// Explanation:- https://www.javatpoint.com/circular-doubly-linked-list

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;
}
}

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

/**
* Inserts an element at the specified position in the list.
* @param {number} position - The position at which to insert the element.
* @param {*} element - The element to be inserted.
* @returns {boolean} - True if the insertion was successful, false otherwise.
*/
insertAt(position, element) {
if (position >= 0 && position <= this.length) {
const node = new Node(element);
let current = this.head;
let previous = null;
let index = 0;

if (position === 0) {
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;
}
node.next = current;
node.prev = previous;
previous.next = node;
current.prev = node;

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

this.length++;
return true;
} else {
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) {
if (position >= 0 && position < this.length) {
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;
if (this.length === 1) {
this.tail = null;
}
} else {
while (index++ < position) {
previous = current;
current = current.next;
}
previous.next = current.next;
current.next.prev = previous;

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

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

/**
* Iterator over the elements in the list.
*/
*elements() {
let currentNode = this.head;
if (!currentNode) return;
do {
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;
}
}

export { CircularDoublyLinkedList };
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { CircularDoublyLinkedList } from '../CircularDoublyLinkedList';

describe('CircularDoublyLinkedList', () => {
/**
* Creates a new CircularDoublyLinkedList and appends elements to it.
* @param {Array} elements - The elements to append.
* @returns {CircularDoublyLinkedList} - The created list.
*/
function createAndAppend(elements) {
const list = new CircularDoublyLinkedList();
elements.forEach((element) => list.append(element));
return list;
}

const expectList = (list, expected) => expect([...list.elements()]).toEqual(expected);

it('append', () => {
const list = createAndAppend([1]);

expectList(list, [1]);

list.append(2);
expectList(list, [1, 2]);
});

it('insertAt', () => {
const list = createAndAppend([1]);

list.insertAt(0, 20);
expectList(list, [20, 1]);

list.insertAt(1, 30);
expectList(list, [20, 30, 1]);
});

it('removeAt', () => {
const list = createAndAppend([10, 40, 30]);

list.removeAt(0);
expectList(list, [40, 30]);

list.removeAt(1);
expectList(list, [40]);
});

it('elements', () => {
const list = createAndAppend([10, 20, 30]);
expect([...list.elements()]).toEqual([10, 20, 30]);
});

it('isEmpty', () => {
const emptyList = new CircularDoublyLinkedList();
expect(emptyList.isEmpty()).toEqual(true);

const nonEmptyList = createAndAppend(['Hello']);
expect(nonEmptyList.isEmpty()).toEqual(false);
});
});