I have written a priority queue program using an array. It works except that the values pop out highest first. I could probably change the pop function to read from the end, but I would like to just fix whatever is wrong with the push function.
Here is the function:
The example I am using is the set {4, 5, 2}. I think the data should end up being stored in the array as {2, 4, 5} with 2 at index 0. This program is putting 5 at index 0.Code:void PArrayClass:: pushData(int num) { if(rear<(MAX_SIZE-2))//make sure there is room to add something { for (int check=0; num<=data[check]; check++) { cout << check << ", " << data[check] << endl; //added to make sure this loop really did something }//find where to insert new cell for (int move=rear; move>=check; move--) { data[move+1]=data[move]; }//move all cells over one data[check]=num; rear++; noOfItems++; } else { cout << "\nNo more room to add data.\n"; } }
If it makes any difference, I initialized the array to all 0's.
Thank you for your help.