Priority Queue help

I've been beating my head about this for a little while and I'm hoping someone here can help me spot my mistake. For some reason when I call the top position in the Queue peek() I'm getting null values. Any help would be greatly appreciated.

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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
using namespace std;

Passenger::Passenger() {

}

Passenger::Passenger(string &newLname, char &newType, int &newSeat, int &newKey){

    setLname(newLname);
    setType(newType);
    setSeat(newSeat);
    setKey(newKey);

}
Passenger::Passenger(string &newLname, char &newType, int &newSeat) {

    setLname(newLname);
    setType(newType);
    setSeat(newSeat);
}

Passenger::Passenger(const Passenger &obj) {

    lName = obj.lName;
    type = obj.type;
    seat = obj.seat;
    key = obj.key;
    //cout << "copied passenger object" << endl;


}

string  Passenger::getLname(){
    return lName;
}
char Passenger::getType() {
    return type;
};
int Passenger::getSeat(){
    return seat;
}
void Passenger::setLname(string name) {
    lName = name;
}
void Passenger::setType(char aType) {
    if ((aType == 'E') || (aType == 'G') || (aType == 'H')){
        type = aType;
    }
    else{
        cout << "Invalid Passenger type" << endl;
    }
}
void Passenger::setSeat(int aSeat) {
    if((aSeat > 0) && (aSeat < 27)){
        seat = aSeat;
    }
    else{
        cout << "Seat number out of range" << endl;
    }
}

void Passenger::setKey(int aKey) {
    key = aKey;
}
int Passenger::getKey() {
    return key;
}

void Passenger::display() {
    cout << key << endl;
    cout << lName << endl;
    cout << type << endl;
    cout << seat << endl;
}

bool Passenger::operator<(const Passenger &queue) {
    //cout << key <<endl;
    bool queueReturn = (key < queue.key);
    //cout << queueReturn << endl;

    return queueReturn;
}

bool Passenger::operator>(const Passenger &queue) {
    //cout << key <<endl;
    bool queueReturn = (key > queue.key);
    //cout << queueReturn << endl;

    return queueReturn;
}

Passenger Passenger::operator=(const Passenger &queue) {
    //cout << key <<endl;
    Passenger returnPassenger =  queue;
    returnPassenger.display();

    return returnPassenger;
}

using namespace std;
const int NOT_BLOCKED = 1, BLOCKED = 25;


