Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Written in C programming language. priority_queue.h Write the implementation fil

ID: 3717999 • Letter: W

Question

Written in C programming language.

priority_queue.h

Write the implementation file, priority_ queue.c, for the interface in the given header file, priority_queue.h. Turn in your priority_queue.c file and a suitable main program, main.c, that tests the opaque object. priority_queue.h is attached as a file to this assignment but is also listed here for your convenience. Your implementation file should implement the priority queue using a heap data structure. Submissions that implement the priority queue without using a heap will not receive

Explanation / Answer

PROGRAMM CODING:

#include <functional>

#include <queue>

#include <vector>

#include <iostream>

template<typename T> void print_queue(T& q) { while(!q.empty())

{

std::cout << q.top() << " ";

q.pop();

}

std::cout << ' ';

}

int main()

{

std::priority_queue<int> q;

for(int n : {1,8,5,6,3,4,0,9,7,2})

q.push(n);

print_queue(q);

std::priority_queue<int, std::vector<int>, std::greater<int> > q2;

for(int n : {1,8,5,6,3,4,0,9,7,2})

q2.push(n);

print_queue(q2); // Using lambda to compare elements. auto cmp = [](int left, int right)

{

return (left ^ 1) < (right ^ 1);

}

std::priority_queue<int, std::vector<int>, decltype(cmp)> q3(cmp);

for(int n : {1,8,5,6,3,4,0,9,7,2})

q3.push(n);

print_queue(q3);

}

OUTPUT:

9 8 7 6 5 4 3 2 1 0

0 1 2 3 4 5 6 7 8 9

8 9 6 7 4 5 2 3 0 1