Priority Queue max functions?

Hello, today i got stuck on creating 2 functions that could find a max and extract the max from a priority queue. I have done pretty much everything except these 2 examples. If anyone could offer any advice or help id love that.

Here is my code so far...

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
#include <iostream>    
using namespace std;

class priorityQueue
{
private:
    int front; 
    int back;
    int size;
    int *data;

public:
    priorityQueue();
    ~priorityQueue();
    static const int CAPACITY = 50;
    int getParent(int index);
    int getLeftChild(int index);
    int getRightChild(int index);
    void swap(int &, int &);
    void insert(int item); //heap_insert
    void printArray(int [], int size);
    void heapify(int A[],int index, int size);
    int extractMax();
    bool empty() const;
    int max() const;
	
};

priorityQueue::priorityQueue()
{
    front = back = -1;
    size = CAPACITY;
    data = new int[size];
}

priorityQueue::~priorityQueue()
{
    delete[] data;
}

int getParent(int index)
{
	return (index - 1) / 2;
}

int getLeftChild(int index)
{
	return (index + 1) * 2 - 1;
}

int getRightChild(int index)
{
	return (index + 1) * 2;
}

void swap(int &item1, int &item2)
{
	int temp = item1;
	item1 = item2;
	item2 = temp;
}

void printArray(int A[], int size)
{
    	cout << "Print Array A:" << endl;

	int counter;

	for (counter = 0; counter < size; counter++)
	{
		cout << A[counter] << " ";
	}

	cout << endl << endl;
}

void priorityQueue::insert(int item)
{
    {

    if ((back + 1)% size == front )
    {
        return;
    }

    else if(empty())
    {
        back = front = 0;
    }

    else
    {
	   front = (front+1) % size;
    }

    data[back] = item;
    }
}

int priorityQueue::extractMax()
{

}

int priorityQueue::max() const
{
    
}

Last edited on
If this is supposed to be a true priority queue (maintaining heap property) then your insert function is wrong. Once you understand how to insert properly then you will understand how to extract max. Seek and you will find

Otherwise the most straightforward way of doing this is to
- search through the array and find the position of the max element
- replace it by the last element in the array
- decrement the size of the array
Topic archived. No new replies allowed.