-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Added CircularDoublyLinkedList.js alongwith it's test code #1414
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
Open
Open
Changes from all commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
fba34e6
Create RecursiveLinearSearch.js
debnath003 c865c35
Create RecursiveLinearSearch.test.js
debnath003 00c13af
Update RecursiveLinearSearch.js
debnath003 3185e53
Update RecursiveLinearSearch.js
debnath003 8e77a5e
Update RecursiveLinearSearch.js
debnath003 a6dd54f
Update RecursiveLinearSearch.js
debnath003 c1961f1
Update RecursiveLinearSearch.js
debnath003 a839184
Update RecursiveLinearSearch.test.js
debnath003 c1b4f0c
Delete Recursive/RecursiveLinearSearch.js
debnath003 dd8cb8f
Delete Recursive/test/RecursiveLinearSearch.test.js
debnath003 aaef3a8
Create Rectangle.js
debnath003 82e00cb
Update Rectangle.js
debnath003 2109aa6
Create Rectangle.test.js
debnath003 72765bd
Update Rectangle.js
debnath003 197cf3c
Delete Geometry/Rectangle.js
debnath003 3e36938
Delete Geometry/Test/Rectangle.test.js
debnath003 860dda7
Create CircularDoublyLinkedList.js
debnath003 9bdaf7d
Update CircularDoublyLinkedList.js
debnath003 834cd1b
Update CircularDoublyLinkedList.js
debnath003 12ec96e
Create CircularDoublyLinkedList.test.js
debnath003 b1f967a
Update CircularDoublyLinkedList.test.js
debnath003 808d740
Update CircularDoublyLinkedList.js
debnath003 393e591
Update CircularDoublyLinkedList.test.js
debnath003 46b572e
Update CircularDoublyLinkedList.js
debnath003 476aae1
Update CircularDoublyLinkedList.test.js
debnath003 4997d01
Update CircularDoublyLinkedList.test.js
debnath003 627c3ff
Update CircularDoublyLinkedList.js
debnath003 6eb5856
Update CircularDoublyLinkedList.test.js
debnath003 21c42c9
Clean up tests a bit
appgurueu eac5a0f
missed one "Check"
appgurueu 1424735
createIterator -> elements
appgurueu aba8600
Replace toArray with elements iterator
appgurueu ab99a65
Remove `toArray` test
appgurueu 42b8013
Remove usages of `toArray` in other tests
appgurueu 9355002
Update CircularDoublyLinkedList.js
debnath003 3a2de53
Update CircularDoublyLinkedList.test.js
debnath003 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
58 changes: 58 additions & 0 deletions
Data-Structures/Linked-List/test/CircularDoublyLinkedList.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.