Question

Can you please write the two C++ modules below is a step by step instuctions to follow that will ...

Can you please write the two C++ modules below is a step by step instuctions to follow that will make it very easy thanks.

Create a module called pqueue.cpp that implements priority queues, with a header file calledpqueue.hthat describes what pqueue.cpp exports. The interface includes the following, and nothing else. Types ItemType and PriorityType are discussed below under the refinement plan.

  1. A type, PriorityQueue. Creating a PriorityQueue object with line

      PriorityQueue q;
    
    makes q be an initially empty priority queue.
  2. Function isEmpty(q) returns true if q is an empty priority queue. Its prototype is

      bool isEmpty(const PriorityQueue& q);
    
  3. Function insert(q, x, p) inserts item x with priority p into q. Its prototype is

      void insert(PriorityQueue& q, ItemType x, PriorityType p);
    
  4. Function remove(q, x, p) removes the item that has the smallest priority from q. Its prototype is

      void remove(PriorityQueue& q, ItemType& x, PriorityType& p);
    
    Parameters x and p are out-parameters. 'Remove' sets x to the item that was removed and sets p to the priority associated with that item.

    If q is empty, then remove(q, x, p) writes a message on the standard output explaining the problem and stops the program. Statement

      exit(1);
    
    stops the program and returns exit status 1 to the operating system. You will need to include <cstdlib> to use exit.
  5. Function printPriorityQueue(q, printItem, printPriority) prints the contents of priority queue q in a readable format for debugging. Parameters printItem and printPriority are functions. PrintPriorityQueue uses printItem(x) to print item x and uses printPriority(p) to print priority p.

Summary of the interface

A module that uses priority queues can do the following, and nothing more.

  • It can create a variable q of type PriorityQueue, as follows.

      PriorityQueue q;
    
    The priority queue is initially empty.
  • It can use the following functions.

      bool isEmpty(const PriorityQueue& q);
      void insert(PriorityQueue& q, ItemType item, PriorityType pri);
      void remove(PriorityQueue& q, ItemType& item, PriorityType& pri);
    
  • Strictly for debugging, it can use

      void printPriorityQueue(const PriorityQueue& q,
                              ItemPrinter printItem,
                              PriorityPrinter printPriority);
    

Additional Requirements

Your program must follow the design in the section "A refinement plan." It can have additional functions, but it must have at least the functions indicated in the design. Do not add extra responsibilities to the required functions.

A Refinement Plan

Development plan

1. Create a directory for assignment 6 and create files pqueue.h and pqueue.cpp.

Use the module-template for pqueue.cpp and the header-template for pqueue.h. Modify the templates with your name and the separation between tab stops, if you use tabs.

Get files Makefile, dotest and testpqueue.cpp and put them in that directory.

Add a comment to pqueue.cpp telling its purpose. It provides priority queues. Give a sketch of the interface.

2. Items and Priorities: Types ItemType and PriorityType

Your module is intended to support an application, not to be an application. You will use it in assignments 7 and 8.

It is not clear right now what types of items or priorities the application will need; assignment 7 needs different item and priority types than assignment 8.

To handle that, define two types, ItemType and PriorityType, in pqueue.h. For this assignment, use definitions

  typedef const char* ItemType;
  typedef double      PriorityType;

