Priority Queue with Heap
May 7, 2014 at 6:34pm UTC
I am trying to print items as I remove them from the priority queue, but no data is being printed. The data is going in properly but it is not printing as it is coming out. Please help. The Heap_PriorityQueue cannot be changed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
/** ADT priority queue: Heap-based implementation.
Listing 17-3.
@file Heap_PriorityQueue.h */
#ifndef _HEAP_PRIORITY_QUEUE
#define _HEAP_PRIORITY_QUEUE
#include "ArrayMaxHeap.h"
#include "PriorityQueueInterface.h"
template <class ItemType>
class Heap_PriorityQueue : public PriorityQueueInterface<ItemType>,
private ArrayMaxHeap<ItemType>
{
public :
Heap_PriorityQueue();
bool isEmpty() const ;
bool add(const ItemType& newEntry);
bool remove();
/** @pre The priority queue is not empty. */
ItemType peek() const throw (PrecondViolatedExcep);
}; // end Heap_PriorityQueue
#include "Heap_PriorityQueue.cpp"
#endif
// Created by Frank M. Carrano and Tim Henry.
// Copyright (c) 2013 __Pearson Education__. All rights reserved.
/** Heap-based implementation of the ADT priority queue.
Listing 17-4.
@file Heap_PriorityQueue.cpp */
#include "Heap_PriorityQueue.h"
template <class ItemType>
Heap_PriorityQueue<ItemType>::Heap_PriorityQueue()
{
ArrayMaxHeap<ItemType>();
} // end constructor
template <class ItemType>
bool Heap_PriorityQueue<ItemType>::isEmpty() const
{
return ArrayMaxHeap<ItemType>::isEmpty();
} // end isEmpty
template <class ItemType>
bool Heap_PriorityQueue<ItemType>::add(const ItemType& newEntry)
{
return ArrayMaxHeap<ItemType>::add(newEntry);
} // end add
template <class ItemType>
bool Heap_PriorityQueue<ItemType>::remove()
{
return ArrayMaxHeap<ItemType>::remove();
} // end remove
template <class ItemType>
ItemType Heap_PriorityQueue<ItemType>::peek() const throw (PrecondViolatedExcep)
{
try
{
return ArrayMaxHeap<ItemType>::peekTop();
}
catch (PrecondViolatedExcep e)
{
throw PrecondViolatedExcep("Attempted peek into an empty priority queue." );
} // end try/catch
} // end peek
May 7, 2014 at 6:34pm UTC
Here is the airworthy.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
/**
* @file Airworthy.cpp
*/
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include "Airworthy.h"
#include "Passenger.h"
#include "Heap_PriorityQueue.h"
using namespace std;
const int NOT_BLOCKED = 1, BLOCKED = 25;
Airworthy::Airworthy()
{
boardingTime = 0;
rdmBoardingTime = 0;
}
void Airworthy::readInData(ifstream& inFile)
{
string tempLastName;
string tempRow;
string strType;
char tempType;
int tempIntRow;
//Reads in the last name of the passenger
getline(inFile, tempLastName, ' ' );
myPassenger.setLastName(tempLastName);
//Reads in the passenger type and converts it to a char
getline(inFile, strType, ' ' );
tempType = strType[0];
myPassenger.setPassengerType(tempType);
//Reads in the row and converts it to an int
getline(inFile, tempRow);
tempIntRow = atoi (tempRow.c_str());
myPassenger.setRow(tempIntRow);
}//end readInData
void Airworthy::loadPriorityQueue(ifstream& inFile, ostream& outFile)
{
readInData(inFile);
while (inFile)
{
//Previous priority queue loading
preboardConditions();
previousBoarding();
outFile << "PREVIOUS CONDITIONS PRIORITY QUEUE" << endl;
outFile << "Priority Queue:" << endl
<< "Name: " << myPassenger.getLastName() << endl
<< "Type: " << myPassenger.getPassengerType() << endl
<< "Row: " << myPassenger.getRow() << endl << endl;
ppq.add(myPassenger);
//Random priority queue loading
preboardConditions();
randomBoarding();
outFile << "RANDOM CONDITIONS PRIORITY QUEUE" << endl;
outFile << "Priority Queue:" << myPassenger.getKey() << endl
<< "Name: " << myPassenger.getLastName() << endl
<< "Type: " << myPassenger.getPassengerType() << endl
<< "Row: " << myPassenger.getRow() << endl << endl;
rpq.add(myPassenger);
readInData(inFile);
}
}
void Airworthy::runSimulation(ostream& outFile)
{
int passengerOne = 0;
int rdmPassengerOne = 0;
//Previous boarding simulation
//Special consideration needed for first passenger on plane
//First passenger will never be blocked so must be handled
//outside of while loop
outFile << "PREVIOUS CONDITIONS BOARDING" << endl;
ppq.remove();
passengerOne = myPassenger.getRow();
boardingTime += NOT_BLOCKED;
outFile << "Boarding Plane:" << endl
<< "Key: " << myPassenger.getKey() << endl
<< "Name: " << myPassenger.getLastName() << endl
<< "Type: " << myPassenger.getPassengerType() << endl
<< "Row: " << myPassenger.getRow() << endl << endl;
while (!ppq.isEmpty())
{
if (passengerOne >= myPassenger.getRow())
{
cout << "value for passengerOne= " << passengerOne << endl;
boardingTime += BLOCKED;
}
else
boardingTime += NOT_BLOCKED;
passengerOne = myPassenger.getRow();
ppq.remove();
outFile << "PREVIOUS CONDITIONS BOARDING" << endl
<< "Key: " << myPassenger.getKey() << endl
<< "Name: " << myPassenger.getLastName() << endl
<< "Type: " << myPassenger.getPassengerType() << endl
<< "Row: " << myPassenger.getRow() << endl << endl;
}//end while
//Random Boarding simulation
//Special consideration needed for first passenger on plane
//First passenger will never be blocked so must be handled
//outside of while loop
outFile << "RANDOM CONDITIONS BOARDING" << endl;
rpq.remove();
rdmPassengerOne = myPassenger.getRow();
rdmBoardingTime += NOT_BLOCKED;
outFile << "Boarding Plane:" << endl
<< "Key: " << myPassenger.getKey() << endl
<< "Name: " << myPassenger.getLastName() << endl
<< "Type: " << myPassenger.getPassengerType() << endl
<< "Row: " << myPassenger.getRow() << endl << endl;
while (!rpq.isEmpty())
{
if (rdmPassengerOne >= myPassenger.getRow())
rdmBoardingTime += BLOCKED;
else
rdmBoardingTime += NOT_BLOCKED;
rdmPassengerOne = myPassenger.getRow();
rpq.remove();
outFile << "RANDOM CONDITIONS BOARDING" << endl;
outFile << "Boarding Plane:" << endl
<< "Key: " << myPassenger.getKey() << endl
<< "Name: " << myPassenger.getLastName() << endl
<< "Type: " << myPassenger.getPassengerType() << endl
<< "Row: " << myPassenger.getRow() << endl << endl;
}//end while
//Calculations for the final boarding times in minutes
double finalBoardingTime = boardingTime/60;
outFile << fixed;
outFile << setprecision(2) << "Previous Boarding Time = " << finalBoardingTime << " minutes" <<endl;
double finalRdmBoardingTime = rdmBoardingTime/60;
outFile << fixed;
outFile << setprecision(2) << "Random Boarding Time = " << finalRdmBoardingTime << " minutes" <<endl;
}
void Airworthy::preboardConditions()
{
//Comparisons for special boarding conditions
if (myPassenger.getPassengerType() == 'H' )
{
myPassenger.setKey(7);
}
else if (myPassenger.getRow() >= 1 && myPassenger.getRow() <=4)
{
myPassenger.setKey(6);
}
else if (myPassenger.getPassengerType() == 'E' || myPassenger.getRow() >= 10 && myPassenger.getRow() <=11)
{
myPassenger.setKey(5);
}
}
void Airworthy::previousBoarding()
{
//Comparisons for previous general boarding conditions
if (myPassenger.getPassengerType() == 'G' && myPassenger.getRow() >= 23 && myPassenger.getRow() <= 26)
{
myPassenger.setKey(4);
}
else if (myPassenger.getPassengerType() == 'G' && myPassenger.getRow() >= 17 && myPassenger.getRow() <= 22)
{
myPassenger.setKey(3);
}
else if (myPassenger.getPassengerType() == 'G' && myPassenger.getRow() >= 12 && myPassenger.getRow() <= 16)
{
myPassenger.setKey(2);
}
else if (myPassenger.getPassengerType() == 'G' && myPassenger.getRow() >= 5 && myPassenger.getRow() <=9)
{
myPassenger.setKey(1);
}
}
void Airworthy::randomBoarding()
{
//Comparison for the random general boarding conditions
if (myPassenger.getPassengerType() == 'G' && myPassenger.getRow() >= 5 && myPassenger.getRow() <=9)
myPassenger.setKey(4);
else if (myPassenger.getPassengerType() == 'G' && myPassenger.getRow() >= 12 && myPassenger.getRow() <=26)
myPassenger.setKey(4);
}
Topic archived. No new replies allowed.