C++ project Priority Queue

0 comments

Your objective for this project is to implement from scratch a Priority Queue Abstract Data Structure using a Linked Chain as the underlying mechanism. In order to successfully complete this project, you must understand the concept of the regular Queue ADT. A Priorty Queue behaves exactly like a regular Queue, except that each element, when enqued, immediately advances past all elements with lower priority in front of it. The automatic effect of this is that elements will always dequeue in order of their priority, rather than the order in which they were enqueued. The order in which they were enqueued will only matter for elements with the same priority.

Admitedly, a Linked Chain implementation of the Priority Queue ADT will result in O(n) enqueque performance.

Task

The public interface for the Priority Queue will be essentially the same as the regular Queue. In fact, the implementation of all functions but one will be the same as well. The only function that you will need to change is the void enqueue(const ItemType& new_entry) function. Again, you must write this function from scratch.

#ifndef PRIORITYQUEUE_H_
#define PRIORITYQUEUE_H_
template<typename ItemType>
class PriorityQueue
{
public:
  PriorityQueue();
  PriorityQueue(const PriorityQueue<ItemType>& a_priority_queue);
  ~PriorityQueue();
  void enqueue(const ItemType& new_entry, int priority); //adds by priority
  void dequeue(); // removes element from front of the queue
  ItemType front() const; // returns a copy of the front element
  PriorityNode<ItemType>* getFrontPtr() const; //returns front_ pointer
  int size() const; // returns the number of elements in the queue
  bool isEmpty() const; // returns true if no elements in the queue
private:
  PriorityNode<ItemType>* back_; // Pointer to back of the queue
  PriorityNode<ItemType>* front_; // Pointer to front of the queue
  int item_count;
}; //end PriorityQueue
#include "PriorityQueue.cpp"
#endif // PRIORITYQUEUE_H_ 

You will also need to use a new Node<ItemType> class, which will now have three data members:Node<ItemType>* next_, ItemType item_, and int priority_.

#ifndef PRIORITY_NODE_H_ 
#define PRIORITY_NODE_H_
template<typename ItemType> class PriorityNode
{
public:
  PriorityNode();
  PriorityNode(const ItemType& an_item);
  PriorityNode(const ItemType& an_item, int priority);
  PriorityNode(const ItemType& an_item, int priority, 
               PriorityNode<ItemType>* next_node_ptr);
  void setItem(const ItemType& an_item);
  void setPriority(const int priority);
  void setNext(PriorityNode<ItemType>* next_node_ptr);
  ItemType getItem() const;
  int getPriority() const;
  PriorityNode<ItemType>* getNext() const;
private:
  ItemType item_; // A data item
  int priority_; // The item's priority
  PriorityNode<ItemType>* next_; // Pointer to next node
}; // end PriorityNode
#include "PriorityNode.cpp"
#endif // PRIORITYNODE_H_

You will submit the following files:
PriorityNode.cpp
PriorityQueue.cpp

About the Author

Follow me


{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}