当前位置:网站首页>队列,双向队列,及其运用
队列,双向队列,及其运用
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('') === ''
边栏推荐
- Summary of three basic interview questions
- Principle and application of the most complete H-bridge motor drive module L298N
- 针对直播痛点的关键技术解析——首帧秒开、清晰度、流畅度
- 多个类的设计
- 三道基础面试题总结
- RockerMQ消息发送模式
- VIM from dislike to dependence (19) -- substitution
- 1098 insertion or heap sort (PAT class a)
- [ 扩散模型(Diffusion Model) ]
- Win10 add right-click menu for any file
猜你喜欢

ucore lab3

I'm almost addicted to it. I can't sleep! Let a bug fuck me twice!

视频文件太大?使用FFmpeg来无损压缩它

I'm almost addicted to it. I can't sleep! Let a bug fuck me twice!

C# 解决使用SQLite 的相对路径问题

Video file too large? Use ffmpeg to compress it losslessly

One week's experience of using Obsidian (configuration, theme and plug-in)

Advanced mathematics Chapter 7 differential equations

【生动理解】深度学习中常用的各项评价指标含义TP、FP、TN、FN、IoU、Accuracy

There is no doubt that this is an absolutely elaborate project
随机推荐
微信小程序学习之五种页面跳转方法.
Demand visual Engineer
即构「畅直播」,全链路升级的一站式直播服务
How Oracle converts strings to multiple lines
RockerMQ消息发送模式
使线程释放锁资源的操作/方法重载一点注意事项
ucore lab3
Digital ic-1.9 understands the coding routine of state machine in communication protocol
IO pin configuration and pinctrl drive
Internal class ~ lock ~ access modifier
Some considerations on operation / method overloading for thread to release lock resources
I'm almost addicted to it. I can't sleep! Let a bug fuck me twice!
webrtc入门:12.Kurento下的RtpEndpoint和WebrtcEndpoint
Object含有Copy方法?
招聘需求 视觉工程师
Markem Imaje Marken IMAS printer maintenance 9450e printer maintenance
冒牌构造函数???
Today's three interviews demo[integer ASCII class relationship]
2022.6.26-----leetcode. seven hundred and ten
About the problem that the El date picker Click to clear the parameter and make it null