当前位置:网站首页>队列,双向队列,及其运用
队列,双向队列,及其运用
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('') === ''
边栏推荐
- Parameters argc and argv of main()
- Video file too large? Use ffmpeg to compress it losslessly
- Introduction to websocket protocol
- vim 从嫌弃到依赖(20)——global 命令
- 0号进程,1号进程,2号进程
- Design of a solar charge pump power supply circuit
- 多個類的設計
- Conception de plusieurs classes
- Quelques exercices sur les arbres binaires
- 针对直播痛点的关键技术解析——首帧秒开、清晰度、流畅度
猜你喜欢

oracle怎样将字符串转为多行

HiTek电源维修X光机高压发生器维修XR150-603-02

IO pin configuration and pinctrl drive

粗读DS-TransUNet: Dual Swin Transformer U-Net for Medical Image Segmentation

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

Flow chart of Alipay wechat payment business

That is, a one-stop live broadcast service with "smooth live broadcast" and full link upgrade

高等数学第七章微分方程

Win10 add right-click menu for any file
![[MySQL basic] general syntax 1](/img/f2/fb38409c034546e503d08a0b96cc61.png)
[MySQL basic] general syntax 1
随机推荐
Object contains copy method?
MATLAB小技巧(18)矩阵分析--熵权法
IO pin configuration and pinctrl drive
Reading and writing Apache poi
经典的一道面试题,涵盖4个热点知识
提高效率 Or 增加成本,开发人员应如何理解结对编程?
我大抵是卷上瘾了,横竖睡不着!竟让一个Bug,搞我两次!
了解神经网络结构和优化方法
1098 Insertion or Heap Sort(堆排序解释)(PAT甲级)
Rman-08137 main library failed to delete archive file
【系统设计】邻近服务
2022.06.26(LC_6101_判断矩阵是否是一个 X 矩阵)
内存泄露的最直接表现
RockerMQ消息发送模式
Principle and application of the most complete H-bridge motor drive module L298N
The markdown plug-in of the browser cannot display the picture
[MySQL basic] general syntax 1
2022.06.26 (LC Luo 6101 Luo determines whether the matrix is an X matrix)
Oracle uses an SQL to find out which data is not in a table
Today's three interviews demo[integer ASCII class relationship]