当前位置:网站首页>Queue, two-way queue, and its application
Queue, two-way queue, and its application
2022-06-27 10:17:00 【Xiaaoke】
queue
characteristic : fifo FIFO ( similar , line up )
/** * queue * characteristic : fifo FIFO ( similar , line up ) * * enqueue(element(s)): Add one or more new items to the end of the queue * dequeue: Remove the first item from the queue , And return the * peek: Returns the first element in the queue * isEmpty: Determine whether it is null * size: Returns the length of the elements in the stack * clear: Clear the elements in the stack * toString: Array toString Method * * @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;
}
}
// test
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);
Queue application ( Beat the drum to spread the flowers )
// Beat the drum to spread the flowers
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} It was eliminated in the drumming and flower passing `)
})
console.log(` The winner :${
result.winner}`)
The bidirectional queue
A combination of stacks and queues
/** * addFront: Add elements to the front of the queue * addBack: Add elements to the back end of the queue * removeFront: The front end of the queue removes the element * peekBack: The queue back end removes the element * isEmpty: Determine whether it is null * size: Returns the length of the elements in the stack * clear: Clear the elements in the stack * toString: Array toString Method * * @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
Two way queue application ( Palindrome checker )
// Palindrome checker
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('') === ''
边栏推荐
- .NET 中的引用程序集
- R语言使用econocharts包创建微观经济或宏观经济图、demand函数可视化需求曲线(demand curve)、自定义配置demand函数的参数丰富可视化效果
- Review of last week's hot spots (6.20-6.26)
- Installation manuelle de MySQL par UBUNTU
- Flutter wechat sharing
- 【SO官方采访】为何使用Rust的开发者如此深爱它
- C language learning day_ 05
- leetcode:968. Monitor the binary tree [tree DP, maintain the three states of each node's subtree, it is very difficult to think of the right as a learning, analogous to the house raiding 3]
- The R language uses the preprocess function of the caret package for data preprocessing: Center all data columns (subtract the average value from each data column), and set the method parameter to cen
- C# Any()和AII()方法
猜你喜欢

When does the mobile phone video roll off?

Feedforward feedback control system design (process control course design matlab/simulink)

CPU design (single cycle and pipeline)

详解各种光学仪器成像原理

Win10快捷键整理
![[hcie-rs review mind map] - STP](/img/b5/b89e59fe7f23bf23feeadb991acba7.png)
[hcie-rs review mind map] - STP

Easy to understand Laplace smoothing of naive Bayesian classification

【TcaplusDB知识库】Tmonitor后台一键安装介绍(二)

Oracle连接MySQL报错IM002

学习笔记之——数据集的生成
随机推荐
BufferedWriter 和 BufferedReader 的使用
2-4Kali下安装nessus
Win10 shortcut key sorting
Dimitt's law
flutter 微信分享
新旧两个界面对比
File name setting causes an error to be written to writelines: oserror: [errno 22] invalid argument
leetcode:522. 最长特殊序列 II【贪心 + 子序列判断】
R语言plotly可视化:plotly可视化基础小提琴图(basic violin plot in R with plotly)
R语言plotly可视化:可视化多个数据集归一化直方图(historgram)并在直方图中添加密度曲线kde、设置不同的直方图使用不同的分箱大小(bin size)、在直方图的底部边缘添加边缘轴须图
Test how students participate in codereview
leetcode待做题目
【面经】云泽科技
Reorganize common shell scripts for operation and maintenance frontline work
【SO官方采访】为何使用Rust的开发者如此深爱它
C any() and aii() methods
[registration] infrastructure design: from architecture hot issues to industry changes | tf63
R語言plotly可視化:可視化多個數據集歸一化直方圖(historgram)並在直方圖中添加密度曲線kde、設置不同的直方圖使用不同的分箱大小(bin size)、在直方圖的底部邊緣添加邊緣軸須圖
In the three-tier architecture, at which layer is the database design implemented, not at the data storage layer?
【HCIE-RS复习思维导图】- STP