Later, when you want to change the definitions of ItemType and PriorityType, you should only need to change those two lines. Not a single other line should need to be touched. (You might need to add one or more #include lines to pqueue.h to make some types available.)

That means you need to write the entire implementation of priority queues using ItemType for the type of an item and PriorityType for the type of a priority. Do not assume that ItemType is const char* or that PriorityType is double. They will need to be changed for later assignments.

3. Representing Priority Queues: Types PriorityQueue and PQCell

You are required to represent the information in a priority queue using a linked list, kept in nondecreasing order by priority. That means items with smaller priorities are closer to the beginning of the linked list. You will need the following types.

  1. In pqueue.cpp, define a structure type, PQCell, that is used as a cell in a linked list. It holds an item, a priority, and a pointer to the next cell in the list.  Document type PQCell. Be sure to say that an object of type PQCell is a cell in a linked list.

    In pqueue.h, write a forward declaration of PQCell, as follows.

      struct PQCell;
    
    That allows your definition of PriorityQueue to use type PQCell*.
  2. In pqueue.h, after the forward definition of PQCell, define a structure type, PriorityQueue, that holds only a pointer to a linked list made of PQCells. This pointer must be initially set to NULL by a parameterless constructor in the definition of PriorityQueue.

    Document type PriorityQueue in pqueue.h. What is an object of type PriorityQueue? Mention that it is represented by a linked list that is in nondecreasing order by priority.

4. Testing Whether a Priority Queue Is Empty

In pqueue.cpp, document and define function isEmpty(q) with the following prototype.

  bool isEmpty(const PriorityQueue& q);
IsEmpty(q) must return true if q is empty, false if q is not empty.

Add a prototype for isEmpty pqueue.h.

5. Insertion into a Priority Queue

In pqueue.cpp, document and define function insert(q, x, p), which inserts item x with priority p into PriorityQueue object q. Add prototype

  void insert(PriorityQueue& q, ItemType x, PriorityType p);
to pqueue.h.

You will find it convenient to write a helper function

  void insertCell(PQCell*& L, ItemType x, PriorityType p)
that inserts item x with priority p into linked list L (made of PQCells). It assumes that L is in nondescending order by priority, and it inserts the new item into the correct spot so that the list is still in nondescending order by priority.

The reason for defining 'insertCell' is that it can be recursive, and that makes it easier to write. The body of 'insert' should just make a call to 'insertCell'. That is, 'insert' should have a one-line body.

Do not put a prototype for 'insertCell' into pqueue.h since 'insertCell; is not being exported; it is only to be used in pqueue.cpp.

6. Debugging: Printing the Priority Queue

In pqueue.cpp, write a function for debugging that prints the content of the priority queue in a readable format.

There is an important issue here. Remember that the definitions of types ItemTypeandPriorityTypemight be changed. You cannot assume that ItemType is const char* or that PriorityTypeis double. But then how do you know how to print the items and priorities?

A solution is to require the module that uses priority queues to say how to print items and priorities by providing two functions, one for printing an item and another for printing a priority. C++ allows you to pass a function as a parameter of a function. (Think of lending a tool to a friend. You don't use the tool, your friend does.)

Add the following type definitions to pqueue.h.

  typedef void (*ItemPrinter)(ItemType);
  typedef void (*PriorityPrinter)(PriorityType);
They define types ItemPrinter and PriorityPrinter. The function to print an item has type ItemPrinter; it takes a parameter of type ItemType and has a void return type. The function to print a priority has type PriorityPrinter. It takes a parameter of type PriorityType and has a void return type.

In pqueue.cpp, document and define a function printPriorityQueue with the following prototype.

  void printPriorityQueue(const PriorityQueue& q,
                          ItemPrinter printItem,
                          PriorityPrinter printPriority);
It must show a clear and readable representation of what is in priority queue q, including each value and its priority. In the body of printPriorityQueue, use statement
  printItem(x);
to print item x and
  printPriority(y);
to print priority y.

PrintPriorityQueue should assume that printItem and printPriority do not write any spaces or newlines.PrintPriorityQueue should print those. It is not acceptable to write out the entire priority queue on one line.

Function printPriorityQueue is only intended to be used for debugging. Put a prototype for printPriorityQueue into pqueue.h so that other modules can use it for debugging.

7. Testing insert

Add a stub for 'remove'. It does nothing, but it allows you to run the tester to test 'insert'. Here is a suitable stub.

  void remove(PriorityQueue& q, ItemType& x, PriorityType& p)
  {
     x = "nothing";
     p = 0.0;
  }

Put a prototype for remove into pqueue.h.

Now use the automated tester to test insert. Don't be surprised that errors are reported about 'remove' not working. But don't move on to the next step until 'insert' is working.

8. Removing an Item

In pqueue.cpp, replace the stub for 'remove' by a correct definition. 'Remove' must remove the item from q that has the smallest priority. (If two or more items have the same priority, remove one of them.)

Parameters x and p are out-parameters. 'Remove' must store the item that is removed into variable xand store its priority into p.

Be sure that 'remove' deletes the list cell that is removed so that you do not have a memory leak.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

pqueue.h

typedef const char* ItemType;
typedef double PriorityType;

typedef void (*ItemPrinter)(ItemType);
typedef void (*PriorityPrinter)(PriorityType);


struct PQCell{
ItemType item;
PriorityType priority;
struct PQCell *next;

};


struct PriorityQueue{
PQCell *head;
///constructor
PriorityQueue():head(NULL)
{

}

};

void remove(PriorityQueue& q, ItemType& x, PriorityType& p);

//--------------------------------------------------------------------------------------

pqueue.cpp

#include <iostream>
#include "pqueue.h"
using namespace std;
///function to check PriorityQueue is empty or NOT
bool isEmpty(const PriorityQueue& q)
{
if(q.head ==NULL)
return true;
return false;
}

///insert x with priority p in priorityQueue q
///result : priorityQueue in decreasing order of priority
void insert(PriorityQueue& q, ItemType x, PriorityType p){

///create node and initialize
struct PQCell *new_node=new PQCell();
new_node->priority=p;
new_node->item=x;

struct PQCell* current;
/* for the head end-- if head is NULL */
if (q.head== NULL || q.head->priority <= new_node->priority)
{
//insert at front
new_node->next = q.head;
q.head = new_node;
}
else
{
/* Locate the point of insertion */
current = q.head;
while (current->next!=NULL &&current->next->priority > new_node->priority)
{
current = current->next;
}
///insert
new_node->next = current->next;
current->next = new_node;
}

}

///function to print PriorityQueue

void printPriorityQueue(const PriorityQueue& q,ItemPrinter printItem,PriorityPrinter printPriority){
///create node for traversal
PQCell *current=q.head;
/// travel upto last
while(current !=NULL){
printItem(current->item);
printPriority(current->priority);

current= current->next;
}

}

///stub for testing
void remove(PriorityQueue& q, ItemType& x, PriorityType& p)
{
x = "nothing";
p = 0.0;
}

///function to delete highest priority element form PriorityQueue
PQCell* Remove(PriorityQueue& q){
///if q is empty
if(isEmpty(q)==true){
return NULL;
}
///else

///removing first element as it stores the HIGHEST PRIORITY
PQCell *cell;
cell=q.head;

///delete head from PriorityQueue
q.head=q.head->next;

return cell;
}

///int main is for testing only

int main()
{
PriorityQueue pq;
cout<<"\n"<<isEmpty(pq);
insert(pq,"a",1);
insert(pq,"b",2);
insert(pq,"c",2.3);
insert(pq,"d",0.5);
insert(pq,"e",1.8);
cout<<"\n"<<isEmpty(pq);
// printPriorityQueue(pq);

cout<<"\nItems in PQ are -> \t";
PQCell* c;
c=Remove(pq);
cout<<c->item;
c=Remove(pq);
cout<<c->item;
c=Remove(pq);
cout<<c->item;
c=Remove(pq);
cout<<c->item;
c=Remove(pq);
cout<<c->item;


return 0;
}

OUTPUT


Add a comment
Know the answer?
Add Answer to:
Can you please write the two C++ modules below is a step by step instuctions to follow that will ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can you please write the two C++ modules below is a step by step instuctions to...

    Can you please write the two C++ modules below is a step by step instuctions to follow that will make it very easy thanks. Create a module called pqueue.cpp that implements priority queues, with a header file calledpqueue.hthat describes what pqueue.cpp exports. The interface includes the following, and nothing else. Types ItemType and PriorityType are discussed below under the refinement plan. A type, PriorityQueue. Creating a PriorityQueue object with line PriorityQueue q; makes q be an initially empty priority queue....

  • A priority queue is a collection of items each having a priority. A priority queue supports three...

    A priority queue is a collection of items each having a priority. A priority queue supports three fundamental operations. You can ask a priority queue whether it is empty. You can insert an item into the priority queue with a given priority. You can remove the item from the priority queue that has the smallest priority. For example, suppose that you start with an empty priority queue and imagine performing the following steps. Insert item "one" with priority 10. Insert...

  • I want the full code for this part and Its needs to be done in C++...

    I want the full code for this part and Its needs to be done in C++ Use a linked list to implement the following skeleton of an unsorted list typedef int ItemType: struct NodeType ItemType item; NodeType "next; class List List(); // default constructor List( const List &x); I copy constructor: deep copy is required List & operator = (const List &x); // assignment operator: deep copy. 1s required bool IsThere(ItemType x); identify if x is in the list void...

  • c++ please Given the following skeleton of an unsorted list class that uses an unsorted linked...

    c++ please Given the following skeleton of an unsorted list class that uses an unsorted linked list: template<class ItemType> struct NodeType {                 ItemType item;                 NodeType* next; }; template<class ItemType> class UList { public:                 UList(); // default constrctor                 UList(const UList &x); // we implement copy constructor with deep copy                 UList& operator = (UList &x); // equal sign operator with deep copy                 bool IsThere(ItemType item) const; // return true of false to indicate if item is...

  • Can someone implement this using singly linked list. Please and thank you public interface Priori...

    Can someone implement this using singly linked list. Please and thank you public interface PriorityQueueInterface<T extends Comparable<T>> { /** * Adds a new entry to this priority queue * @param newEntry An object to be added. */ void add(T newEntry); /** Removes and returns the entry having the highest priority. * @return Either the object having the highest priority or, if the priority * queue is empty before the operation, null. */ T remove(); /** Retrieves the entry having the...

  • MUST USE C++ PLEASE READ THE QUESTION CAREFULLY ADD COMMENTS AND EXPLAIN THE CODES PLEASE. Given...

    MUST USE C++ PLEASE READ THE QUESTION CAREFULLY ADD COMMENTS AND EXPLAIN THE CODES PLEASE. Given the following skeleton of an unsorted list class that uses an unsorted linked list: template < class ItemType > struct NodeType {                 ItemType item;                 NodeType* next; }; template < class ItemType > class UList { public:                 UList(); // default constrctor                 UList(const UList &x); // we implement copy constructor with deep copy                 UList& operator = (UList &x); // equal sign...

  • I've posted 3 classes after the instruction that were given at start You will implement and...

    I've posted 3 classes after the instruction that were given at start You will implement and test a PriorityQueue class, where the items of the priority queue are stored on a linked list. The material from Ch1 ~ 8 of the textbook can help you tremendously. You can get a lot of good information about implementing this assignment from chapter 8. There are couple notes about this assignment. 1. Using structure Node with a pointer point to Node structure to...

  • In C++ I need the printRange function, and the main.cpp program. Thanks. (Binary search tree) Write...

    In C++ I need the printRange function, and the main.cpp program. Thanks. (Binary search tree) Write a function printRange that takes as input a binary search tree t and two keys, k1 and k2, which are ordered so that k1 < k2, and print all elements x in the tree such that k1 <= x <= k2. You can add this function in BinarySearchTree.h (click the link) that we used in the lecture and lab 7. public: void printRange(int k1,...

  • C++ Error. I'm having trouble with a pointer. I keep getting the error "Thread 1: EXC_BAD_ACCESS...

    C++ Error. I'm having trouble with a pointer. I keep getting the error "Thread 1: EXC_BAD_ACCESS (code=1, address=0x18)" The objective of the assignment is to revise the public method add in class template LinkedBag so that the new node is inserted at the end of the linked chain instead of at the beginning. This is the code I have: linkedBag-driver.cpp BagInterface.hpp LinkedBag.hpp Node.hpp int main) LinkedBag<string> bag; cout << "Testing array-based Set:" << endl; cout << "The initial bag is...

  • Am Specification For this assignment, you will write a multi-file C program to define, implement ...

    Must be written and C, and compile with MinGW. Thank you! am Specification For this assignment, you will write a multi-file C program to define, implement and use a dynamic linked lists. Please refer to Lab 07 for the definition of a basic linked list. In this assignment you will need to use the basic ideas of a node and of a linked list of nodes to implement a suit of functions which can be used to create and maintain...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT