JavaScript implements double-ended queue

JavaScript implements double-ended queue

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 queue

A 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 queues

If 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
addBack(element): This method adds a new element to the back end of the double-ended queue (the implementation method is the same as the enqueue method in the Queue class).
removeFront(): This method removes the first element from the front of the double-ended queue
removeBack(): This method removes the first element from the back end of the double-ended queue
peekFront(): This method returns the first element of the double-ended queue.
peekBack()): This method returns the first element at the back end of the double-ended queue.

4. Implementation

class 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:
  • Detailed implementation and application of queues and double-ended queues in JS
  • Write an event queue operation function in js
  • Detailed explanation of the stack method and queue method of JavaScript array
  • JS implementation of queue and stack method
  • Detailed explanation of JavaScript queue functions and asynchronous execution
  • Detailed explanation of the queue example in JS algorithms and data structures
  • JS implementation of queue first-in-first-out function example
  • Circular queue code implemented using array in javascript
  • JavaScript queues, priority queues and circular queues
  • JS implements a method of using two queues to represent a stack

<<:  The most commonly used HTML tags to create web pages

>>:  Use CSS to switch between dark mode and bright mode

Recommend

5 Ways to Clear or Delete Large File Contents in Linux

Sometimes, while working with files in the Linux ...

Analysis of examples of using anti-shake and throttling in Vue components

Be careful when listening for events that are tri...

CSS sample code with search navigation bar

This article shows you how to use CSS to create a...

Markup language - web application CSS style

Click here to return to the 123WORDPRESS.COM HTML ...

CSS3 realizes draggable Rubik's Cube 3D effect

Mainly used knowledge points: •css3 3d transforma...

Detailed explanation of how to view the number of MySQL server threads

This article uses an example to describe how to v...

HTML solves the problem of invalid table width setting

If you set the table-layer:fixed style for a tabl...

Vue parent component calls child component function implementation

Vue parent component calls the function of the ch...

Summary of some reasons why crontab scheduled tasks are not executed

Preface I recently encountered some problems at w...

Vue2 implements provide inject to deliver responsiveness

1. Conventional writing in vue2 // The parent com...

How to modify the mysql table partitioning program

How to modify the mysql table partitioning progra...

Control the vertical center of the text in the HTML text box through CSS

When the height attribute of Text is defined, the ...

Analysis of the method of setting up scheduled tasks in mysql

This article uses an example to describe how to s...