介绍
如果用数组实现个简单的栈,那么弹出和插入复杂度都是O(N),因为会涉及到数组的动态扩容。
这里可以使用「线性栈」和「链表栈」:
线性栈实现
这里借助了指针来实现线性栈。从而避免自动阔缩数组。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| class LinearStack { constructor(capcity = 16) { this.capcity = capcity; this.container = new Array(capcity); this.count = 0; }
push(data) { if (this.count === this.capcity) { return false; } this.container[this.count] = data; ++this.count; return true; }
pop() { if (this.count === 0) { return null; }
const data = this.container[this.count - 1]; --this.count; return data; }
changeCapcity(capcity = 20) { if (capcity <= this.capcity) { return false; } this.capcity = capcity;
const newContainer = new Array(capcity); this.container.forEach((data, index) => (newContainer[index] = data)); this.container = newContainer; return true; } }
|
使用效果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| const stack = new LinearStack(2); stack.push(1); stack.push(2); stack.push(3);
console.log(stack.container); console.log(stack.pop()); console.log(stack.pop()); console.log(stack.pop());
stack.push(1); stack.push(2); stack.changeCapcity(3); stack.push(3); console.log(stack.container);
|