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 23 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
138 changes: 138 additions & 0 deletions Data-Structures/Linked-List/CircularDoublyLinkedList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// Explanation:- https://www.javatpoint.com/circular-doubly-linked-list

class Node {
constructor (element) {
this.element = element
this.next = null
this.prev = null
}
}

class CircularDoublyLinkedList {
constructor () {
this.length = 0
this.head = null
this.tail = null
}

// Add new element
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++
}

// Insert element at a specific position
insert (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) {
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
}
}

// Remove element at a specific position
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
}
}

// Print the list
print () {
const elements = []
let currentNode = this.head
if (!currentNode) return
do {
elements.push(currentNode.element)
currentNode = currentNode.next
} while (currentNode !== this.head)
console.log(elements.join(' <-> '))
}

// Convert the list to a string
toString () {
const elements = []
let currentNode = this.head
if (!currentNode) return ''
do {
elements.push(currentNode.element)
currentNode = currentNode.next
} while (currentNode !== this.head)
return elements.join(' <-> ')
}

// Check if the list is empty
isEmpty () {
return this.length === 0
}
}

export { CircularDoublyLinkedList }
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { CircularDoublyLinkedList } from '../CircularDoublyLinkedList' // Adjust the import path as needed

describe('CircularDoublyLinkedList', () => {
it('Check append', () => {
const list = new CircularDoublyLinkedList()

list.append(1)
expect(list.toString()).toEqual('1')

list.append(2)
expect(list.toString()).toEqual('1 <-> 2')
})

it('Check insert', () => {
const list = new CircularDoublyLinkedList()

list.insert(0, 1)
expect(list.toString()).toEqual('1')

list.insert(1, 20)
expect(list.toString()).toEqual('1 <-> 20')
})

it('Check removeAt', () => {
const list = new CircularDoublyLinkedList()

list.append(10)
list.append(40)
list.append(30)

list.removeAt(0)
expect(list.toString()).toEqual('40 <-> 30')

list.removeAt(1)
expect(list.toString()).toEqual('40')
})

it('Check print', () => {
const list = new CircularDoublyLinkedList()

list.append(20)
expect(() => {
list.print()
}).not.toThrow()
})

it('Check isEmpty', () => {
const list = new CircularDoublyLinkedList()

expect(list.isEmpty()).toEqual(true)

list.append('Hello')
expect(list.isEmpty()).toEqual(false)
})
})