当前位置:网站首页>队列,双向队列,及其运用
队列,双向队列,及其运用
2022-06-27 09:14:00 【Xiaaoke】
队列
特点:先进先出 FIFO (类似,排队)
/** * 队列 * 特点:先进先出 FIFO (类似,排队) * * enqueue(element(s)):向队尾添加一个或多个新的项 * dequeue:移除队列的第一项,并返回该项 * peek:返回队列中第一个元素 * isEmpty:判断是否为空 * size:返回栈里元素的长度 * clear:清空栈里的元素 * toString:数组中的toString方法 * * @class Queue */
class Queue{
constructor(){
this._count = 0;
this._lowestCount = 0;
this._items = {
};
}
isEmpty(){
return this._count - this._lowestCount === 0;
}
enqueue(...elem){
for(let i = 0; i < elem.length; i++){
this._items[this._count] = elem[i];
this._count ++;
}
}
dequeue(){
if(this.isEmpty()){
return undefined;
}
const result = this._items[this._lowestCount];
delete this._items[this._lowestCount];
this._lowestCount ++;
return result;
}
peek(){
if(this.isEmpty()){
return undefined;
}
return this._items[this._lowestCount];
}
size(){
return this._count - this._lowestCount;
}
clear(){
this._items = {
};
this._count = 0;
this._lowestCount = 0;
}
toString(){
if(this.isEmpty()){
return '';
}
let objString = `${
this._items[this._lowestCount]}`;
for(let i = this._lowestCount + 1; i < this._count; i++ )
objString = `${
objString},${
this._items[i]}`;
return objString;
}
}
// 测试
const queue = new Queue();
console.log(queue.isEmpty()); // true
queue.enqueue('Joho');
queue.enqueue('Jack');
console.log(queue.toString()); // Joho,Jack
queue.enqueue('Camila');
console.log(queue.size()); // 3
console.log(queue.toString()); // Joho,Jack,Camila
console.log(queue.isEmpty()); // false
queue.dequeue();
queue.dequeue();
console.log(queue.toString()); // Camila
queue.enqueue('Joho','Joho','Camila');
console.log(queue.toString());
console.log(queue);
队列运用(击鼓传花)
// 击鼓传花
const hotPotato = (elementList, num) => {
const queue = new Queue();
const elimitatedList = [];
for(let i = 0; i < elementList.length; i++){
queue.enqueue(elementList[i]);
}
while(queue.size() > 1){
for(let i = 0; i < num; i++){
queue.enqueue(queue.dequeue());
}
elimitatedList.push(queue.dequeue());
}
return {
elimitated: elimitatedList,
winner: queue.dequeue()
}
}
const names = ['zhangsan', 'lisi', 'wangwu', 'zhaoliu'];
const result = hotPotato(names, 6);
result.elimitated.forEach(name => {
console.log(`${
name} 在击鼓传花中被淘汰啦`)
})
console.log(`胜利者:${
result.winner}`)
双向队列
栈和队列的结合体
/** * addFront:队列前端添加元素 * addBack:队列后端添加元素 * removeFront:队列前端移除元素 * peekBack:队列后端移除元素 * isEmpty:判断是否为空 * size:返回栈里元素的长度 * clear:清空栈里的元素 * toString:数组中的toString方法 * * @class Deque */
class Deque{
constructor(){
this._count = 0;
this._lowestCount = 0;
this._items = {
};
}
addFront(elem){
if(this.isEmpty()){
this.addBack(elem);
}else if(this._lowestCount > 0){
this._lowestCount --;
this._items[this._lowestCount] = elem;
}else {
for(let i = this._count; i > 0; i--){
this._items[i] = this._items[i-1];
}
this._count++;
this._lowestCount = 0;
this._items[0] = elem;
}
}
addBack(...elem){
for(let i = 0; i < elem.length; i++){
this._items[this._count] = elem[i];
this._count ++;
}
}
removeFront(){
if(this.isEmpty()){
return undefined;
}
const result = this._items[this._lowestCount];
delete this._items[this._lowestCount];
this._lowestCount ++;
return result;
}
removeBack(){
if(this.isEmpty()){
return undefined;
}
this._count --;
const result = this._items[this._count];
delete this._items[this._count];
return result;
}
peekFront(){
if(this.isEmpty()){
return undefined;
}
return this._items[this._lowestCount];
}
peekBack(){
if(this.isEmpty()){
return undefined;
}
return this._items[this._count - 1];
}
isEmpty(){
return this._count - this._lowestCount === 0;
}
size(){
return this._count - this._lowestCount;
}
clear(){
this._items = {
};
this._count = 0;
this._lowestCount = 0;
}
toString(){
if(this.isEmpty()){
return '';
}
let objString = `${
this._items[this._lowestCount]}`;
for(let i = this._lowestCount + 1; i < this._count; i++ )
objString = `${
objString},${
this._items[i]}`;
return objString;
}
}
const deque = new Deque();
console.log(deque.isEmpty()); // true
deque.addBack('john');
deque.addBack('jack');
console.log(deque.toString()); // john,jack
deque.addBack('camila');
console.log(deque.toString()); // john,jack,camila
console.log(deque.size()); // 3
console.log(deque.isEmpty()); // false
deque.removeFront();
console.log(deque.toString()); // jack,camila
deque.addFront('john');
console.log(deque.toString()); // john,jack,camila
双向队列运用(回文检查器)
// 回文检查器
const palindromeChecker = (aString) => {
if(aString === undefined || aString === null || (aString !== null && aString.length === 0)){
return false;
}
const deque = new Deque();
const lowerString = aString.toLocaleLowerCase().split(' ').join('');
let isEqual = true;
let fistChar, lastChat;
for(let i = 0; i < lowerString.length; i++){
deque.addBack(lowerString.charAt(i));
}
while(deque.size() > 1 && isEqual){
fistChar = deque.removeFront();
lastChat = deque.removeBack();
if(fistChar !== lastChat){
isEqual = false;
}
}
return isEqual;
}
console.log('a', palindromeChecker('a'))
console.log('abc', palindromeChecker('abc'))
console.log('aba', palindromeChecker('aba'))
// ''.split('').reverse().join('') === ''
边栏推荐
- Installation and use of SVN version controller
- Take you to play with the camera module
- 今日3大面试Demo[Integer ASCII 类关系]
- How Oracle converts strings to multiple lines
- 2022.06.26(LC_6101_判断矩阵是否是一个 X 矩阵)
- MYSQL精通-01 增删改
- IO管脚配置和pinctrl驱动
- 最全H桥电机驱动模块L298N原理及应用
- E+h secondary meter repair pH transmitter secondary display repair cpm253-mr0005
- 招聘需求 视觉工程师
猜你喜欢

