Skip to content
Merged
Show file tree
Hide file tree
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
28 changes: 28 additions & 0 deletions Data-Structures/Linked-List/DoublyLinkedList.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,30 @@ function DoubleLinkedList () {
this.getTail = function () {
return tail
}

// Method to iterate over the LinkedList
this.iterator = function () {
let currentNode = this.getHead()
if (currentNode === null) return -1

const iterate = function * () {
while (currentNode) {
yield currentNode.element
currentNode = currentNode.next
}
}
return iterate()
}

// Method to log the LinkedList, for debugging
// it' a circular structure, so can't use stringify to debug the whole structure
this.log = function () {
let currentNode = this.getHead()
while (currentNode) {
console.log(currentNode.element)
currentNode = currentNode.next
}
}
}

// Example
Expand All @@ -202,5 +226,9 @@ function DoubleLinkedList () {
// newDoubleLinkedList.append(1)
// newDoubleLinkedList.append(2)
// newDoubleLinkedList.size() // returns 2
// const iterate = newDoubleLinkedList.iterator()
// console.log(iterate.next().value) // 1
// console.log(iterate.next().value) // 2
// console.log(newDoubleLinkedList.log())

export { DoubleLinkedList }
19 changes: 19 additions & 0 deletions Data-Structures/Linked-List/SinglyLinkedList.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,25 @@ class LinkedList {

return list
}

// Method to iterate over the LinkedList
iterator () {
let currentNode = this.headNode
if (currentNode === null) return -1

const iterate = function * () {
while (currentNode) {
yield currentNode.data
currentNode = currentNode.next
}
}
return iterate()
}

// Method to log the LinkedList
log () {
console.log(JSON.stringify(this.headNode, null, 2))
}
}

export { Node, LinkedList }
127 changes: 127 additions & 0 deletions Data-Structures/Linked-List/test/DoublyLinkedList.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import { DoubleLinkedList } from '../DoublyLinkedList'

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

list.append(1)
expect(list.getHead().element).toEqual(1)

list.append(2)
expect(list.getTail().element).toEqual(2)
})

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

list.insert(0, 1)
expect(list.getHead().element).toEqual(1)

list.insert(1, 20)
expect(list.getTail().element).toEqual(20)
})

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

list.insert(0, 10)
list.insert(1, 40)
list.insert(2, 30)

list.removeAt(0)
expect(list.getHead().element).toEqual(40)

list.removeAt(1)
expect(list.getTail().element).toEqual(40)
})

it('Check delete', () => {
const list = new DoubleLinkedList()

list.insert(0, 10)
list.insert(1, 40)

list.delete(10)
expect(list.getHead().element).toEqual(40)
})

it('Check deleteTail', () => {
const list = new DoubleLinkedList()

list.insert(0, 10)
list.insert(1, 40)

list.deleteTail()
expect(list.getTail().element).toEqual(10)
})

it('Check toString', () => {
const list = new DoubleLinkedList()

list.insert(0, 20)
expect(list.toString()).toEqual('20')
})

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

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

list.insert(0, 'Hello')
expect(list.isEmpty()).toEqual(false)
})

it('Check size', () => {
const list = new DoubleLinkedList()
expect(list.size()).toBe(0)

list.append(10)
expect(list.size()).toBe(1)

list.removeAt(1)
expect(list.size()).toBe(1)
})

it('Check toArray', () => {
const list = new DoubleLinkedList()
list.append(1)
list.append(2)

const listArray = list.toArray()
expect(listArray).toEqual([1, 2])
})

it('Check getHead', () => {
const list = new DoubleLinkedList()
expect(list.getHead()).toEqual(null)

list.append(1)
list.append(2)
expect(list.getHead()).toBeInstanceOf(Object)
})

it('Check Iterator', () => {
const list = new DoubleLinkedList()

let iterate = list.iterator()
expect(iterate).toBe(-1)

const arr = [10, 20, 5]
list.append(arr[0])
list.append(arr[1])
list.append(arr[2])
iterate = list.iterator()

for (let i = 0; i < arr.length; i++) {
expect(iterate.next().value).toBe(arr[i])
}
expect(iterate.next().value).toBe(undefined)

iterate = list.iterator()
let count = 0
for (const item of iterate) {
expect(item).toBe(arr[count])
count++
}
})
})
25 changes: 25 additions & 0 deletions Data-Structures/Linked-List/test/SinglyLinkedList.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,29 @@ describe('SinglyLinkedList', () => {
list.removeFirst()
expect(list.size()).toBe(1)
})

it('Check Iterator', () => {
const list = new LinkedList()

let iterate = list.iterator()
expect(iterate).toBe(-1)

const arr = [10, 20, 5]
list.addLast(arr[0])
list.addLast(arr[1])
list.addLast(arr[2])
iterate = list.iterator()

for (let i = 0; i < arr.length; i++) {
expect(iterate.next().value).toBe(arr[i])
}
expect(iterate.next().value).toBe(undefined)

iterate = list.iterator()
let count = 0
for (const item of iterate) {
expect(item).toBe(arr[count])
count++
}
})
})
Loading