0% found this document useful (0 votes)
138 views2 pages

Implementing Priority Queue in C++

A priority queue ADT is implemented using a heap-based binary tree structure. The lab designs and implements the priority queue using templates for generic data types. Functions for enqueue, dequeue, checking empty/full status are provided. A driver program tests the priority queue by creating one with size 15, inserting 10 items, dequeuing items and printing values, and solving a problem to determine the maximum number of candies that can be eaten in K minutes given N bags with initial candy counts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
138 views2 pages

Implementing Priority Queue in C++

A priority queue ADT is implemented using a heap-based binary tree structure. The lab designs and implements the priority queue using templates for generic data types. Functions for enqueue, dequeue, checking empty/full status are provided. A driver program tests the priority queue by creating one with size 15, inserting 10 items, dequeuing items and printing values, and solving a problem to determine the maximum number of candies that can be eaten in K minutes given N bags with initial candy counts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CSE225L – Data Structures and Algorithms Lab

Lab 14
Priority Queue
In today’s lab we will design and implement the Priority Queue ADT.

heaptype.h pqtype.h
#ifndef HEAPTYPE_H_INCLUDED #ifndef PQTYPE_H_INCLUDED
#define HEAPTYPE_H_INCLUDED #define PQTYPE_H_INCLUDED
template<class ItemType> #include "heaptype.h"
struct HeapType #include "[Link]"
{ class FullPQ
void ReheapDown(int root, int bottom); {};
void ReheapUp(int root, int bottom); class EmptyPQ
ItemType* elements; {};
int numElements; template<class ItemType>
}; class PQType
#endif // HEAPTYPE_H_INCLUDED {
[Link] public:
#include "heaptype.h" PQType(int);
template<class ItemType> ~PQType();
void Swap(ItemType& one, ItemType& two) void MakeEmpty();
{ bool IsEmpty();
ItemType temp; bool IsFull();
temp = one; void Enqueue(ItemType);
one = two; void Dequeue(ItemType&);
two = temp; private:
} int length;
template<class ItemType> HeapType<ItemType> items;
void HeapType<ItemType>::ReheapDown(int root, int bottom) int maxItems;
{ };
int maxChild; #endif // PQTYPE_H_INCLUDED
int rightChild; [Link]
int leftChild; #include "pqtype.h"
template<class ItemType>
leftChild = root*2+1; PQType<ItemType>::PQType(int max)
rightChild = root*2+2; {
if (leftChild <= bottom) maxItems = max;
{ [Link]=new ItemType[max];
if (leftChild == bottom) length = 0;
maxChild = leftChild; }
else template<class ItemType>
{ PQType<ItemType>::~PQType()
if(elements[leftChild]<=elements[rightChild]) {
maxChild = rightChild; delete [] [Link];
else }
maxChild = leftChild; template<class ItemType>
} void PQType<ItemType>::MakeEmpty()
if (elements[root] < elements[maxChild]) {
{ length = 0;
Swap(elements[root], elements[maxChild]); }
ReheapDown(maxChild, bottom); template<class ItemType>
} bool PQType<ItemType>::IsEmpty()
} {
} return length == 0;
template<class ItemType> }
void HeapType<ItemType>::ReheapUp(int root, int bottom) template<class ItemType>
{ bool PQType<ItemType>::IsFull()
int parent; {
if (bottom > root) return length == maxItems;
{ }
parent = (bottom-1) / 2;
if (elements[parent] < elements[bottom])
{
Swap(elements[parent], elements[bottom]);
ReheapUp(root, parent);
}
}
}
template<class ItemType> template<class ItemType>
void PQType<ItemType>::Enqueue(ItemType newItem) void PQType<ItemType>::Dequeue(ItemType& item)
{ {
if (length == maxItems) if (length == 0)
throw FullPQ(); throw EmptyPQ();
else else
{ {
length++; item = [Link][0];
[Link][length-1] = newItem; [Link][0] =
[Link](0, length-1); [Link][length-1];
} length--;
} [Link](0, length-1);
}
}

Now generate the Driver file ([Link]) where you perform the following tasks:

Operation to Be Tested and Description of Action Input Values Expected Output


• Create a PQType object with size 15
• Print if the queue is empty or not Queue is empty

• Insert ten items, in the order they appear 4 9 2 7 3 11 17 0 5 1
• Print if the queue is empty or not Queue is not empty
• Dequeue one element and print the dequeued value 17
• Dequeue one element and print the dequeued value 11
• You have N magical bags of candies in front of you. The 5 3 2 1 7 4 2 14
ith bag has Ai candies in it. It takes you one minute to
finish a bag of candies, no matter how many candies in
it. Every time you finish a bag with X candies in it, the
bag is magically replenished with X/2 (rounded down to
the nearest integer) more candies. Write a program
that determines the maximum number of candies you
can eat in K minutes.

The input is a sequence of integers. The first integer N


is the number of bags. The next integer K is the number
of minutes you have. The next N integers is the number
of candies in the bags. The output of your program is a
single integer which represents the maximum number
of candies you can eat.

You might also like