0

My data is going in to the queue just fine, but when I go to print the data nothing happens. How do I pull the data from the priority queue heap to print? NOTE: The heap implementation cannot be altered.

/** 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



/**
 * @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);   
   }

Edited by ashley.vanhoesen.7

2
Contributors
1
Reply
10
Views
2 Years
Discussion Span
Last Post by triumphost
0

While I look over the code, I just have one comment before I post something useful.. I really hope you can change 1 thing.. DO NOT #include .cpp files. It is a bad habit and will get you in a lot of trouble down the line.

I must also ask, is this a snippet from a book? It has a copyright which is why I asked. Is it for your company or just a snippet from a book you want help on.

Looking at your code, it is very hard to tell what you are doing. What I see is:

You removing from the queue by calling ppq.remove which I assume returns a boolean according to your declaration.

What you should be doing is:

while(!queue.isEmpty())
{
    data = ppq.peek();
    //print the data here..
    ppq.remove();
}

This peek is required because you have no pop and remove returns a boolean rather than the item from the queue. peek + remove will return an item and remove from the queue.

Edited by triumphost

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.