Airworthy::Airworthy() {
    boardingTime = 0;
    rdmBoardingTime = 0;
}
void Airworthy::readInData(ifstream& inFile) {

    while (!inFile.eof()) {

        string tempLName;
        string tempSeat;
        string strType;
        char tempType;
        int tempIntRow;
        int tempKey = 0;
        int tempKey2 = 0;

        //Reads in the last name of the passenger
        getline(inFile, tempLName, ' ');

        //Reads in the passenger type and converts it to a char
        getline(inFile, strType, ' ');
        tempType = strType[0];

        //Reads in the row and converts it to an int
        getline(inFile, tempSeat);
        tempIntRow = atoi(tempSeat.c_str());
        //constructs the passenger objects
        Passenger priPassenger(tempLName, tempType, tempIntRow);
        Passenger rdmPassenger(tempLName, tempType, tempIntRow);

        //determines the key and preboarding conditions
        if (preboardConditions(priPassenger) > 0) {
            tempKey = preboardConditions(priPassenger);
        } else if (previousBoarding(priPassenger) > 0) {
            tempKey = previousBoarding(priPassenger);
        }
        priPassenger.setKey(tempKey);
        //adds passenger to the heap Queue
        heapQueue.add(priPassenger);
        //determines key and preboarding conditions
        if (preboardConditions(rdmPassenger) > 0) {
            tempKey2 = previousBoarding(rdmPassenger);
        } else if (randomBoarding(rdmPassenger) > 0) {
            tempKey2 = randomBoarding(rdmPassenger);
        }
        rdmPassenger.setKey(tempKey2);
        //adds passenger to the random heap queue
        rdmHeapQueue.add(rdmPassenger);
        heapQueue.peek().display(); //these are the lines where I get null
        rdmHeapQueue.peek().display(); 
    }
int Airworthy::preboardConditions(Passenger myPassenger)
{
    int key= 0;
    //Comparisons for special boarding conditions
    if (myPassenger.getType() == 'H')
    {
        key = 7;
    }
    else if (myPassenger.getSeat() >= 1 && myPassenger.getSeat() <=4)
    {
        key = 6;
    }
    else if (myPassenger.getType() == 'E' || myPassenger.getSeat() >= 10 && myPassenger.getSeat() <=11)
    {
        key = 5;
    }
    return key;
}
int Airworthy::previousBoarding(Passenger myPassenger)
{
    int key = 0;
    //Comparisons for previous general boarding conditions
    if (myPassenger.getType() == 'G' && myPassenger.getSeat() >= 23 && myPassenger.getSeat() <= 26)
    {
        key = 4;
    }
    else if (myPassenger.getType() == 'G' && myPassenger.getSeat() >= 17 && myPassenger.getSeat() <= 22)
    {
        key = 3;
    }
    else if (myPassenger.getType() == 'G' && myPassenger.getSeat() >= 12 && myPassenger.getSeat() <= 16)
    {
        key = 2;
    }
    else if (myPassenger.getType() == 'G' && myPassenger.getSeat() >= 5 && myPassenger.getSeat() <=9)
    {
        key = 1;
    }
    return key;
}
int Airworthy::randomBoarding(Passenger myPassenger)
{
    int key = 0;
    //Comparison for the random general boarding conditions
    if (myPassenger.getType() == 'G' && myPassenger.getSeat() > 4 && myPassenger.getSeat() < 27) {
        key = 4;
    }
    return key;

}
void Airworthy::runSimulation()
{
    int passengerOne = 0;
    int rdmPassengerOne = 0;

    passengerOne = heapQueue.peek().getSeat();

    boardingTime += NOT_BLOCKED;
    heapQueue.peek().display();
    heapQueue.remove();
    while(!heapQueue.isEmpty())
    {
        if(passengerOne >= heapQueue.peek().getSeat())
        {
            cout << "Passenger is blocked" << endl;
            cout << passengerOne << endl;
            boardingTime += BLOCKED;
            heapQueue.peek().display();
            heapQueue.remove();
        }
        else {
            boardingTime += NOT_BLOCKED;
            cout << "Passenger is not blocked" << endl;
            heapQueue.peek().display();
            heapQueue.remove();

        }
        passengerOne = heapQueue.peek().getSeat();

    }//end while
    //Calculations for the final boarding times in minutes


    cout << "RANDOM CONDITIONS BOARDING" << endl;

    rdmPassengerOne =  rdmHeapQueue.peek().getSeat();
    rdmBoardingTime += NOT_BLOCKED;
    rdmHeapQueue.peek().display();
    rdmHeapQueue.remove();
    while(!rdmHeapQueue.isEmpty())
    {
        if(rdmPassengerOne >=  rdmHeapQueue.peek().getSeat()) {
            rdmBoardingTime += BLOCKED;
            rdmHeapQueue.peek().display();
            rdmHeapQueue.remove();
        }
        else {
            rdmBoardingTime += NOT_BLOCKED;
            rdmHeapQueue.peek().display();
            rdmHeapQueue.remove();
        }

        rdmPassengerOne =  rdmHeapQueue.peek().getSeat();
    }//end while

    double finalBoardingTime = boardingTime/60;
    cout << fixed;
    cout << setprecision(2) << "Boarding Time = " << finalBoardingTime << " minutes" <<endl;
    double finalRdmBoardingTime = rdmBoardingTime/60;
    cout << fixed;
    cout << setprecision(2) << "Random Boarding Time = " << finalRdmBoardingTime << " minutes" <<endl;

}
What is the type of heapQueue? It seems you do not use std::priority_queue
I am using an array max heap queue.

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
217
218
219
220
221

#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

#include "ArrayMaxHeap.h"

#include <cmath> // for log2
#include "ArrayMaxHeap.h"
#include "PrecondViolatedExcep.h"

template<class ItemType>
int ArrayMaxHeap<ItemType>::getLeftChildIndex(const int nodeIndex) const
{
    return (2 * nodeIndex) + 1;
}  // end getLeftChildIndex

template<class ItemType>
int ArrayMaxHeap<ItemType>::getRightChildIndex(const int nodeIndex) const
{
    return (2 * nodeIndex) + 2;
}  // end getRightChildIndex

template<class ItemType>
int ArrayMaxHeap<ItemType>::getParentIndex(const int nodeIndex) const
{
    return (nodeIndex - 1) / 2;
}  // end getParentIndex

template<class ItemType>
bool ArrayMaxHeap<ItemType>::isLeaf(const int nodeIndex) const
{
    return (getLeftChildIndex(nodeIndex) >= itemCount);
}  // end isLeaf

template<class ItemType>
void ArrayMaxHeap<ItemType>::heapRebuild(const int subTreeNodeIndex)
{
    if (!isLeaf(subTreeNodeIndex))
    {
        // Find larger child
        int leftChildIndex = getLeftChildIndex(subTreeNodeIndex);   // A left child must exist
        int largerChildIndex = leftChildIndex;                      // Make assumption about larger child
        int rightChildIndex = getRightChildIndex(subTreeNodeIndex); // A right child might not exist

        // Check to see whether a right child exists
        if (rightChildIndex < itemCount)
        {
            // A right child exists; check whether it is larger
            if (items[rightChildIndex] > items[largerChildIndex])
                largerChildIndex = rightChildIndex; // Asssumption was wrong
        }  // end if

        // If root value is smaller that the value in the larger child, swap values
        if (items[subTreeNodeIndex] < items[largerChildIndex])
        {
            swap(items[largerChildIndex], items[subTreeNodeIndex]);

            // Continue with the recursion at that child
            heapRebuild(largerChildIndex);
        }  // end if
    }  // end if
}  // end heapRebuild

template<class ItemType>
void ArrayMaxHeap<ItemType>::heapCreate()
{
    for (int index = itemCount / 2; index >= 0; index--)
    {
        heapRebuild(index);
    }  // end for
}  // end heapCreate

//******************************************************************
//
// Public methods start here
//
//******************************************************************

template<class ItemType>
ArrayMaxHeap<ItemType>::ArrayMaxHeap(): itemCount(0), maxItems(DEFAULT_CAPACITY)
{
    items = new ItemType[DEFAULT_CAPACITY];
}  // end default constructor

template<class ItemType>
ArrayMaxHeap<ItemType>::
ArrayMaxHeap(const ItemType someArray[], const int arraySize):
        itemCount(arraySize), maxItems(2 * arraySize)
{
    // Allocate the array
    items = new ItemType[2 * arraySize];

    // Copy given values into the array
    for (int i = 0; i < itemCount; i++)
        items[i] = someArray[i];

    // Reorganize the array into a heap
    heapCreate();
} // end constructor

template<class ItemType>
ArrayMaxHeap<ItemType>::~ArrayMaxHeap()
{
    delete[] items;
}  // end destructor

template<class ItemType>
bool ArrayMaxHeap<ItemType>::isEmpty() const
{
    return itemCount == 0;
}  // end isEmpty

template<class ItemType>
int ArrayMaxHeap<ItemType>::getHeight() const
{
    return ceil(log2(itemCount + 1));
}  // end getHeight

template<class ItemType>
int ArrayMaxHeap<ItemType>::getNumberOfNodes() const
{
    return itemCount;
}  // end getNumberOfNodes

template<class ItemType>
void ArrayMaxHeap<ItemType>::clear()
{
    itemCount = 0;
}  // end clear

template<class ItemType>
ItemType ArrayMaxHeap<ItemType>::peekTop() const throw(PrecondViolatedExcep)
{
    if (isEmpty())
        throw PrecondViolatedExcep("Attempted peek into an empty heap.");

    return items[0];
} // end peekTop

template<class ItemType>
bool ArrayMaxHeap<ItemType>::add(const ItemType& newData)
{
    bool isSuccessful = false;
    if (itemCount < maxItems)
    {
        items[itemCount] = newData;

        bool inPlace = false;
        int newDataIndex = itemCount;
        while ((newDataIndex > 0) && !inPlace)
        {
            int parentIndex = getParentIndex(newDataIndex);
            if (items[newDataIndex] < items[parentIndex])
            {
                inPlace = true;
            }
            else
            {
                swap(items[newDataIndex], items[parentIndex]);
                newDataIndex = parentIndex;
            }  // end if
        }  // end while

        itemCount++;
        isSuccessful = true;
    }  // end if

    return isSuccessful;
}  // end add

template<class ItemType>
bool ArrayMaxHeap<ItemType>::remove()
{
    bool isSuccessful = false;
    if (!isEmpty())
    {
        items[ROOT_INDEX] = items[itemCount - 1];
        itemCount--;
        heapRebuild(ROOT_INDEX);
        isSuccessful = true;
    }  // end if

    return isSuccessful;
}  // end remove
I figured it out, I had some issues in my overloaded operator changed to this
1
2
3
4
5
6
7
8
Passenger Passenger::operator=(const Passenger &queue) {
    this ->lName = queue.lName;
    this -> type = queue.type;
    this -> seat = queue.seat;
    this ->key = queue.key;
    return *this;
}
Topic archived. No new replies allowed.