javascript Realizes Two Ended Queue

  • 2021-12-04 09:30:13
  • OfStack

In this paper, we share the specific code of javascript to realize double-ended queue for your reference. The specific contents are as follows

1. Two-ended queue

Two-ended queue is a special queue that allows us to add and remove elements from both front-end and back-end

2. Application of two-ended queue

A person who has just bought a ticket can go directly back to the head of the team if he only needs to ask some simple information, and the person at the end of the team can leave the team directly if he is in a hurry

3. Two-ended queue method

addFront (element): This method adds a new element to the front end of the two-ended queue
addBack (element): This method adds a new element to the back end of the two-ended queue (the implementation is the same as the enqueue method in the Queue class).
removeFront (): This method removes the first element from the front end of the two-ended queue
removeBack (): This method removes the first element from the back end of the two-ended queue
peekFront (): This method returns the first element of a two-ended queue.
peekBack ()): This method returns the first element of the back end of the two-ended queue.

4. Implement


class Deque{
           constructor(){
               this.items = {};
               this.count = 0;
               this.lowestCount = 0; 
           }

        //  Add a new element to the front end of a two-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);

Related articles: