This article example shares the specific code of javascript to implement double-ended queues for your reference. The specific content is as follows 1. Double-ended queueA deque is a special queue that allows us to add and remove elements from both the front and back ends at the same time. 2. Application of double-ended queuesIf a person who has just bought a ticket needs to ask some simple information, he can go back to the head of the line. If the person at the end of the line is in a hurry, he can leave the line. 3. Double-ended queue method addFront(element): This method adds a new element to the front of the double-ended queue 4. Implementationclass Deque{ constructor(){ this.items = {}; this.count = 0; this.lowestCount = 0; } // Add a new element to the front of the double-ended queue addFront(element){ if(this.isEmpty()){ this.addBack(element); } else if(this.lowestCount > 0){ this.lowestCount --; this.items[this.lowestCount] = element; } else{ for(let i=this.count;i>0;i--){ this.items[i] = this.items[i-1]; } this.lowestCount = 0; this.items[this.lowestCount] = element; this.count++; } }; addBack(element){ this.count++; this.items[this.count-1] = element; }; 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; } const result = this.items[this.count-1]; delete this.items[this.count-1]; this.count--; return result; }; peekFront(){ if(this.isEmpty()){ return null; } return this.items[this.lowestCount]; }; peekBack(){ if(this.isEmpty()){ return null; } return this.items[this.count-1]; }; isEmpty(){ return this.count - this.lowestCount == 0; } size(){ return this.count - this.lowestCount; } toString(){ if(this.isEmpty()){ return ''; } let objString = `${this.items[this.lowestCount]}`; for(var i=this.lowestCount+1;i<this.count;i++){ objString = `${objString},${this.items[i]}`; } return objString; } clear(){ this.items={}; this.count = 0; this.lowestCount = 0; } } const deque = new Deque(); deque.addFront('John'); deque.addFront('Jack'); deque.addFront('Amy'); deque.addBack('Lisa'); // deque.removeFront(); // deque.removeBack(); console.log(deque.size()); console.log(deque.toString()); console.log(deque); console.log(deque.isEmpty()); console.log(deque.clear()); console.log(deque); The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: The most commonly used HTML tags to create web pages
>>: Use CSS to switch between dark mode and bright mode
Table of contents 1. What is an Operating System ...
I encountered this problem today. I reassigned the...
Normally, you'll need to read everyone's s...
Table of contents 1. What is vuex 2. Installation...
This article example shares the specific code of ...
This article uses examples to describe MySQL tran...
Recently, Oracle announced the public availabilit...
1. How do I remove the blank space of a few pixels...
I saw an article in Toutiao IT School that CSS pe...
Recently I need to change the version of MySQL da...
I don’t know if you have noticed that when we ope...
CentOS 8 is officially released! CentOS fully com...
Configuration file that needs to be loaded when t...
Application Scenario In many cases, we install so...
Table of contents 1. Introduction to v-slot 2. An...