Create a Priority Queue Class*

I do not understand what is the reason why dequeue is not working.
Tell us what’s happening:
Describe your issue in detail here.

  **Your code so far**

function PriorityQueue () {
this.collection = [];
this.printCollection = function() {
  console.log(this.collection);
};
this.enqueue = function (val){
     if(this.collection.length===0){
        return this.collection.push(val)
     }

  }
this.dequeue = function (val){
     for(let i=0;i<this.collection.length;i++){
                if(this.collection[i][1]<val[1]){
                      this.collection.push(val)        
                }else{
                        this.collection.splice(i,0,val)
                }
     }
}
this.size = function () {
     return this.collection.length  
}
this.isEmpty = function () {
      return this.collection.length===0
} 
this.front = function () {
      return this.collection[0]
}

}
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36.

Challenge: Create a Priority Queue Class

Link to the challenge:

So first of all, the dequeue method does not take an argument, it merely removes the next item waiting to be removed from the queue. Knowing this, the logic of the deque method should be fairly simple now.

Your enqueue logic is too simple. That’s where all the work is done and what makes a priority queue special. Remember, you can’t just tack an element onto the end of the collection array (unless the array is currently empty). You have to take into account the priority of the element as well.

Perhaps the description of a priority queue in the challenge isn’t enough? I found this description which seems to do a pretty good job explaining how it works. The code examples are in C, but that’s OK, you just need a description, not actual code.

Also, you could click the “Get Help” button, which does give a little better explanation of how a priority queue works. Just don’t be tempted to look at the solution quite yet :slight_smile:

1 Like

I have made some changes, but it still does not work.

function PriorityQueue () {
  this.collection = [];
  this.printCollection = function() {
    console.log(this.collection);
  };
  this.enqueue = function (...val) {
    for(let i=0;i<this.collection.length;i++){
                if(val[1]<this.collection[i][1]){
                          this.collection.splice(i,0,val)
                }
      }
      return this.collection
}
  this.dequeue = function (){
    return this.collection.shift()
  }
  this.size = function () {
    return this.collection.length
  }
  this.front = function () {
    return this.collection[0][0]
  }
  this.isEmpty = function (){
       return this.collection.length===0
  }
}
let alfa = new PriorityQueue ()

A few things:

this.enqueue = function (...val) {

What is ...val doing here? Remember, the input into the enqueue method is going to be an array of two elements (e.g ['kitten', 2]) where the first element is the value and the second is the priority integer. I’m not sure you need to put the array passed into this method into an array.

Also, what if collection is currently empty. I don’t see in your enqueue method that it handles this condition.

If you haven’t done so yet, I would highly recommend you install node on your computer. This will allow you to do all of your coding on your local computer, which will make it much easier to test your code as well.

1 Like

I do not see the bug. This should work.

function PriorityQueue () {

    var collection = [];

    this.printCollection = function() {

      (console.log(collection));

    };

    this.enqueue = function(e){

        if (this.collection.length==0){ 

            collection.push(e);

        } else {

            var contain = false;

            for (var i=0; i<collection.length; i++){

                 if (e[1] <collection[i][1]){ //checking priorities

                    collection.splice(i,0,e);

                    contain = true;

                    break;

                }

            }

            if (!contain){

                collection.push(element);

            }

        }

    };

    this.dequeue = function() {

        

        return collection.shift();

    };

    this.front = function() {

        return collection[0][0];

    };

    this.size = function() {

        return collection.length; 

    };

    this.isEmpty = function() {

        return collection.length === 0; 

    };

}

In your PriorityQueue funtion, you have defined a variable named collection to be an empty array. In your enqueue method, you reference this.collection which is undefined.

1 Like

Also, I see the above code reference a variable named element but you have not defined such a variable.

1 Like

It shows me this: The priority queue should return items with a higher priority before items with a lower priority and return items in first-in-first-out order otherwise. But, that should be enough, is there an article to read about this? I want to figure out his.

function PriorityQueue () {

    let collection = [];

    this.printCollection = function() {

      (console.log(collection));

    };

    this.enqueue = function(e){

        if (collection.length === 0){ 

            collection.push(e);

        } else {

            var contain = false;

            for (var i=0; i<collection.length; i++){

                 if (e[1] <collection[i][1]){ //checking priorities

                    collection.splice(i,0,e);

                    contain = true;

                    break;

                }

            }

            if (!contain){

                collection.push(e);

            }

        }

    };

    this.dequeue = function() {

        

        return collection.shift();

    };

    this.front = function() {

        return collection[0][0];

    };

    this.size = function() {

        return collection.length; 

    };

    this.isEmpty = function() {

        return collection.length === 0; 

    };

}

Make sure to read the specifics of the instructions:

dequeue and front should return only the item’s name, not its priority.

Your dequeue is not doing what is being asked.

1 Like
function PriorityQueue () {

    let collection = [];

    this.printCollection = function() {

      (console.log(collection));

    };

    this.enqueue = function(e){

        if (collection.length === 0){ 

            collection.push(e);

        } else {

            var contain = false;

            for (var i=0; i<collection.length; i++){

                 if (e[1] <collection[i][1]){ //checking priorities

                    collection.splice(i,0,e);

                    contain = true;

                    break;

                }

            }

            if (!contain){

                collection.push(e);

            }

        }

    };

    this.dequeue = function() {

        

        return collection.shift()[0];

    };

    this.front = function() {

        return collection[0][0];

    };

    this.size = function() {

        return collection.length; 

    };

    this.isEmpty = function() {

        return collection.length === 0; 

    };

}

Thanks! @bbsmooth and @RandellDawson

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.