user1907700
user1907700

Reputation:

C++ Priority Queue, logical error, can't figure out

I'm implementing a simple priority queue in C++.

However when it runs, it prints out gibberish numbers. Am I somehow trying to access invalid entries in the array in my code?

Below is the code.

Also, is my "remove" function somehow not doing its job? Conceptually, shall I be putting null into the first entry and return whatever was just erased?

Thanks.

[Priority.h]

#ifndef Priority_h
#define Priority_h


class Priority
{
    public:
        Priority(void);
        Priority(int s);
        ~Priority(void);

        void insert(long value);
        long remove();
        long peekMin();
        bool isEmpty();
        bool isFull();

        int maxSize;
        long queArray [5];
        int nItems; 

    private:

};

#endif

[Priority.cpp]

#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include "Priority.h"

using namespace std;

Priority::Priority(void)
{

}

Priority::Priority(int s)
{
    nItems = 0;
}

Priority::~Priority(void)
{

}

void Priority::insert(long item)    
{
      int j;

      if(nItems==0)                         // if no items,
            {
            queArray[0] = item; nItems++;
            }// insert at 0
      else                                // if items,
         {
         for(j=nItems-1; j=0; j--)         // start at end,
            {
            if( item > queArray[j] )      // if new item larger,
               queArray[j+1] = queArray[j]; // shift upward
            else                          // if smaller,
               break;                     // done shifting
            }  // end for
         queArray[j+1] = item;            // insert it
         nItems++;
         }  // end else (nItems > 0)

}

long Priority::remove()             
{ 
    return queArray[0];
}

long Priority::peekMin()            
{ 
    return queArray[nItems-1]; 
}

bool Priority::isEmpty()         
{ 
    return (nItems==0);
}

bool Priority::isFull()          
{
    return (nItems == maxSize); 
}

int main ()
{
      Priority thePQ; 
      thePQ.insert(30);
      thePQ.insert(50);
      thePQ.insert(10);
      thePQ.insert(40);
      thePQ.insert(20);

      while( !thePQ.isEmpty() )
         {
         long item = thePQ.remove();
         cout << item << " ";  // 10, 20, 30, 40, 50
         }  // end while
      cout << "" << endl;

    system("pause");
}

Upvotes: 0

Views: 226

Answers (3)

Silas
Silas

Reputation: 406

I agree with the other answers here, but I would add this:

Your "Remove" method isn't actually removing anything - it is just returning the first element - but it doesn't do anything to the array itself.

Edited to say that your insert method needs some work - it may or may not write over the end of the array, but it is certainly confusing as to what it is doing.

Upvotes: 0

Ben Collins
Ben Collins

Reputation: 20686

Try initializing your queue array in the constructor.

Upvotes: 0

NPE
NPE

Reputation: 500933

Here is one error:

     for(j=nItems-1; j=0; j--)         // start at end,
                      ^ this is assignment, not comparison.

I am also not convinced that there isn't an off-by-one error in

     queArray[j+1] = item;            // insert it

Finally, your default constructor fails to initialize nItems.

There could be further errors, but I'll stop at this.

Upvotes: 4

Related Questions