merge: Iterator and log methods added for linked lists (#891)

* iterator, log methods added in SL

* iterator, log methods added in DL

* test file added for DoublyLL

* format issue fix
This commit is contained in:
Keramot UL Islam
2022-02-18 20:43:45 +06:00
committed by GitHub
parent 68ca0ceeef
commit 29a3ab73bc
5 changed files with 206 additions and 10984 deletions

View File

@ -194,6 +194,30 @@ function DoubleLinkedList () {
this.getTail = function () { this.getTail = function () {
return tail 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 // Example
@ -202,5 +226,9 @@ function DoubleLinkedList () {
// newDoubleLinkedList.append(1) // newDoubleLinkedList.append(1)
// newDoubleLinkedList.append(2) // newDoubleLinkedList.append(2)
// newDoubleLinkedList.size() // returns 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 } export { DoubleLinkedList }

View File

@ -211,6 +211,25 @@ class LinkedList {
return list 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 } export { Node, LinkedList }

View File

@ -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++
}
})
})

View File

@ -163,4 +163,29 @@ describe('SinglyLinkedList', () => {
list.removeFirst() list.removeFirst()
expect(list.size()).toBe(1) 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++
}
})
}) })

10991
package-lock.json generated

File diff suppressed because it is too large Load Diff