1

For my Computer Science class I am implementing a templated Priority Queue class and I am struggling to implement the enqueue function. I know the error is in the if(value < back) block of the function. Here is the Function:

template<class T>
bool PriorityQueue<T>::enqueue(T& value){

   if(!isEmpty()){
        if(value < back()){
        Node<T>* curr = f_ptr;
        Node<T>* prev = NULL;
        while(value > curr->data() && curr != NULL){
            prev = curr;
            curr = curr->m_next;
        }
        if(curr != NULL && value == curr->data()){
            prev = curr;
            curr = curr->m_next;
        }
        Node<T>* tmp = new Node<T>(value,curr);
        prev->m_next = tmp;
        return true;
        }else{
            b_ptr->m_next = new Node<T>(value);
            b_ptr = b_ptr->m_next;
        }
   }else{
    f_ptr = new Node<T>(value);
    b_ptr = f_ptr;
    return true;
   }
}

The Helper functions are all functioning properly. Please explain what part of my logic is incorrect.

  • Would you please explain what you're trying to accomplish with `if(curr != NULL && value == curr->data()){/*code*/}`? Also, are you trying to put larger values at the front of the priority queue or at the end of it? `if( value < back() )` makes it look like larger values should go at the end of the priority queue (i.e. it's meant to be smallest -> largest), but `while( value > curr->data() ... )` makes it look like you're searching for where the new value is _smaller_ than the remaining data (ergo the priority queue would be largest -> smallest). – Shaavin Apr 05 '20 at 20:10
  • 1
    One thing that is wrong is that not all the paths through the function return a value. – john Apr 05 '20 at 20:13
  • Have you tried a debugger on this? I'm sure you would find the problem very quickly. – john Apr 05 '20 at 20:14
  • @Shaavin The priority should be smallest to largest, I forgot to mention the if statement you pointed out checks to see if the values are they same and handles it accordingly. – MichaelConnors1017 Apr 05 '20 at 21:46
  • @john I dont have access to a debugger within my current dev environment. – MichaelConnors1017 Apr 05 '20 at 21:47
  • @MichaelConnors1017 Programming is very hard without a debugger. – john Apr 06 '20 at 06:02
  • 1
    If you don't have a debugger, then you put `cout` statements in your code so that it prints information about what it's doing. You might even insert pauses where you have to press a key to continue. That way, you can look at your code while following the trace output. It's not quite as convenient as a debugger, but it's very helpful. – Jim Mischel Apr 06 '20 at 14:36

0 Answers0