一、栈
1.1栈的特点
1.2栈的实现
class Stack {
/* 创建一个栈空间 */
constructor() {
this.stack = [];
console.log('创建一个栈空间');
}
/* 入栈 */
push(...arg) {
this.stack.push(...arg);
console.log(`${[...arg]}入栈--当前栈${this.stack}`);
}
/* 出栈 */
pop() {
if(!this.stack.length) {
console.log('当前栈中已无元素!');
return;
}
console.log(`${this.stack.pop()}出栈--当前栈${this.stack}`);
}
/* 获得栈顶元素 */
getTopOfStack() {
if(!this.stack.length) {
console.log('当前栈中无元素!');
return;
}
const top = this.stack[this.stack.length - 1];
console.log(`当前栈中栈顶元素:${top}`);
return top;
}
/* 获得栈底元素 */
getBottomOfStack() {
if(!this.stack.length) return;
const bottom = this.stack[0];
console.log(`当前栈中栈底元素:${bottom}`);
return bottom;
}
/* 获得第n个元素 */
get(n) {
if(n> this.stack.length) {
console.log(`当前栈空间总长度为${this.stack.length}!`);
}
const ele = this.stack[n-1];
console.log(`当前栈中第${n}个元素:${ele}`);
return ele;
}
/* 获得栈的长度 */
length(){
console.log(`当前栈长度为${this.stack.length}`);
return this.stack.length;
}
/* 栈的toString方法 */
toString(){
this.stack.toString();
}
/* 判断栈是否为空 */
isEmpty(){
return this.stack.length===0;
}
}
/* const stack = new Stack();
stack.push(1,2,3,4,5);
stack.pop();
stack.pop();
stack.getTopOfStack();
stack.getBottomOfStack();
stack.get(1);
stack.get(2);
stack.get(3);
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.length();
stack.toString(); */
二、队列
2.1队列的特点
2.2队列的实现
function Queue() {
this.queue = [];
console.log('创建一个队列');
}
Queue.prototype = {
constructor: Queue,
/* 入队 */
enqueue(...arg) {
this.queue.push(...arg);
console.log(`${[...arg]}入队--当前队列${this.toString()}`);
},
/* 出队 */
dequeue() {
const shift = this.queue.shift();
shift ? console.log(`${shift}出队--当前队列${this.toString()}`) :
console.log(`队列为空,出队失败`);
},
/* 返回第一项 */
front() {
const first = this.queue[0];
!first ? console.log(`获得队列第一项失败,因为当前队列为空`) : console.log(`队列第一项为${this.queue[0]}`);
return first;
},
/* 判断队列是否为空 */
isEmpty() {
return this.length === 0;
},
/* 返回队列长度 */
size() {
return this.queue.length;
},
toString() {
return this.queue.toString();
}
};
/* const queue = new Queue();
queue.enqueue(1, 2, 3, 4, 5);
queue.dequeue();
queue.dequeue();
queue.dequeue();
queue.front();
queue.dequeue();
queue.dequeue();
queue.dequeue();
queue.front(); */
2.3优先级队列的实现
class PriorityQueue {
constructor() {
this.queue = [];
}
/*入队 */
enqueue({
ele,
priority
}) {
if (isNaN(priority)) {
console.log(`入队失败,请输入有效的优先级---当前队列${this.toString()}`);
return;
}
if (this.queue.length === 0) {
this.queue.push({
ele,
priority
});
} else {
let i = -1;
this.queue.find(({
ele: e,
priority: p
}, index) => {
if (p > priority) {
i = index;
return true;
}
});
i >= 0 ? this.queue.splice(i, 0, {
ele,
priority
}) : this.queue.push({
ele,
priority
});
}
console.log(`(${ele+','+priority})入队---当前队列${this.toString()}`);
}
/* 出队 */
dequeue() {
const shift = this.queue.shift();
shift ? console.log(`${this.toSingleString(shift)}出队--当前队列${this.toString()}`) :
console.log(`队列为空,出队失败`);
}
/* 返回第一项 */
front() {
const first = this.queue[0];
!first ? console.log(`获得队列第一项失败,当前队列为空`) : console.log(`队列第一项为${this.toSingleString(this.queue[0])}`);
return first;
}
/* 判断队列是否为空 */
isEmpty() {
return this.length === 0;
}
/* 返回队列长度 */
size() {
return this.queue.length;
}
/* 输出'[(,),(,),(,),...]' */
toString() {
if (this.queue.length === 0) return '';
let str = ``;
for (const {
ele,
priority
} of this.queue) {
str += `(${ele},${priority}),`
}
return str.substr(0, str.length - 1);
}
/* 输出'(,)' */
toSingleString(obj) {
return `(${obj.ele},${obj.priority})`;
}
}
/* const priorityQueue = new PriorityQueue();
priorityQueue.enqueue({
ele: 'a',
priority: 5
});
priorityQueue.enqueue({
ele: 'b',
priority: 4
});
priorityQueue.enqueue({
ele: 'c',
priority: 3
});
priorityQueue.enqueue({
ele: 'd',
priority: 2
});
priorityQueue.enqueue({
ele: 'e',
priority: 1
});
priorityQueue.front();
priorityQueue.dequeue();
priorityQueue.dequeue();
priorityQueue.dequeue();
priorityQueue.dequeue();
priorityQueue.dequeue();
priorityQueue.front();
priorityQueue.dequeue(); */