@tsevimli/collections
Advanced tools
Comparing version
'use strict'; | ||
module.exports = { | ||
...require('./lib/linkedlist.js'), | ||
LinkedList: require('./lib/linkedlist.js'), | ||
FixedQueue: require('./lib/fixedQueue.js'), | ||
}; |
@@ -32,12 +32,13 @@ 'use strict'; | ||
const element = this.first; | ||
if (!element) return null; | ||
this.length--; | ||
if (this.last === element) { | ||
this.first = null; | ||
this.last = null; | ||
} else { | ||
this.first = element.next; | ||
this.first.prev = null; | ||
if (element) { | ||
this.length--; | ||
if (this.last === element) { | ||
this.first = null; | ||
this.last = null; | ||
} else { | ||
this.first = element.next; | ||
this.first.prev = null; | ||
} | ||
} | ||
return element.item; | ||
return element?.item; | ||
} | ||
@@ -65,15 +66,35 @@ | ||
toArray() { | ||
const array = new Array(this.length); | ||
let element = this.first; | ||
for (let i = 0; i < this.length; i++) { | ||
array[i] = element.item; | ||
element = element.next; | ||
} | ||
return array; | ||
} | ||
[Symbol.iterator]() { | ||
let element = this.first; | ||
return { | ||
next() { | ||
if (!element) return { done: true }; | ||
const result = { value: element.item, done: false }; | ||
element = element.next; | ||
return result; | ||
}, | ||
const next = () => { | ||
if (!element) return { done: true }; | ||
const result = { value: element.item, done: false }; | ||
element = element.next; | ||
return result; | ||
}; | ||
return { next }; | ||
} | ||
// sort(compareFn = (a, b) => a - b) { | ||
// if (!this.first) return this; | ||
// this.first = mergeSort(this.first, compareFn); | ||
// let current = this.first; | ||
// while (current.next) { | ||
// current = current.next; | ||
// } | ||
// this.last = current; | ||
// return this; | ||
// } | ||
} | ||
module.exports = { LinkedList }; | ||
module.exports = LinkedList; |
@@ -6,4 +6,57 @@ 'use strict'; | ||
// const split = (head) => { | ||
// let slow = head; | ||
// let fast = head.next; | ||
// | ||
// while (fast && fast.next) { | ||
// slow = slow.next; | ||
// fast = fast.next.next; | ||
// } | ||
// | ||
// const middle = slow.next; | ||
// slow.next = null; | ||
// if (middle) middle.prev = null; | ||
// return middle; | ||
// }; | ||
// | ||
// const merge = (left, right, compareFn) => { | ||
// const dummy = { next: null }; | ||
// let current = dummy; | ||
// | ||
// while (left && right) { | ||
// if (compareFn(left.item, right.item) <= 0) { | ||
// current.next = left; | ||
// left.prev = current; | ||
// left = left.next; | ||
// } else { | ||
// current.next = right; | ||
// right.prev = current; | ||
// right = right.next; | ||
// } | ||
// current = current.next; | ||
// } | ||
// | ||
// if (left) { | ||
// current.next = left; | ||
// left.prev = current; | ||
// } else if (right) { | ||
// current.next = right; | ||
// right.prev = current; | ||
// } | ||
// | ||
// return dummy.next; | ||
// }; | ||
// | ||
// const mergeSort = (node, compareFn) => { | ||
// if (!node || !node.next) return node; | ||
// | ||
// const middle = split(node); | ||
// const left = mergeSort(node, compareFn); | ||
// const right = mergeSort(middle, compareFn); | ||
// | ||
// return merge(left, right, compareFn); | ||
// }; | ||
module.exports = { | ||
isIterable, | ||
}; |
{ | ||
"name": "@tsevimli/collections", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Data structures in JavaScript", | ||
@@ -5,0 +5,0 @@ "main": "collections.js", |
@@ -6,3 +6,3 @@ 'use strict'; | ||
plan(10); | ||
plan(11); | ||
@@ -134,1 +134,11 @@ test('LinkedList initialization', (t) => { | ||
}); | ||
test('LinkedList to toArray', (t) => { | ||
t.plan(2); | ||
const list = new LinkedList([1, 2, null, 4, undefined, {}]); | ||
const array = list.toArray(); | ||
t.equal(array.length, 6); | ||
t.same(array, [1, 2, null, 4, undefined, {}]); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
180714
210.81%31
29.17%1465
73.17%1
Infinity%