data-footstone
Advanced tools
Comparing version 0.1.3 to 0.1.4
@@ -77,6 +77,7 @@ 'use strict'; | ||
if (p.length) { | ||
this.head = p.reduceRight((r, c) => { | ||
this.head = p.reduceRight((r, c, index) => { | ||
return { | ||
value: c, | ||
next: r, | ||
position: index, | ||
}; | ||
@@ -87,10 +88,11 @@ }, null); | ||
} | ||
createNode(p) { | ||
createNode(v, p) { | ||
return { | ||
value: p, | ||
value: v, | ||
next: null, | ||
position: p, | ||
}; | ||
} | ||
append(p) { | ||
let node = this.createNode(p); | ||
let node = this.createNode(p, this.length); | ||
if (!this.head) { | ||
@@ -111,3 +113,3 @@ this.head = node; | ||
let res; | ||
let node = this.createNode(v); | ||
let node = this.createNode(v, p); | ||
if (p === 0) { | ||
@@ -129,2 +131,3 @@ node.next = this.head; | ||
} | ||
this.setPosition(p); | ||
this.length++; | ||
@@ -157,2 +160,3 @@ return res; | ||
} | ||
this.setPosition(p); | ||
this.length--; | ||
@@ -165,32 +169,38 @@ return res; | ||
} | ||
removeElement(v, all = false) { | ||
let res = false; | ||
if (this.length) { | ||
if (this.head.value === v) { | ||
this.head = this.head.next; | ||
this.length--; | ||
res = true; | ||
} | ||
else { | ||
let cur = this.head.next; | ||
let pre = this.head; | ||
while (cur) { | ||
if (cur.value === v) { | ||
pre.next = cur.next; | ||
this.length--; | ||
res = true; | ||
if (!all) { | ||
break; | ||
} | ||
} | ||
pre = cur; | ||
cur = cur.next; | ||
} | ||
} | ||
} | ||
else { | ||
res = false; | ||
} | ||
return res; | ||
} | ||
// 删除此方法 | ||
// removeElement(v: T, all = false) { | ||
// let res: B = false | ||
// if (this.length) { | ||
// if (this.head.value === v) { | ||
// this.head = this.head.next | ||
// this.length-- | ||
// res = true | ||
// } else { | ||
// let cur = this.head.next | ||
// let pre = this.head | ||
// while (cur) { | ||
// if (cur.value === v) { | ||
// pre.next = cur.next | ||
// this.length-- | ||
// res = true | ||
// if (!all) { | ||
// break | ||
// } | ||
// } | ||
// pre = cur | ||
// cur = cur.next | ||
// } | ||
// } | ||
// let cur = this.head | ||
// let index = 0 | ||
// while (cur) { | ||
// cur.position = index | ||
// index++ | ||
// cur = cur.next | ||
// } | ||
// } else { | ||
// res = false | ||
// } | ||
// return res | ||
// } | ||
// includes() {} | ||
@@ -200,9 +210,11 @@ reverseSelf() { | ||
if (p) { | ||
return q; | ||
return fn(p.next, { value: p.value, next: q }); | ||
} | ||
else { | ||
return fn(p.next, { value: p.value, next: q }); | ||
return q; | ||
} | ||
}; | ||
this.head = fn(this.head); | ||
this.setPosition(); | ||
return this; | ||
} | ||
@@ -219,2 +231,13 @@ reverse() { | ||
} | ||
setPosition(from = 0) { | ||
let cur = this.head; | ||
let index = 0; | ||
while (cur) { | ||
if (index >= from) { | ||
cur.position = index; | ||
} | ||
index++; | ||
cur = cur.next; | ||
} | ||
} | ||
} | ||
@@ -224,10 +247,9 @@ class DoublyChain extends BaseChain { | ||
super(); | ||
// this.head = null | ||
this.tail = null; | ||
// this.length = 0 | ||
p.forEach(this.append); | ||
p.forEach(this.append, this); | ||
} | ||
createNode(p) { | ||
createNode(v, p) { | ||
return { | ||
value: p, | ||
value: v, | ||
position: p, | ||
prev: null, | ||
@@ -237,4 +259,4 @@ next: null, | ||
} | ||
append(p) { | ||
let node = this.createNode(p); | ||
append(v) { | ||
let node = this.createNode(v, this.length); | ||
if (this.length) { | ||
@@ -254,3 +276,3 @@ node.prev = this.tail; | ||
if (this.isValidRange(p)) { | ||
let node = this.createNode(v); | ||
let node = this.createNode(v, p); | ||
if (p === 0) { | ||
@@ -273,2 +295,3 @@ node.next = this.head; | ||
} | ||
this.setPosition(p); | ||
this.length++; | ||
@@ -313,2 +336,3 @@ res = true; | ||
} | ||
this.setPosition(p); | ||
this.length--; | ||
@@ -321,41 +345,49 @@ return res; | ||
} | ||
removeElement(v, all = false) { | ||
let res = false; | ||
// removeElement(v: T, all = false) { | ||
// let res: B = false | ||
// let cur = this.head | ||
// let index = 0 | ||
// while (index < this.length) { | ||
// if (cur.value === v) { | ||
// if (index === 0) { | ||
// // op this.head | ||
// this.head = this.head.next | ||
// this.head.prev = null | ||
// } else if (index === this.length) { | ||
// // op this.tail | ||
// this.tail = this.tail.prev | ||
// this.tail.next = null | ||
// } else { | ||
// // op middle | ||
// cur.prev.next = cur.next | ||
// cur.next.prev = cur.prev | ||
// } | ||
// res = true | ||
// this.length-- | ||
// if (!all) { | ||
// break | ||
// } | ||
// } else { | ||
// index++ | ||
// } | ||
// cur = cur.next | ||
// } | ||
// return res | ||
// } | ||
clear() { | ||
this.head = null; | ||
this.tail = null; | ||
this.length = 0; | ||
} | ||
setPosition(from = 0) { | ||
let cur = this.head; | ||
let index = 0; | ||
while (index < this.length) { | ||
if (cur.value === v) { | ||
if (index === 0) { | ||
// op this.head | ||
this.head = this.head.next; | ||
this.head.prev = null; | ||
} | ||
else if (index === this.length) { | ||
// op this.tail | ||
this.tail = this.tail.prev; | ||
this.tail.next = null; | ||
} | ||
else { | ||
// op middle | ||
cur.prev.next = cur.next; | ||
cur.next.prev = cur.prev; | ||
} | ||
res = true; | ||
this.length--; | ||
if (!all) { | ||
break; | ||
} | ||
while (cur) { | ||
if (index >= from) { | ||
cur.position = index; | ||
} | ||
else { | ||
index++; | ||
} | ||
index++; | ||
cur = cur.next; | ||
} | ||
return res; | ||
} | ||
clear() { | ||
this.head = null; | ||
this.tail = null; | ||
this.length = 0; | ||
} | ||
} | ||
@@ -366,22 +398,34 @@ class SingleCircleChain extends SingleChain { | ||
this.head = null; | ||
this.tail = null; | ||
this.length = 0; | ||
p.forEach(this.append, this); | ||
} | ||
// head() {} | ||
// length() {} | ||
createNode(p) { | ||
toArray() { | ||
let res = []; | ||
let index = 0; | ||
let cur = this.head; | ||
while (index < this.length) { | ||
res.push(cur.value); | ||
cur = cur.next; | ||
index++; | ||
} | ||
return res; | ||
} | ||
createNode(v, p) { | ||
return { | ||
value: p, | ||
value: v, | ||
position: p, | ||
next: null, | ||
prev: null, | ||
}; | ||
} | ||
append(v) { | ||
let node = this.createNode(v); | ||
let node = this.createNode(v, this.length); | ||
if (this.length) { | ||
this.head.prev = node; | ||
this.tail.next = node; | ||
node.next = this.head; | ||
this.tail = node; | ||
} | ||
else { | ||
this.head = node; | ||
node.prev = node; | ||
this.tail = node; | ||
node.next = node; | ||
@@ -394,6 +438,6 @@ } | ||
if (this.isValidRange(p)) { | ||
let node = this.createNode(v); | ||
let node = this.createNode(v, p); | ||
if (p === 0) { | ||
this.tail.next = node; | ||
node.next = this.head; | ||
this.head.prev = node; | ||
this.head = node; | ||
@@ -411,7 +455,6 @@ } | ||
pre.next = node; | ||
node.prev = pre; | ||
node.next = cur; | ||
cur.prev = node; | ||
} | ||
this.length++; | ||
this.setPosition(p); | ||
return true; | ||
@@ -429,4 +472,4 @@ } | ||
res = this.head.value; | ||
this.head.prev.next = this.head.next; | ||
this.head.next.prev = this.head.prev; | ||
this.head = this.head.next; | ||
this.tail.next = this.head; | ||
} | ||
@@ -443,5 +486,5 @@ else { | ||
pre.next = cur.next; | ||
cur.next.prev = pre; | ||
} | ||
this.length--; | ||
this.setPosition(p); | ||
return res; | ||
@@ -453,32 +496,12 @@ } | ||
} | ||
// 返回 boolean 表示是否删除 | ||
removeElement(v, all = false) { | ||
let res = false; | ||
if (this.length) { | ||
if ((this.head.value = v)) { | ||
this.head.next.prev = this.head.prev; | ||
this.head.prev.next = this.head.next; | ||
this.length--; | ||
res = true; | ||
setPosition(from = 0) { | ||
let index = 0; | ||
let cur = this.head; | ||
while (index < this.length) { | ||
if (index >= from) { | ||
cur.position = index; | ||
} | ||
else { | ||
let cur = this.head.next; | ||
while (cur) { | ||
if (cur.value === v) { | ||
cur.prev.next = cur.next; | ||
cur.next.prev = cur.prev; | ||
this.length--; | ||
res = true; | ||
if (!all) { | ||
break; | ||
} | ||
} | ||
cur = cur.next; | ||
} | ||
} | ||
index++; | ||
cur = cur.next; | ||
} | ||
else { | ||
res = false; | ||
} | ||
return res; | ||
} | ||
@@ -489,6 +512,14 @@ } | ||
super(); | ||
p.forEach(this.append, this); | ||
} | ||
// createNode() | ||
createNode(v, p) { | ||
return { | ||
value: v, | ||
position: p, | ||
next: null, | ||
prev: null, | ||
}; | ||
} | ||
append(p) { | ||
let node = this.createNode(p); | ||
let node = this.createNode(p, this.length); | ||
if (this.length) { | ||
@@ -512,3 +543,3 @@ node.next = this.head; | ||
if (this.isValidRange(p)) { | ||
let node = this.createNode(v); | ||
let node = this.createNode(v, p); | ||
if (p === 0) { | ||
@@ -533,2 +564,3 @@ node.prev = node; | ||
this.length++; | ||
this.setPosition(p); | ||
res = true; | ||
@@ -572,2 +604,3 @@ } | ||
} | ||
this.setPosition(p); | ||
this.length--; | ||
@@ -580,34 +613,53 @@ return res; | ||
} | ||
removeElement(v, all = false) { | ||
let res = false; | ||
// removeElement(v: T, all = false) { | ||
// let res: B = false | ||
// let cur = this.head | ||
// let index = 0 | ||
// while (index < this.length) { | ||
// if (cur.value === v) { | ||
// if (index === 0) { | ||
// this.head = this.head.next | ||
// this.head.prev = this.tail | ||
// this.tail.next = this.head | ||
// } else if (index === this.length) { | ||
// this.tail = this.tail.prev | ||
// this.tail.next = this.head | ||
// this.head.prev = this.tail | ||
// } else { | ||
// cur.prev.next = cur.next | ||
// cur.next.prev = cur.prev | ||
// } | ||
// res = true | ||
// this.length-- | ||
// if (!all) { | ||
// break | ||
// } | ||
// } else { | ||
// index++ | ||
// } | ||
// cur = cur.next | ||
// } | ||
// return res | ||
// } | ||
toArray() { | ||
let res = []; | ||
let index = 0; | ||
let cur = this.head; | ||
while (index < this.length) { | ||
res.push(cur.value); | ||
index++; | ||
cur = cur.next; | ||
} | ||
return res; | ||
} | ||
setPosition(from = 0) { | ||
let index = 0; | ||
let cur = this.head; | ||
while (index < this.length) { | ||
if (cur.value === v) { | ||
if (index === 0) { | ||
this.head = this.head.next; | ||
this.head.prev = this.tail; | ||
this.tail.next = this.head; | ||
} | ||
else if (index === this.length) { | ||
this.tail = this.tail.prev; | ||
this.tail.next = this.head; | ||
this.head.prev = this.tail; | ||
} | ||
else { | ||
cur.prev.next = cur.next; | ||
cur.next.prev = cur.prev; | ||
} | ||
res = true; | ||
this.length--; | ||
if (!all) { | ||
break; | ||
} | ||
if (index >= from) { | ||
cur.position = index; | ||
} | ||
else { | ||
index++; | ||
} | ||
index++; | ||
cur = cur.next; | ||
} | ||
return res; | ||
} | ||
@@ -614,0 +666,0 @@ } |
@@ -5,2 +5,18 @@ 'use strict'; | ||
let djb2HashFn = (k) => { | ||
let h = 5381; | ||
let t = String(k); | ||
for (let i = 0; i < t.length; i++) { | ||
h = h * 33 + t.charCodeAt(i); | ||
} | ||
return h % 1013; | ||
}; | ||
let loseloseHashFn = (k) => { | ||
let h = 0; | ||
let t = String(k); | ||
for (let i = 0; i < t.length; i++) { | ||
h = h + t.charCodeAt(i); | ||
} | ||
return h % 37; | ||
}; | ||
class HashMap { | ||
@@ -105,20 +121,6 @@ constructor(kind = 'separate', hash = 'djb2') { | ||
default: | ||
this._hashFn = (k) => { | ||
let h = 5381; | ||
let t = String(k); | ||
for (let i = 0; i < t.length; i++) { | ||
h = h * 33 + t.charCodeAt(i); | ||
} | ||
return h % 1013; | ||
}; | ||
this._hashFn = djb2HashFn; | ||
break; | ||
case 'loselose': | ||
this._hashFn = (k) => { | ||
let h = 0; | ||
let t = String(k); | ||
for (let i = 0; i < t.length; i++) { | ||
h = h + t.charCodeAt(i); | ||
} | ||
return h % 37; | ||
}; | ||
this._hashFn = loseloseHashFn; | ||
break; | ||
@@ -151,2 +153,4 @@ } | ||
exports.HashMap = HashMap; | ||
exports.djb2HashFn = djb2HashFn; | ||
exports.loseloseHashFn = loseloseHashFn; | ||
//# sourceMappingURL=hashMap.js.map |
'use strict'; | ||
var order = require('./order.js'); | ||
var stack = require('./stack.js'); | ||
@@ -11,6 +10,6 @@ var queue = require('./queue.js'); | ||
var tree = require('./tree.js'); | ||
var order = require('./order.js'); | ||
exports.order = order; | ||
exports.Stack = stack.Stack; | ||
@@ -26,2 +25,4 @@ exports.PriorityQueue = queue.PriorityQueue; | ||
exports.HashMap = hashMap.HashMap; | ||
exports.djb2HashFn = hashMap.djb2HashFn; | ||
exports.loseloseHashFn = hashMap.loseloseHashFn; | ||
exports.AVLTree = tree.AVLTree; | ||
@@ -31,2 +32,3 @@ exports.BaseTree = tree.BaseTree; | ||
exports.RedBackTree = tree.RedBackTree; | ||
exports.order = order; | ||
//# sourceMappingURL=index.js.map |
'use strict'; | ||
// 冒泡 | ||
// n^2 | ||
@@ -23,2 +24,3 @@ let bubbleSort = (arr, order = 'asc') => { | ||
}; | ||
// 选择 | ||
let selectSort = (arr, order = 'asc') => { | ||
@@ -46,2 +48,3 @@ for (let i = 0; i < arr.length - 1; i++) { | ||
}; | ||
// 插入 | ||
// n^2 | ||
@@ -71,2 +74,3 @@ let insertSort = (arr, order = 'asc') => { | ||
}; | ||
// 快速 | ||
// nlogn | ||
@@ -102,8 +106,4 @@ let quickSort = (arr, order = 'asc') => { | ||
}; | ||
let heapSort = () => { }; | ||
let binarySearch = () => { }; | ||
exports.binarySearch = binarySearch; | ||
exports.bubbleSort = bubbleSort; | ||
exports.heapSort = heapSort; | ||
exports.insertSort = insertSort; | ||
@@ -110,0 +110,0 @@ exports.quickSort = quickSort; |
@@ -9,2 +9,3 @@ 'use strict'; | ||
this.items.push(...p); | ||
return this.size(); | ||
} | ||
@@ -34,3 +35,4 @@ dequeue() { | ||
reverse() { | ||
return this.items.reverse(); | ||
this.items.reverse(); | ||
return this; | ||
} | ||
@@ -99,2 +101,3 @@ } | ||
} | ||
return this.size(); | ||
} | ||
@@ -101,0 +104,0 @@ dequeue() { |
@@ -75,6 +75,7 @@ class BaseChain { | ||
if (p.length) { | ||
this.head = p.reduceRight((r, c) => { | ||
this.head = p.reduceRight((r, c, index) => { | ||
return { | ||
value: c, | ||
next: r, | ||
position: index, | ||
}; | ||
@@ -85,10 +86,11 @@ }, null); | ||
} | ||
createNode(p) { | ||
createNode(v, p) { | ||
return { | ||
value: p, | ||
value: v, | ||
next: null, | ||
position: p, | ||
}; | ||
} | ||
append(p) { | ||
let node = this.createNode(p); | ||
let node = this.createNode(p, this.length); | ||
if (!this.head) { | ||
@@ -109,3 +111,3 @@ this.head = node; | ||
let res; | ||
let node = this.createNode(v); | ||
let node = this.createNode(v, p); | ||
if (p === 0) { | ||
@@ -127,2 +129,3 @@ node.next = this.head; | ||
} | ||
this.setPosition(p); | ||
this.length++; | ||
@@ -155,2 +158,3 @@ return res; | ||
} | ||
this.setPosition(p); | ||
this.length--; | ||
@@ -163,32 +167,38 @@ return res; | ||
} | ||
removeElement(v, all = false) { | ||
let res = false; | ||
if (this.length) { | ||
if (this.head.value === v) { | ||
this.head = this.head.next; | ||
this.length--; | ||
res = true; | ||
} | ||
else { | ||
let cur = this.head.next; | ||
let pre = this.head; | ||
while (cur) { | ||
if (cur.value === v) { | ||
pre.next = cur.next; | ||
this.length--; | ||
res = true; | ||
if (!all) { | ||
break; | ||
} | ||
} | ||
pre = cur; | ||
cur = cur.next; | ||
} | ||
} | ||
} | ||
else { | ||
res = false; | ||
} | ||
return res; | ||
} | ||
// 删除此方法 | ||
// removeElement(v: T, all = false) { | ||
// let res: B = false | ||
// if (this.length) { | ||
// if (this.head.value === v) { | ||
// this.head = this.head.next | ||
// this.length-- | ||
// res = true | ||
// } else { | ||
// let cur = this.head.next | ||
// let pre = this.head | ||
// while (cur) { | ||
// if (cur.value === v) { | ||
// pre.next = cur.next | ||
// this.length-- | ||
// res = true | ||
// if (!all) { | ||
// break | ||
// } | ||
// } | ||
// pre = cur | ||
// cur = cur.next | ||
// } | ||
// } | ||
// let cur = this.head | ||
// let index = 0 | ||
// while (cur) { | ||
// cur.position = index | ||
// index++ | ||
// cur = cur.next | ||
// } | ||
// } else { | ||
// res = false | ||
// } | ||
// return res | ||
// } | ||
// includes() {} | ||
@@ -198,9 +208,11 @@ reverseSelf() { | ||
if (p) { | ||
return q; | ||
return fn(p.next, { value: p.value, next: q }); | ||
} | ||
else { | ||
return fn(p.next, { value: p.value, next: q }); | ||
return q; | ||
} | ||
}; | ||
this.head = fn(this.head); | ||
this.setPosition(); | ||
return this; | ||
} | ||
@@ -217,2 +229,13 @@ reverse() { | ||
} | ||
setPosition(from = 0) { | ||
let cur = this.head; | ||
let index = 0; | ||
while (cur) { | ||
if (index >= from) { | ||
cur.position = index; | ||
} | ||
index++; | ||
cur = cur.next; | ||
} | ||
} | ||
} | ||
@@ -222,10 +245,9 @@ class DoublyChain extends BaseChain { | ||
super(); | ||
// this.head = null | ||
this.tail = null; | ||
// this.length = 0 | ||
p.forEach(this.append); | ||
p.forEach(this.append, this); | ||
} | ||
createNode(p) { | ||
createNode(v, p) { | ||
return { | ||
value: p, | ||
value: v, | ||
position: p, | ||
prev: null, | ||
@@ -235,4 +257,4 @@ next: null, | ||
} | ||
append(p) { | ||
let node = this.createNode(p); | ||
append(v) { | ||
let node = this.createNode(v, this.length); | ||
if (this.length) { | ||
@@ -252,3 +274,3 @@ node.prev = this.tail; | ||
if (this.isValidRange(p)) { | ||
let node = this.createNode(v); | ||
let node = this.createNode(v, p); | ||
if (p === 0) { | ||
@@ -271,2 +293,3 @@ node.next = this.head; | ||
} | ||
this.setPosition(p); | ||
this.length++; | ||
@@ -311,2 +334,3 @@ res = true; | ||
} | ||
this.setPosition(p); | ||
this.length--; | ||
@@ -319,41 +343,49 @@ return res; | ||
} | ||
removeElement(v, all = false) { | ||
let res = false; | ||
// removeElement(v: T, all = false) { | ||
// let res: B = false | ||
// let cur = this.head | ||
// let index = 0 | ||
// while (index < this.length) { | ||
// if (cur.value === v) { | ||
// if (index === 0) { | ||
// // op this.head | ||
// this.head = this.head.next | ||
// this.head.prev = null | ||
// } else if (index === this.length) { | ||
// // op this.tail | ||
// this.tail = this.tail.prev | ||
// this.tail.next = null | ||
// } else { | ||
// // op middle | ||
// cur.prev.next = cur.next | ||
// cur.next.prev = cur.prev | ||
// } | ||
// res = true | ||
// this.length-- | ||
// if (!all) { | ||
// break | ||
// } | ||
// } else { | ||
// index++ | ||
// } | ||
// cur = cur.next | ||
// } | ||
// return res | ||
// } | ||
clear() { | ||
this.head = null; | ||
this.tail = null; | ||
this.length = 0; | ||
} | ||
setPosition(from = 0) { | ||
let cur = this.head; | ||
let index = 0; | ||
while (index < this.length) { | ||
if (cur.value === v) { | ||
if (index === 0) { | ||
// op this.head | ||
this.head = this.head.next; | ||
this.head.prev = null; | ||
} | ||
else if (index === this.length) { | ||
// op this.tail | ||
this.tail = this.tail.prev; | ||
this.tail.next = null; | ||
} | ||
else { | ||
// op middle | ||
cur.prev.next = cur.next; | ||
cur.next.prev = cur.prev; | ||
} | ||
res = true; | ||
this.length--; | ||
if (!all) { | ||
break; | ||
} | ||
while (cur) { | ||
if (index >= from) { | ||
cur.position = index; | ||
} | ||
else { | ||
index++; | ||
} | ||
index++; | ||
cur = cur.next; | ||
} | ||
return res; | ||
} | ||
clear() { | ||
this.head = null; | ||
this.tail = null; | ||
this.length = 0; | ||
} | ||
} | ||
@@ -364,22 +396,34 @@ class SingleCircleChain extends SingleChain { | ||
this.head = null; | ||
this.tail = null; | ||
this.length = 0; | ||
p.forEach(this.append, this); | ||
} | ||
// head() {} | ||
// length() {} | ||
createNode(p) { | ||
toArray() { | ||
let res = []; | ||
let index = 0; | ||
let cur = this.head; | ||
while (index < this.length) { | ||
res.push(cur.value); | ||
cur = cur.next; | ||
index++; | ||
} | ||
return res; | ||
} | ||
createNode(v, p) { | ||
return { | ||
value: p, | ||
value: v, | ||
position: p, | ||
next: null, | ||
prev: null, | ||
}; | ||
} | ||
append(v) { | ||
let node = this.createNode(v); | ||
let node = this.createNode(v, this.length); | ||
if (this.length) { | ||
this.head.prev = node; | ||
this.tail.next = node; | ||
node.next = this.head; | ||
this.tail = node; | ||
} | ||
else { | ||
this.head = node; | ||
node.prev = node; | ||
this.tail = node; | ||
node.next = node; | ||
@@ -392,6 +436,6 @@ } | ||
if (this.isValidRange(p)) { | ||
let node = this.createNode(v); | ||
let node = this.createNode(v, p); | ||
if (p === 0) { | ||
this.tail.next = node; | ||
node.next = this.head; | ||
this.head.prev = node; | ||
this.head = node; | ||
@@ -409,7 +453,6 @@ } | ||
pre.next = node; | ||
node.prev = pre; | ||
node.next = cur; | ||
cur.prev = node; | ||
} | ||
this.length++; | ||
this.setPosition(p); | ||
return true; | ||
@@ -427,4 +470,4 @@ } | ||
res = this.head.value; | ||
this.head.prev.next = this.head.next; | ||
this.head.next.prev = this.head.prev; | ||
this.head = this.head.next; | ||
this.tail.next = this.head; | ||
} | ||
@@ -441,5 +484,5 @@ else { | ||
pre.next = cur.next; | ||
cur.next.prev = pre; | ||
} | ||
this.length--; | ||
this.setPosition(p); | ||
return res; | ||
@@ -451,32 +494,12 @@ } | ||
} | ||
// 返回 boolean 表示是否删除 | ||
removeElement(v, all = false) { | ||
let res = false; | ||
if (this.length) { | ||
if ((this.head.value = v)) { | ||
this.head.next.prev = this.head.prev; | ||
this.head.prev.next = this.head.next; | ||
this.length--; | ||
res = true; | ||
setPosition(from = 0) { | ||
let index = 0; | ||
let cur = this.head; | ||
while (index < this.length) { | ||
if (index >= from) { | ||
cur.position = index; | ||
} | ||
else { | ||
let cur = this.head.next; | ||
while (cur) { | ||
if (cur.value === v) { | ||
cur.prev.next = cur.next; | ||
cur.next.prev = cur.prev; | ||
this.length--; | ||
res = true; | ||
if (!all) { | ||
break; | ||
} | ||
} | ||
cur = cur.next; | ||
} | ||
} | ||
index++; | ||
cur = cur.next; | ||
} | ||
else { | ||
res = false; | ||
} | ||
return res; | ||
} | ||
@@ -487,6 +510,14 @@ } | ||
super(); | ||
p.forEach(this.append, this); | ||
} | ||
// createNode() | ||
createNode(v, p) { | ||
return { | ||
value: v, | ||
position: p, | ||
next: null, | ||
prev: null, | ||
}; | ||
} | ||
append(p) { | ||
let node = this.createNode(p); | ||
let node = this.createNode(p, this.length); | ||
if (this.length) { | ||
@@ -510,3 +541,3 @@ node.next = this.head; | ||
if (this.isValidRange(p)) { | ||
let node = this.createNode(v); | ||
let node = this.createNode(v, p); | ||
if (p === 0) { | ||
@@ -531,2 +562,3 @@ node.prev = node; | ||
this.length++; | ||
this.setPosition(p); | ||
res = true; | ||
@@ -570,2 +602,3 @@ } | ||
} | ||
this.setPosition(p); | ||
this.length--; | ||
@@ -578,34 +611,53 @@ return res; | ||
} | ||
removeElement(v, all = false) { | ||
let res = false; | ||
// removeElement(v: T, all = false) { | ||
// let res: B = false | ||
// let cur = this.head | ||
// let index = 0 | ||
// while (index < this.length) { | ||
// if (cur.value === v) { | ||
// if (index === 0) { | ||
// this.head = this.head.next | ||
// this.head.prev = this.tail | ||
// this.tail.next = this.head | ||
// } else if (index === this.length) { | ||
// this.tail = this.tail.prev | ||
// this.tail.next = this.head | ||
// this.head.prev = this.tail | ||
// } else { | ||
// cur.prev.next = cur.next | ||
// cur.next.prev = cur.prev | ||
// } | ||
// res = true | ||
// this.length-- | ||
// if (!all) { | ||
// break | ||
// } | ||
// } else { | ||
// index++ | ||
// } | ||
// cur = cur.next | ||
// } | ||
// return res | ||
// } | ||
toArray() { | ||
let res = []; | ||
let index = 0; | ||
let cur = this.head; | ||
while (index < this.length) { | ||
res.push(cur.value); | ||
index++; | ||
cur = cur.next; | ||
} | ||
return res; | ||
} | ||
setPosition(from = 0) { | ||
let index = 0; | ||
let cur = this.head; | ||
while (index < this.length) { | ||
if (cur.value === v) { | ||
if (index === 0) { | ||
this.head = this.head.next; | ||
this.head.prev = this.tail; | ||
this.tail.next = this.head; | ||
} | ||
else if (index === this.length) { | ||
this.tail = this.tail.prev; | ||
this.tail.next = this.head; | ||
this.head.prev = this.tail; | ||
} | ||
else { | ||
cur.prev.next = cur.next; | ||
cur.next.prev = cur.prev; | ||
} | ||
res = true; | ||
this.length--; | ||
if (!all) { | ||
break; | ||
} | ||
if (index >= from) { | ||
cur.position = index; | ||
} | ||
else { | ||
index++; | ||
} | ||
index++; | ||
cur = cur.next; | ||
} | ||
return res; | ||
} | ||
@@ -612,0 +664,0 @@ } |
import { SingleChain } from './chain.js'; | ||
let djb2HashFn = (k) => { | ||
let h = 5381; | ||
let t = String(k); | ||
for (let i = 0; i < t.length; i++) { | ||
h = h * 33 + t.charCodeAt(i); | ||
} | ||
return h % 1013; | ||
}; | ||
let loseloseHashFn = (k) => { | ||
let h = 0; | ||
let t = String(k); | ||
for (let i = 0; i < t.length; i++) { | ||
h = h + t.charCodeAt(i); | ||
} | ||
return h % 37; | ||
}; | ||
class HashMap { | ||
@@ -102,20 +118,6 @@ constructor(kind = 'separate', hash = 'djb2') { | ||
default: | ||
this._hashFn = (k) => { | ||
let h = 5381; | ||
let t = String(k); | ||
for (let i = 0; i < t.length; i++) { | ||
h = h * 33 + t.charCodeAt(i); | ||
} | ||
return h % 1013; | ||
}; | ||
this._hashFn = djb2HashFn; | ||
break; | ||
case 'loselose': | ||
this._hashFn = (k) => { | ||
let h = 0; | ||
let t = String(k); | ||
for (let i = 0; i < t.length; i++) { | ||
h = h + t.charCodeAt(i); | ||
} | ||
return h % 37; | ||
}; | ||
this._hashFn = loseloseHashFn; | ||
break; | ||
@@ -147,3 +149,3 @@ } | ||
export { HashMap }; | ||
export { HashMap, djb2HashFn, loseloseHashFn }; | ||
//# sourceMappingURL=hashMap.js.map |
@@ -1,3 +0,1 @@ | ||
import * as order from './order.js'; | ||
export { order }; | ||
export { Stack } from './stack.js'; | ||
@@ -8,4 +6,6 @@ export { PriorityQueue, Queue } from './queue.js'; | ||
export { PMap } from './pMap.js'; | ||
export { HashMap } from './hashMap.js'; | ||
export { HashMap, djb2HashFn, loseloseHashFn } from './hashMap.js'; | ||
export { AVLTree, BaseTree, BinarySearchTree, RedBackTree } from './tree.js'; | ||
import * as order from './order.js'; | ||
export { order }; | ||
//# sourceMappingURL=index.js.map |
@@ -0,1 +1,2 @@ | ||
// 冒泡 | ||
// n^2 | ||
@@ -21,2 +22,3 @@ let bubbleSort = (arr, order = 'asc') => { | ||
}; | ||
// 选择 | ||
let selectSort = (arr, order = 'asc') => { | ||
@@ -44,2 +46,3 @@ for (let i = 0; i < arr.length - 1; i++) { | ||
}; | ||
// 插入 | ||
// n^2 | ||
@@ -69,2 +72,3 @@ let insertSort = (arr, order = 'asc') => { | ||
}; | ||
// 快速 | ||
// nlogn | ||
@@ -100,6 +104,4 @@ let quickSort = (arr, order = 'asc') => { | ||
}; | ||
let heapSort = () => { }; | ||
let binarySearch = () => { }; | ||
export { binarySearch, bubbleSort, heapSort, insertSort, quickSort, selectSort }; | ||
export { bubbleSort, insertSort, quickSort, selectSort }; | ||
//# sourceMappingURL=order.js.map |
@@ -7,2 +7,3 @@ class Queue { | ||
this.items.push(...p); | ||
return this.size(); | ||
} | ||
@@ -32,3 +33,4 @@ dequeue() { | ||
reverse() { | ||
return this.items.reverse(); | ||
this.items.reverse(); | ||
return this; | ||
} | ||
@@ -97,2 +99,3 @@ } | ||
} | ||
return this.size(); | ||
} | ||
@@ -99,0 +102,0 @@ dequeue() { |
{ | ||
"name": "data-footstone", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "data structure", | ||
@@ -75,3 +75,3 @@ "author": "feigebaobei <18515195415@163.com>", | ||
}, | ||
"gitHead": "4fac9843e0455fc3ac6a8683d20b0f2de94dfd53" | ||
"gitHead": "7172a7b54aee94be2bde392cf11b2c3defeb730f" | ||
} |
@@ -75,6 +75,7 @@ class BaseChain { | ||
if (p.length) { | ||
this.head = p.reduceRight((r, c) => { | ||
this.head = p.reduceRight((r, c, index) => { | ||
return { | ||
value: c, | ||
next: r, | ||
position: index, | ||
}; | ||
@@ -85,10 +86,11 @@ }, null); | ||
} | ||
createNode(p) { | ||
createNode(v, p) { | ||
return { | ||
value: p, | ||
value: v, | ||
next: null, | ||
position: p, | ||
}; | ||
} | ||
append(p) { | ||
let node = this.createNode(p); | ||
let node = this.createNode(p, this.length); | ||
if (!this.head) { | ||
@@ -109,3 +111,3 @@ this.head = node; | ||
let res; | ||
let node = this.createNode(v); | ||
let node = this.createNode(v, p); | ||
if (p === 0) { | ||
@@ -127,2 +129,3 @@ node.next = this.head; | ||
} | ||
this.setPosition(p); | ||
this.length++; | ||
@@ -155,2 +158,3 @@ return res; | ||
} | ||
this.setPosition(p); | ||
this.length--; | ||
@@ -163,32 +167,38 @@ return res; | ||
} | ||
removeElement(v, all = false) { | ||
let res = false; | ||
if (this.length) { | ||
if (this.head.value === v) { | ||
this.head = this.head.next; | ||
this.length--; | ||
res = true; | ||
} | ||
else { | ||
let cur = this.head.next; | ||
let pre = this.head; | ||
while (cur) { | ||
if (cur.value === v) { | ||
pre.next = cur.next; | ||
this.length--; | ||
res = true; | ||
if (!all) { | ||
break; | ||
} | ||
} | ||
pre = cur; | ||
cur = cur.next; | ||
} | ||
} | ||
} | ||
else { | ||
res = false; | ||
} | ||
return res; | ||
} | ||
// 删除此方法 | ||
// removeElement(v: T, all = false) { | ||
// let res: B = false | ||
// if (this.length) { | ||
// if (this.head.value === v) { | ||
// this.head = this.head.next | ||
// this.length-- | ||
// res = true | ||
// } else { | ||
// let cur = this.head.next | ||
// let pre = this.head | ||
// while (cur) { | ||
// if (cur.value === v) { | ||
// pre.next = cur.next | ||
// this.length-- | ||
// res = true | ||
// if (!all) { | ||
// break | ||
// } | ||
// } | ||
// pre = cur | ||
// cur = cur.next | ||
// } | ||
// } | ||
// let cur = this.head | ||
// let index = 0 | ||
// while (cur) { | ||
// cur.position = index | ||
// index++ | ||
// cur = cur.next | ||
// } | ||
// } else { | ||
// res = false | ||
// } | ||
// return res | ||
// } | ||
// includes() {} | ||
@@ -198,9 +208,11 @@ reverseSelf() { | ||
if (p) { | ||
return q; | ||
return fn(p.next, { value: p.value, next: q }); | ||
} | ||
else { | ||
return fn(p.next, { value: p.value, next: q }); | ||
return q; | ||
} | ||
}; | ||
this.head = fn(this.head); | ||
this.setPosition(); | ||
return this; | ||
} | ||
@@ -217,2 +229,13 @@ reverse() { | ||
} | ||
setPosition(from = 0) { | ||
let cur = this.head; | ||
let index = 0; | ||
while (cur) { | ||
if (index >= from) { | ||
cur.position = index; | ||
} | ||
index++; | ||
cur = cur.next; | ||
} | ||
} | ||
} | ||
@@ -222,10 +245,9 @@ class DoublyChain extends BaseChain { | ||
super(); | ||
// this.head = null | ||
this.tail = null; | ||
// this.length = 0 | ||
p.forEach(this.append); | ||
p.forEach(this.append, this); | ||
} | ||
createNode(p) { | ||
createNode(v, p) { | ||
return { | ||
value: p, | ||
value: v, | ||
position: p, | ||
prev: null, | ||
@@ -235,4 +257,4 @@ next: null, | ||
} | ||
append(p) { | ||
let node = this.createNode(p); | ||
append(v) { | ||
let node = this.createNode(v, this.length); | ||
if (this.length) { | ||
@@ -252,3 +274,3 @@ node.prev = this.tail; | ||
if (this.isValidRange(p)) { | ||
let node = this.createNode(v); | ||
let node = this.createNode(v, p); | ||
if (p === 0) { | ||
@@ -271,2 +293,3 @@ node.next = this.head; | ||
} | ||
this.setPosition(p); | ||
this.length++; | ||
@@ -311,2 +334,3 @@ res = true; | ||
} | ||
this.setPosition(p); | ||
this.length--; | ||
@@ -319,41 +343,49 @@ return res; | ||
} | ||
removeElement(v, all = false) { | ||
let res = false; | ||
// removeElement(v: T, all = false) { | ||
// let res: B = false | ||
// let cur = this.head | ||
// let index = 0 | ||
// while (index < this.length) { | ||
// if (cur.value === v) { | ||
// if (index === 0) { | ||
// // op this.head | ||
// this.head = this.head.next | ||
// this.head.prev = null | ||
// } else if (index === this.length) { | ||
// // op this.tail | ||
// this.tail = this.tail.prev | ||
// this.tail.next = null | ||
// } else { | ||
// // op middle | ||
// cur.prev.next = cur.next | ||
// cur.next.prev = cur.prev | ||
// } | ||
// res = true | ||
// this.length-- | ||
// if (!all) { | ||
// break | ||
// } | ||
// } else { | ||
// index++ | ||
// } | ||
// cur = cur.next | ||
// } | ||
// return res | ||
// } | ||
clear() { | ||
this.head = null; | ||
this.tail = null; | ||
this.length = 0; | ||
} | ||
setPosition(from = 0) { | ||
let cur = this.head; | ||
let index = 0; | ||
while (index < this.length) { | ||
if (cur.value === v) { | ||
if (index === 0) { | ||
// op this.head | ||
this.head = this.head.next; | ||
this.head.prev = null; | ||
} | ||
else if (index === this.length) { | ||
// op this.tail | ||
this.tail = this.tail.prev; | ||
this.tail.next = null; | ||
} | ||
else { | ||
// op middle | ||
cur.prev.next = cur.next; | ||
cur.next.prev = cur.prev; | ||
} | ||
res = true; | ||
this.length--; | ||
if (!all) { | ||
break; | ||
} | ||
while (cur) { | ||
if (index >= from) { | ||
cur.position = index; | ||
} | ||
else { | ||
index++; | ||
} | ||
index++; | ||
cur = cur.next; | ||
} | ||
return res; | ||
} | ||
clear() { | ||
this.head = null; | ||
this.tail = null; | ||
this.length = 0; | ||
} | ||
} | ||
@@ -364,22 +396,34 @@ class SingleCircleChain extends SingleChain { | ||
this.head = null; | ||
this.tail = null; | ||
this.length = 0; | ||
p.forEach(this.append, this); | ||
} | ||
// head() {} | ||
// length() {} | ||
createNode(p) { | ||
toArray() { | ||
let res = []; | ||
let index = 0; | ||
let cur = this.head; | ||
while (index < this.length) { | ||
res.push(cur.value); | ||
cur = cur.next; | ||
index++; | ||
} | ||
return res; | ||
} | ||
createNode(v, p) { | ||
return { | ||
value: p, | ||
value: v, | ||
position: p, | ||
next: null, | ||
prev: null, | ||
}; | ||
} | ||
append(v) { | ||
let node = this.createNode(v); | ||
let node = this.createNode(v, this.length); | ||
if (this.length) { | ||
this.head.prev = node; | ||
this.tail.next = node; | ||
node.next = this.head; | ||
this.tail = node; | ||
} | ||
else { | ||
this.head = node; | ||
node.prev = node; | ||
this.tail = node; | ||
node.next = node; | ||
@@ -392,6 +436,6 @@ } | ||
if (this.isValidRange(p)) { | ||
let node = this.createNode(v); | ||
let node = this.createNode(v, p); | ||
if (p === 0) { | ||
this.tail.next = node; | ||
node.next = this.head; | ||
this.head.prev = node; | ||
this.head = node; | ||
@@ -409,7 +453,6 @@ } | ||
pre.next = node; | ||
node.prev = pre; | ||
node.next = cur; | ||
cur.prev = node; | ||
} | ||
this.length++; | ||
this.setPosition(p); | ||
return true; | ||
@@ -427,4 +470,4 @@ } | ||
res = this.head.value; | ||
this.head.prev.next = this.head.next; | ||
this.head.next.prev = this.head.prev; | ||
this.head = this.head.next; | ||
this.tail.next = this.head; | ||
} | ||
@@ -441,5 +484,5 @@ else { | ||
pre.next = cur.next; | ||
cur.next.prev = pre; | ||
} | ||
this.length--; | ||
this.setPosition(p); | ||
return res; | ||
@@ -451,32 +494,12 @@ } | ||
} | ||
// 返回 boolean 表示是否删除 | ||
removeElement(v, all = false) { | ||
let res = false; | ||
if (this.length) { | ||
if ((this.head.value = v)) { | ||
this.head.next.prev = this.head.prev; | ||
this.head.prev.next = this.head.next; | ||
this.length--; | ||
res = true; | ||
setPosition(from = 0) { | ||
let index = 0; | ||
let cur = this.head; | ||
while (index < this.length) { | ||
if (index >= from) { | ||
cur.position = index; | ||
} | ||
else { | ||
let cur = this.head.next; | ||
while (cur) { | ||
if (cur.value === v) { | ||
cur.prev.next = cur.next; | ||
cur.next.prev = cur.prev; | ||
this.length--; | ||
res = true; | ||
if (!all) { | ||
break; | ||
} | ||
} | ||
cur = cur.next; | ||
} | ||
} | ||
index++; | ||
cur = cur.next; | ||
} | ||
else { | ||
res = false; | ||
} | ||
return res; | ||
} | ||
@@ -487,6 +510,14 @@ } | ||
super(); | ||
p.forEach(this.append, this); | ||
} | ||
// createNode() | ||
createNode(v, p) { | ||
return { | ||
value: v, | ||
position: p, | ||
next: null, | ||
prev: null, | ||
}; | ||
} | ||
append(p) { | ||
let node = this.createNode(p); | ||
let node = this.createNode(p, this.length); | ||
if (this.length) { | ||
@@ -510,3 +541,3 @@ node.next = this.head; | ||
if (this.isValidRange(p)) { | ||
let node = this.createNode(v); | ||
let node = this.createNode(v, p); | ||
if (p === 0) { | ||
@@ -531,2 +562,3 @@ node.prev = node; | ||
this.length++; | ||
this.setPosition(p); | ||
res = true; | ||
@@ -570,2 +602,3 @@ } | ||
} | ||
this.setPosition(p); | ||
this.length--; | ||
@@ -578,36 +611,55 @@ return res; | ||
} | ||
removeElement(v, all = false) { | ||
let res = false; | ||
// removeElement(v: T, all = false) { | ||
// let res: B = false | ||
// let cur = this.head | ||
// let index = 0 | ||
// while (index < this.length) { | ||
// if (cur.value === v) { | ||
// if (index === 0) { | ||
// this.head = this.head.next | ||
// this.head.prev = this.tail | ||
// this.tail.next = this.head | ||
// } else if (index === this.length) { | ||
// this.tail = this.tail.prev | ||
// this.tail.next = this.head | ||
// this.head.prev = this.tail | ||
// } else { | ||
// cur.prev.next = cur.next | ||
// cur.next.prev = cur.prev | ||
// } | ||
// res = true | ||
// this.length-- | ||
// if (!all) { | ||
// break | ||
// } | ||
// } else { | ||
// index++ | ||
// } | ||
// cur = cur.next | ||
// } | ||
// return res | ||
// } | ||
toArray() { | ||
let res = []; | ||
let index = 0; | ||
let cur = this.head; | ||
while (index < this.length) { | ||
res.push(cur.value); | ||
index++; | ||
cur = cur.next; | ||
} | ||
return res; | ||
} | ||
setPosition(from = 0) { | ||
let index = 0; | ||
let cur = this.head; | ||
while (index < this.length) { | ||
if (cur.value === v) { | ||
if (index === 0) { | ||
this.head = this.head.next; | ||
this.head.prev = this.tail; | ||
this.tail.next = this.head; | ||
} | ||
else if (index === this.length) { | ||
this.tail = this.tail.prev; | ||
this.tail.next = this.head; | ||
this.head.prev = this.tail; | ||
} | ||
else { | ||
cur.prev.next = cur.next; | ||
cur.next.prev = cur.prev; | ||
} | ||
res = true; | ||
this.length--; | ||
if (!all) { | ||
break; | ||
} | ||
if (index >= from) { | ||
cur.position = index; | ||
} | ||
else { | ||
index++; | ||
} | ||
index++; | ||
cur = cur.next; | ||
} | ||
return res; | ||
} | ||
} | ||
export { SingleChain, DoublyChain, SingleCircleChain, DoublyCircleChain }; |
import { SingleChain } from './chain'; | ||
let djb2HashFn = (k) => { | ||
let h = 5381; | ||
let t = String(k); | ||
for (let i = 0; i < t.length; i++) { | ||
h = h * 33 + t.charCodeAt(i); | ||
} | ||
return h % 1013; | ||
}; | ||
let loseloseHashFn = (k) => { | ||
let h = 0; | ||
let t = String(k); | ||
for (let i = 0; i < t.length; i++) { | ||
h = h + t.charCodeAt(i); | ||
} | ||
return h % 37; | ||
}; | ||
class HashMap { | ||
@@ -101,20 +117,6 @@ constructor(kind = 'separate', hash = 'djb2') { | ||
default: | ||
this._hashFn = (k) => { | ||
let h = 5381; | ||
let t = String(k); | ||
for (let i = 0; i < t.length; i++) { | ||
h = h * 33 + t.charCodeAt(i); | ||
} | ||
return h % 1013; | ||
}; | ||
this._hashFn = djb2HashFn; | ||
break; | ||
case 'loselose': | ||
this._hashFn = (k) => { | ||
let h = 0; | ||
let t = String(k); | ||
for (let i = 0; i < t.length; i++) { | ||
h = h + t.charCodeAt(i); | ||
} | ||
return h % 37; | ||
}; | ||
this._hashFn = loseloseHashFn; | ||
break; | ||
@@ -148,2 +150,2 @@ } | ||
// 在扩展类中使用基类的方法 | ||
export { HashMap }; | ||
export { HashMap, djb2HashFn, loseloseHashFn }; |
@@ -1,2 +0,1 @@ | ||
import * as order from './order'; | ||
import { Stack } from './stack'; | ||
@@ -7,4 +6,5 @@ import { Queue, PriorityQueue } from './queue'; | ||
import { PMap } from './pMap'; | ||
import { HashMap } from './hashMap'; | ||
import { HashMap, djb2HashFn, loseloseHashFn } from './hashMap'; | ||
import { BaseTree, BinarySearchTree, AVLTree, RedBackTree } from './tree'; | ||
export { order, Stack, Queue, PriorityQueue, SingleChain, DoublyChain, SingleCircleChain, DoublyCircleChain, PSet, PMap, HashMap, BaseTree, BinarySearchTree, AVLTree, RedBackTree, }; | ||
import * as order from './order'; | ||
export { order, Stack, Queue, PriorityQueue, SingleChain, DoublyChain, SingleCircleChain, DoublyCircleChain, PSet, PMap, HashMap, djb2HashFn, loseloseHashFn, BaseTree, BinarySearchTree, AVLTree, RedBackTree, }; |
@@ -0,1 +1,2 @@ | ||
// 冒泡 | ||
// n^2 | ||
@@ -23,2 +24,3 @@ let bubbleSort = (arr, order = 'asc') => { | ||
}; | ||
// 选择 | ||
let selectSort = (arr, order = 'asc') => { | ||
@@ -47,2 +49,3 @@ for (let i = 0; i < arr.length - 1; i++) { | ||
}; | ||
// 归并 | ||
let merge = (leftArr, rightArr, order) => { | ||
@@ -83,2 +86,3 @@ let tempArr = []; | ||
}; | ||
// 插入 | ||
// n^2 | ||
@@ -108,2 +112,3 @@ let insertSort = (arr, order = 'asc') => { | ||
}; | ||
// 快速 | ||
// nlogn | ||
@@ -139,4 +144,29 @@ let quickSort = (arr, order = 'asc') => { | ||
}; | ||
let heapSort = () => { }; | ||
let binarySearch = () => { }; | ||
export { bubbleSort, selectSort, insertSort, quickSort, heapSort, binarySearch }; | ||
// 堆 | ||
// let heapSort = () => { | ||
// } | ||
// 计数(分布式排序) | ||
// 桶(分布式排序) | ||
// 基数(分布式排序) | ||
// let binarySearch = (arr: A[], item: A) => { | ||
// let res = -1 | ||
// let left = 0 | ||
// let right = arr.length | ||
// let mid: A | ||
// while (left <= right) { | ||
// mid = (left + right) >> 1 | ||
// if (arr[mid] === item) { | ||
// res = mid | ||
// break | ||
// } else if (arr[mid] < item) { | ||
// left = mid + 1 | ||
// } else if (arr[mid] > item) { | ||
// right = mid - 1 | ||
// } | ||
// } | ||
// return res | ||
// } | ||
export { bubbleSort, selectSort, insertSort, quickSort, | ||
// heapSort, | ||
// binarySearch | ||
}; |
@@ -7,2 +7,3 @@ class Queue { | ||
this.items.push(...p); | ||
return this.size(); | ||
} | ||
@@ -32,3 +33,4 @@ dequeue() { | ||
reverse() { | ||
return this.items.reverse(); | ||
this.items.reverse(); | ||
return this; | ||
} | ||
@@ -97,2 +99,3 @@ } | ||
} | ||
return this.size(); | ||
} | ||
@@ -99,0 +102,0 @@ dequeue() { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
283024
64
4640