如何获取GC(垃圾回收器)的STW(暂停)时间?

Hitek power supply maintenance X-ray machine high voltage generator maintenance xr150-603-02

高等数学第七章微分方程

Design of a solar charge pump power supply circuit

E+H二次表维修PH变送器二次显示仪修理CPM253-MR0005
![[MySQL basic] general syntax 1](/img/f2/fb38409c034546e503d08a0b96cc61.png)
[MySQL basic] general syntax 1

Apache POI的读写

IMX8QXP DMA资源和使用(未完结)

IO pin configuration and pinctrl drive

Preliminary understanding of pytorch
随机推荐
Object contains copy method?
Take you to play with the camera module
There is no doubt that this is an absolutely elaborate project
有關二叉樹的一些練習題
Tips for using Jupiter notebook
视频文件太大?使用FFmpeg来无损压缩它
微信小程序学习之五种页面跳转方法.
IO管脚配置和pinctrl驱动
不会初始化类的几种Case
了解神经网络结构和优化方法
Win10 add right-click menu for any file
Flow chart of Alipay wechat payment business
Chapter 11 signal (I) - concept
冒牌构造函数???
MATLAB小技巧(18)矩阵分析--熵权法
ucore lab4
Analysis of orthofinder lineal homologous proteins and result processing
Conception de plusieurs classes
不容置疑,这是一个绝对精心制作的项目
Object含有Copy方法?