当前位置:网站首页>队列,双向队列,及其运用
队列,双向队列,及其运用
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('') === ''
边栏推荐
- 我大抵是卷上瘾了,横竖睡不着!竟让一个Bug,搞我两次!
- Getting started with webrtc: 12 Rtendpoint and webrtcendpoint under kurento
- Creation process and memory layout of objects at JVM level
- orthofinder直系同源蛋白分析及结果处理
- The background prompt module for accessing fastadmin after installation does not exist
- 第十一章 信号(一)- 概念
- vim 从嫌弃到依赖(19)——替换
- ServletConfig and ServletContext
- 招聘需求 视觉工程师
- This, constructor, static, and inter call must be understood!
猜你喜欢
最全H桥电机驱动模块L298N原理及应用
[vivid understanding] the meanings of various evaluation indicators commonly used in deep learning TP, FP, TN, FN, IOU and accuracy
ucore lab5
Markem imaje马肯依玛士喷码机维修9450E打码机维修
快速入门CherryPy(1)
prometheus告警流程及相关时间参数说明
1098 Insertion or Heap Sort(堆排序解释)(PAT甲级)
RMAN-08137 主库无法删除归档文件
为智能设备提供更强安全保护 科学家研发两种新方法
Matlab tips (19) matrix analysis -- principal component analysis
随机推荐
this,构造器,静态,之间调用,必须搞懂啊!
Analysis log log
[ 扩散模型(Diffusion Model) ]
Matlab tips (19) matrix analysis -- principal component analysis
Analysis of key technologies for live broadcast pain points -- second opening, clarity and fluency of the first frame
vim 从嫌弃到依赖(20)——global 命令
快捷键 bug,可复现(貌似 bug 才是需要的功能 [滑稽.gif])
有关二叉树的一些练习题
支付宝微信支付业务流程图
MATLAB小技巧(19)矩阵分析--主成分分析
我大抵是卷上瘾了,横竖睡不着!竟让一个Bug,搞我两次!
Collection framework generic LinkedList TreeSet
高等数学第七章微分方程
Modify the contents of /etc/crontab file directly, and the scheduled task will not take effect
webrtc入门:12.Kurento下的RtpEndpoint和WebrtcEndpoint
Shortcut key bug, reproducible (it seems that bug is the required function [funny.Gif])
NoSQL database redis installation
Tips for using Jupiter notebook
Hitek power supply maintenance X-ray machine high voltage generator maintenance xr150-603-02
数据类型占内存大小?LongVsObject