【从蛋壳到满天飞】JS 数据结构解析和算法实现,所有文章大概的内容以下: Arrays(数组)、Stacks(栈)、Queues(队列)、LinkedList(链表)、Recursion(递归思想)、BinarySearchTree(二分搜索树)、Set(集合)、Map(映射)、Heap(堆)、PriorityQueue(优先队列)、SegmentTree(线段树)、Trie(字典树)、UnionFind(并查集)、AVLTree(AVL 平衡树)、RedBlackTree(红黑平衡树)、HashTable(哈希表)html
源代码有三个:ES6(单个单个的 class 类型的 js 文件) | JS + HTML(一个 js 配合一个 html)| JAVA (一个一个的工程)前端
所有源代码已上传 github,点击我吧,光看文章可以掌握两成,动手敲代码、动脑思考、画图才能够掌握八成。git
本文章适合 对数据结构想了解而且感兴趣的人群,文章风格一如既往如此,就以为手机上看起来比较方便,这样显得比较有条理,整理这些笔记加源码,时间跨度也算将近半年时间了,但愿对想学习数据结构的人或者正在学习数据结构的人群有帮助。github
无处不在的 Undo 操做(撤销)面试
程序调用的系统栈算法
function A () {
1 ...;
2 B();
3 ...;
}
function B () {
1 ...;
2 C();
3 ...;
}
function C () {
1 ...;
2 ...;
3 ...;
}
复制代码
系统栈记录的过程是:编程
A2
会被放入系统栈中,系统栈中显示:[A2]
,[A2, B2]
,[A2]
,[]
,2 和 3 中解释的原理 就是系统栈最神奇的地方数组
栈虽然是一个很是简单的数据结构性能优化
MyStack
void push(e)
:入栈E pop()
:出栈E peek()
:查看位于栈顶位置的元素int getSize()
:获取栈中实际元素的个数boolean isEmpty()
:栈是否为空MyStack
void push(e)
:O(1) 均摊E pop()
:O(1) 均摊E peek()
:O(1)int getSize()
:O(1)boolean isEmpty()
:O(1)(class: MyArray, class: MyStack, class: Main)
网络
MyArray
class MyArray {
// 构造函数,传入数组的容量capacity构造Array 默认数组的容量capacity=10
constructor(capacity = 10) {
this.data = new Array(capacity);
this.size = 0;
}
// 获取数组中的元素实际个数
getSize() {
return this.size;
}
// 获取数组的容量
getCapacity() {
return this.data.length;
}
// 判断数组是否为空
isEmpty() {
return this.size === 0;
}
// 给数组扩容
resize(capacity) {
let newArray = new Array(capacity);
for (var i = 0; i < this.size; i++) {
newArray[i] = this.data[i];
}
// let index = this.size - 1;
// while (index > -1) {
// newArray[index] = this.data[index];
// index --;
// }
this.data = newArray;
}
// 在指定索引处插入元素
insert(index, element) {
// 先判断数组是否已满
if (this.size == this.getCapacity()) {
// throw new Error("add error. Array is full.");
this.resize(this.size * 2);
}
// 而后判断索引是否符合要求
if (index < 0 || index > this.size) {
throw new Error(
'insert error. require index < 0 or index > size.'
);
}
// 最后 将指定索引处腾出来
// 从指定索引处开始,全部数组元素所有日后移动一位
// 从后往前移动
for (let i = this.size - 1; i >= index; i--) {
this.data[i + 1] = this.data[i];
}
// 在指定索引处插入元素
this.data[index] = element;
// 维护一下size
this.size++;
}
// 扩展 在数组最前面插入一个元素
unshift(element) {
this.insert(0, element);
}
// 扩展 在数组最后面插入一个元素
push(element) {
this.insert(this.size, element);
}
// 其实在数组中添加元素 就至关于在数组最后面插入一个元素
add(element) {
if (this.size == this.getCapacity()) {
// throw new Error("add error. Array is full.");
this.resize(this.size * 2);
}
// size其实指向的是 当前数组最后一个元素的 后一个位置的索引。
this.data[this.size] = element;
// 维护size
this.size++;
}
// get
get(index) {
// 不能访问没有存放元素的位置
if (index < 0 || index >= this.size) {
throw new Error('get error. index < 0 or index >= size.');
}
return this.data[index];
}
// 扩展: 获取数组中第一个元素
getFirst() {
return this.get(0);
}
// 扩展: 获取数组中最后一个元素
getLast() {
return this.get(this.size - 1);
}
// set
set(index, newElement) {
// 不能修改没有存放元素的位置
if (index < 0 || index >= this.size) {
throw new Error('set error. index < 0 or index >= size.');
}
this.data[index] = newElement;
}
// contain
contain(element) {
for (var i = 0; i < this.size; i++) {
if (this.data[i] === element) {
return true;
}
}
return false;
}
// find
find(element) {
for (var i = 0; i < this.size; i++) {
if (this.data[i] === element) {
return i;
}
}
return -1;
}
// findAll
findAll(element) {
// 建立一个自定义数组来存取这些 元素的索引
let myarray = new MyArray(this.size);
for (var i = 0; i < this.size; i++) {
if (this.data[i] === element) {
myarray.push(i);
}
}
// 返回这个自定义数组
return myarray;
}
// 删除指定索引处的元素
remove(index) {
// 索引合法性验证
if (index < 0 || index >= this.size) {
throw new Error('remove error. index < 0 or index >= size.');
}
// 暂存即将要被删除的元素
let element = this.data[index];
// 后面的元素覆盖前面的元素
for (let i = index; i < this.size - 1; i++) {
this.data[i] = this.data[i + 1];
}
this.size--;
this.data[this.size] = null;
// 若是size 为容量的四分之一时 就能够缩容了
// 防止复杂度震荡
if (Math.floor(this.getCapacity() / 4) === this.size) {
// 缩容一半
this.resize(Math.floor(this.getCapacity() / 2));
}
return element;
}
// 扩展:删除数组中第一个元素
shift() {
return this.remove(0);
}
// 扩展: 删除数组中最后一个元素
pop() {
return this.remove(this.size - 1);
}
// 扩展: 根据元素来进行删除
removeElement(element) {
let index = this.find(element);
if (index !== -1) {
this.remove(index);
}
}
// 扩展: 根据元素来删除全部元素
removeAllElement(element) {
let index = this.find(element);
while (index != -1) {
this.remove(index);
index = this.find(element);
}
// let indexArray = this.findAll(element);
// let cur, index = 0;
// for (var i = 0; i < indexArray.getSize(); i++) {
// // 每删除一个元素 原数组中就少一个元素,
// // 索引数组中的索引值是按照大小顺序排列的,
// // 因此 这个cur记录的是 原数组元素索引的偏移量
// // 只有这样才可以正确的删除元素。
// index = indexArray.get(i) - cur++;
// this.remove(index);
// }
}
// @Override toString 2018-10-17-jwl
toString() {
let arrInfo = `Array: size = ${this.getSize()},capacity = ${this.getCapacity()},\n`;
arrInfo += `data = [`;
for (var i = 0; i < this.size - 1; i++) {
arrInfo += `${this.data[i]}, `;
}
if (!this.isEmpty()) {
arrInfo += `${this.data[this.size - 1]}`;
}
arrInfo += `]`;
// 在页面上展现
document.body.innerHTML += `${arrInfo}<br /><br /> `;
return arrInfo;
}
}
复制代码
MyStack
class MyStack {
constructor(capacity = 10) {
this.myArray = new MyArray(capacity);
}
// 入栈
push(element) {
this.myArray.push(element);
}
// 出栈
pop() {
return this.myArray.pop();
}
// 查看栈顶的元素
peek() {
return this.myArray.getLast();
}
// 栈中实际元素的个数
getSize() {
return this.myArray.getSize();
}
// 栈是否为空
isEmpty() {
return this.myArray.isEmpty();
}
// 查看栈的容量
getCapacity() {
return this.myArray.getCapacity();
}
// @Override toString 2018-10-20-jwl
toString() {
let arrInfo = `Stack: size = ${this.getSize()},capacity = ${this.getCapacity()},\n`;
arrInfo += `data = [`;
for (var i = 0; i < this.myArray.size - 1; i++) {
arrInfo += `${this.myArray.data[i]}, `;
}
if (!this.isEmpty()) {
arrInfo += `${this.myArray.data[this.myArray.size - 1]}`;
}
arrInfo += `] stack top is right!`;
// 在页面上展现
document.body.innerHTML += `${arrInfo}<br /><br /> `;
return arrInfo;
}
}
复制代码
Main
class Main {
constructor() {
this.alterLine('MyStack Area');
let ms = new MyStack(10);
for (let i = 1; i <= 10; i++) {
ms.push(i);
console.log(ms.toString());
}
console.log(ms.peek());
this.show(ms.peek());
while (!ms.isEmpty()) {
console.log(ms.toString());
ms.pop();
}
}
// 将内容显示在页面上
show(content) {
document.body.innerHTML += `${content}<br /><br />`;
}
// 展现分割线
alterLine(title) {
let line = `--------------------${title}----------------------`;
console.log(line);
document.body.innerHTML += `${line}<br /><br />`;
}
}
window.onload = function() {
// 执行主函数
new Main();
};
复制代码
括号匹配-编译器
编译器是如何检查括号匹配的问题?
能够经过解答 Leetcode 中的一个问题,
leetcode.com
,leetcode-cn.com
leetcode.com
与leetcode-cn.com
的区别
leetcode-cn.com
支持中文,leetcode-cn.com
的题目数量没有英文版的多。leetcode-cn.com
的探索栏目的内容没有英文版的多。leetcode-cn.com
中的题目没有社区讨论功能,但英文版的有。leetcode 中第二十号题目:有效的括号
{ [ ( ) ] }
,class Solution {
isValid(s) {
// leetcode 20. 有效的括号
/** * @param {string} s * @return {boolean} */
var isValid = function(s) {
let stack = [];
// 以遍历的方式进行匹配操做
for (let i = 0; i < s.length; i++) {
// 是不是正括号
switch (s[i]) {
case '{':
case '[':
case '(':
stack.push(s[i]);
break;
default:
break;
}
// 是不是反括号
switch (s[i]) {
case '}':
if (stack.length === 0 || stack.pop() !== '{') {
console.log('valid error. not parentheses. in');
return false;
}
break;
case ']':
if (stack.length === 0 || stack.pop() !== '[') {
console.log('valid error. not parentheses. in');
return false;
}
break;
case ')':
if (stack.length === 0 || stack.pop() !== '(') {
console.log('valid error. not parentheses. in');
return false;
}
break;
default:
break;
}
}
// 是否所有匹配成功
if (stack.length === 0) {
return true;
} else {
console.log('valid error. not parentheses. out');
return false;
}
};
return isValid(s);
}
}
复制代码
class Main {
constructor() {
// this.alterLine("MyStack Area");
// let ms = new MyStack(10);
// for (let i = 1; i <= 10 ; i++) {
// ms.push(i);
// console.log(ms.toString());
// }
// console.log(ms.peek());
// this.show(ms.peek());
// while (!ms.isEmpty()) {
// console.log(ms.toString());
// ms.pop();
// }
this.alterLine('leetcode 20. 有效的括号');
let s = new Solution();
this.show(s.isValid('{ [ ( ) ] }'));
this.show(s.isValid(' [ ( ] ) '));
}
// 将内容显示在页面上
show(content) {
document.body.innerHTML += `${content}<br /><br />`;
}
// 展现分割线
alterLine(title) {
let line = `--------------------${title}----------------------`;
console.log(line);
document.body.innerHTML += `${line}<br /><br />`;
}
}
window.onload = function() {
// 执行主函数
new Main();
};
复制代码
leetcode 是一个很是好的准备面试的一个平台
若是你想使用你本身写的类,
Queue
void enqueue(E)
:入队E dequeue()
:出队E getFront()
:查看队首的元素int getSize()
:获取队列中的实际元素大小boolean isEmpty()
:获取队列是否为空的 bool 值class: MyArray, class: MyQueue, class: Main)
MyArray
class MyArray {
// 构造函数,传入数组的容量capacity构造Array 默认数组的容量capacity=10
constructor(capacity = 10) {
this.data = new Array(capacity);
this.size = 0;
}
// 获取数组中的元素实际个数
getSize() {
return this.size;
}
// 获取数组的容量
getCapacity() {
return this.data.length;
}
// 判断数组是否为空
isEmpty() {
return this.size === 0;
}
// 给数组扩容
resize(capacity) {
let newArray = new Array(capacity);
for (var i = 0; i < this.size; i++) {
newArray[i] = this.data[i];
}
// let index = this.size - 1;
// while (index > -1) {
// newArray[index] = this.data[index];
// index --;
// }
this.data = newArray;
}
// 在指定索引处插入元素
insert(index, element) {
// 先判断数组是否已满
if (this.size == this.getCapacity()) {
// throw new Error("add error. Array is full.");
this.resize(this.size * 2);
}
// 而后判断索引是否符合要求
if (index < 0 || index > this.size) {
throw new Error(
'insert error. require index < 0 or index > size.'
);
}
// 最后 将指定索引处腾出来
// 从指定索引处开始,全部数组元素所有日后移动一位
// 从后往前移动
for (let i = this.size - 1; i >= index; i--) {
this.data[i + 1] = this.data[i];
}
// 在指定索引处插入元素
this.data[index] = element;
// 维护一下size
this.size++;
}
// 扩展 在数组最前面插入一个元素
unshift(element) {
this.insert(0, element);
}
// 扩展 在数组最后面插入一个元素
push(element) {
this.insert(this.size, element);
}
// 其实在数组中添加元素 就至关于在数组最后面插入一个元素
add(element) {
if (this.size == this.getCapacity()) {
// throw new Error("add error. Array is full.");
this.resize(this.size * 2);
}
// size其实指向的是 当前数组最后一个元素的 后一个位置的索引。
this.data[this.size] = element;
// 维护size
this.size++;
}
// get
get(index) {
// 不能访问没有存放元素的位置
if (index < 0 || index >= this.size) {
throw new Error('get error. index < 0 or index >= size.');
}
return this.data[index];
}
// 扩展: 获取数组中第一个元素
getFirst() {
return this.get(0);
}
// 扩展: 获取数组中最后一个元素
getLast() {
return this.get(this.size - 1);
}
// set
set(index, newElement) {
// 不能修改没有存放元素的位置
if (index < 0 || index >= this.size) {
throw new Error('set error. index < 0 or index >= size.');
}
this.data[index] = newElement;
}
// contain
contain(element) {
for (var i = 0; i < this.size; i++) {
if (this.data[i] === element) {
return true;
}
}
return false;
}
// find
find(element) {
for (var i = 0; i < this.size; i++) {
if (this.data[i] === element) {
return i;
}
}
return -1;
}
// findAll
findAll(element) {
// 建立一个自定义数组来存取这些 元素的索引
let myarray = new MyArray(this.size);
for (var i = 0; i < this.size; i++) {
if (this.data[i] === element) {
myarray.push(i);
}
}
// 返回这个自定义数组
return myarray;
}
// 删除指定索引处的元素
remove(index) {
// 索引合法性验证
if (index < 0 || index >= this.size) {
throw new Error('remove error. index < 0 or index >= size.');
}
// 暂存即将要被删除的元素
let element = this.data[index];
// 后面的元素覆盖前面的元素
for (let i = index; i < this.size - 1; i++) {
this.data[i] = this.data[i + 1];
}
this.size--;
this.data[this.size] = null;
// 若是size 为容量的四分之一时 就能够缩容了
// 防止复杂度震荡
if (Math.floor(this.getCapacity() / 4) === this.size) {
// 缩容一半
this.resize(Math.floor(this.getCapacity() / 2));
}
return element;
}
// 扩展:删除数组中第一个元素
shift() {
return this.remove(0);
}
// 扩展: 删除数组中最后一个元素
pop() {
return this.remove(this.size - 1);
}
// 扩展: 根据元素来进行删除
removeElement(element) {
let index = this.find(element);
if (index !== -1) {
this.remove(index);
}
}
// 扩展: 根据元素来删除全部元素
removeAllElement(element) {
let index = this.find(element);
while (index != -1) {
this.remove(index);
index = this.find(element);
}
// let indexArray = this.findAll(element);
// let cur, index = 0;
// for (var i = 0; i < indexArray.getSize(); i++) {
// // 每删除一个元素 原数组中就少一个元素,
// // 索引数组中的索引值是按照大小顺序排列的,
// // 因此 这个cur记录的是 原数组元素索引的偏移量
// // 只有这样才可以正确的删除元素。
// index = indexArray.get(i) - cur++;
// this.remove(index);
// }
}
// @Override toString 2018-10-17-jwl
toString() {
let arrInfo = `Array: size = ${this.getSize()},capacity = ${this.getCapacity()},\n`;
arrInfo += `data = [`;
for (var i = 0; i < this.size - 1; i++) {
arrInfo += `${this.data[i]}, `;
}
if (!this.isEmpty()) {
arrInfo += `${this.data[this.size - 1]}`;
}
arrInfo += `]`;
// 在页面上展现
document.body.innerHTML += `${arrInfo}<br /><br /> `;
return arrInfo;
}
}
复制代码
MyQueue
class MyQueue {
constructor(capacity = 10) {
this.myArray = new MyArray(capacity);
}
// 入队
enqueue(element) {
this.myArray.push(element);
}
// 出队
dequeue() {
return this.myArray.shift();
}
// 查看队首的元素
getFront() {
return this.myArray.getFirst();
}
// 查看队列中实际元素的个数
getSize() {
return this.myArray.getSize();
}
// 查看 队列当前的容量
getCapacity() {
return this.myArray.getCapacity();
}
// 查看队列是否为空
isEmpty() {
return this.myArray.isEmpty();
}
// 输出队列中的信息
// @Override toString 2018-10-20-jwl
toString() {
let arrInfo = `Queue: size = ${this.getSize()},capacity = ${this.getCapacity()},\n`;
arrInfo += `data = front [`;
for (var i = 0; i < this.myArray.size - 1; i++) {
arrInfo += `${this.myArray.data[i]}, `;
}
if (!this.isEmpty()) {
arrInfo += `${this.myArray.data[this.myArray.size - 1]}`;
}
arrInfo += `] tail`;
// 在页面上展现
document.body.innerHTML += `${arrInfo}<br /><br /> `;
return arrInfo;
}
}
复制代码
Main
class Main {
constructor() {
this.alterLine('MyQueue Area');
let mq = new MyQueue(10);
for (let i = 1; i <= 10; i++) {
mq.enqueue(i);
console.log(mq.toString());
}
console.log(mq.getFront());
this.show(mq.getFront());
while (!mq.isEmpty()) {
console.log(mq.toString());
mq.dequeue();
}
}
// 将内容显示在页面上
show(content) {
document.body.innerHTML += `${content}<br /><br />`;
}
// 展现分割线
alterLine(title) {
let line = `--------------------${title}----------------------`;
console.log(line);
document.body.innerHTML += `${line}<br /><br />`;
}
}
复制代码
MyQueue
void enqueue(E)
: O(1)
均摊E dequeue()
:O(n)
出队的性能消耗太大了E getFront()
:O(1)
int getSize()
:O(1)
boolean isEmpty()
:O(1)
O(1)
,O(1)
。O(n)
,O(1)
。O(1)
了,front == tail
时队列就为空,tail+1==front
,(tail+1)%c==front
时就能够扩容了,tail+1==front
变成(tail+1)%c==front
是由于(tail+1)%c==front
,因此队列满了,O(1)
了。(tail + 1) % data.length == front
(class: MyLoopQueue, class: Main)
MyLoopQueue
class MyLoopQueue {
constructor(capacity = 10) {
// 初始化新数组
this.data = new Array(capacity);
// 初始化 队首、队尾的值 (索引)
this.front = this.tail = 0;
// 队列中实际元素个数
this.size = 0;
}
// 扩容
resize(capacity) {
let newArray = new Array(capacity);
let index = 0;
for (let i = 0; i < this.size; i++) {
// 索引可能会越界,因而就要取余一下,
// 若是越界了,就从队首开始
index = (this.front + i) % this.getCapacity();
newArray[i] = this.data[index];
}
this.data = newArray;
this.front = 0;
this.tail = this.size;
}
// 入队
enqueue(element) {
// 判断队列中是否已满
if ((this.tail + 1) % this.getCapacity() === this.front) {
this.resize(Math.floor(this.getCapacity() * 2));
}
this.data[this.tail] = element;
this.tail = (this.tail + 1) % this.getCapacity();
this.size++;
}
// 出队
dequeue() {
// 判断队列是否为空
if (this.isEmpty()) {
throw new Error("can't dequeue from an empty queue.");
}
let element = this.data[this.front];
this.data[this.front] = null;
this.front = (this.front + 1) % this.getCapacity();
this.size--;
// 当size 为容量的四分之一时就缩容一倍
if (this.size === Math.floor(this.getCapacity() / 4)) {
this.resize(this.getCapacity() / 2);
}
return element;
}
// 查看队首的元素
getFront() {
if (this.isEmpty()) {
throw new Error('queue is empty.');
}
return this.data[front];
}
// 查看实际的元素个数
getSize() {
return this.size;
}
// 查看容量
getCapacity() {
return this.data.length;
}
// 队列是否为空
isEmpty() {
// return this.size === 0;
return this.front == this.tail;
}
// 输出循环队列中的信息
// @Override toString 2018-10-20-jwl
toString() {
let arrInfo = `LoopQueue: size = ${this.getSize()},capacity = ${this.getCapacity()},\n`;
arrInfo += `data = front [`;
for (var i = 0; i < this.myArray.size - 1; i++) {
arrInfo += `${this.myArray.data[i]}, `;
}
if (!this.isEmpty()) {
arrInfo += `${this.myArray.data[this.myArray.size - 1]}`;
}
arrInfo += `] tail`;
// 在页面上展现
document.body.innerHTML += `${arrInfo}<br /><br /> `;
return arrInfo;
}
}
复制代码
Main
class Main {
constructor() {
this.alterLine('MyLoopQueue Area');
let mlq = new MyQueue(10);
for (let i = 1; i <= 10; i++) {
mlq.enqueue(i);
console.log(mlq.toString());
}
console.log(mlq.getFront());
this.show(mlq.getFront());
while (!mlq.isEmpty()) {
console.log(mlq.toString());
mlq.dequeue();
}
}
// 将内容显示在页面上
show(content) {
document.body.innerHTML += `${content}<br /><br />`;
}
// 展现分割线
alterLine(title) {
let line = `--------------------${title}----------------------`;
console.log(line);
document.body.innerHTML += `${line}<br /><br />`;
}
}
复制代码
O(n)
,
O(1)
,MyQueue
:数组队列,使用了自定义数组
void enqueue(E)
:O(1)
均摊E dequeue()
:O(n)
出队的性能消耗太大了E getFront()
:O(1)
int getSize()
:O(1)
boolean isEmpty()
:O(1)
MyLoopQueue
:循环队列,没有使用自定义数组
void enqueue(E)
:O(1)
均摊E dequeue()
:O(1)
均摊E getFront()
:O(1)
int getSize()
:O(1)
boolean isEmpty()
:O(1)
O(n)
的复杂度变为了O(1)
的复杂度,O(1)
为均摊的时间复杂度,O(1)
的时间复杂度。O(n)
的复杂度要比O(1)
要慢,
MyQueue,time:15.463472711s
,MyLoopQueue,time:0.009602136s
,MyLoopQueue,time:2.663835877s
,O(1)
对O(n)
、O(n)
对O(n^2)
、O(n)
对O(logn)
。PerformanceTest
class PerformanceTest {
constructor() {}
testQueue(queue, openCount) {
let startTime = Date.now();
let random = Math.random;
for (var i = 0; i < openCount; i++) {
queue.enqueue(random() * openCount);
}
while (!queue.isEmpty()) {
queue.dequeue();
}
let endTime = Date.now();
return this.calcTime(endTime - startTime);
}
// 计算运行的时间,转换为 天-小时-分钟-秒-毫秒
calcTime(result) {
//获取距离的天数
var day = Math.floor(result / (24 * 60 * 60 * 1000));
//获取距离的小时数
var hours = Math.floor((result / (60 * 60 * 1000)) % 24);
//获取距离的分钟数
var minutes = Math.floor((result / (60 * 1000)) % 60);
//获取距离的秒数
var seconds = Math.floor((result / 1000) % 60);
//获取距离的毫秒数
var milliSeconds = Math.floor(result % 1000);
// 计算时间
day = day < 10 ? '0' + day : day;
hours = hours < 10 ? '0' + hours : hours;
minutes = minutes < 10 ? '0' + minutes : minutes;
seconds = seconds < 10 ? '0' + seconds : seconds;
milliSeconds =
milliSeconds < 100
? milliSeconds < 10
? '00' + milliSeconds
: '0' + milliSeconds
: milliSeconds;
// 输出耗时字符串
result =
day +
'天' +
hours +
'小时' +
minutes +
'分' +
seconds +
'秒' +
milliSeconds +
'毫秒' +
' <<<<============>>>> 总毫秒数:' +
result;
return result;
}
}
复制代码
Main
class Main {
constructor() {
this.alterLine('Queues Comparison Area');
let mq = new MyQueue();
let mlq = new MyLoopQueue();
let performanceTest = new PerformanceTest();
let mqInfo = performanceTest.testQueue(mq, 10000);
let mlqInfo = performanceTest.testQueue(mlq, 10000);
this.alterLine('MyQueue Area');
console.log(mqInfo);
this.show(mqInfo);
this.alterLine('MyLoopQueue Area');
console.log(mlqInfo);
this.show(mlqInfo);
}
// 将内容显示在页面上
show(content) {
document.body.innerHTML += `${content}<br /><br />`;
}
// 展现分割线
alterLine(title) {
let line = `--------------------${title}----------------------`;
console.log(line);
document.body.innerHTML += `${line}<br /><br />`;
}
}
复制代码