Assigned: | Wednesday, April 1 |
Due: | Sunday, April 12, 11:59pm |
Points: | 400 |
This assignment has you write a module, not an application, as you did for assignment 4. It gives you experience implementing an abstract data type using linked lists.
Initially, this might look like a difficult assignment. It isn't. Keep it simple. Read the entire assignment before you start to work on it.
Follow the design. Do not rename the functions or reorder the parameters.
You will need to define two structure types and five functions.
You will need to submit two files, pqueue.cpp and pqueue.h.
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.
You should find that x is "two" and y is "one".
For this assignment, you will not write an application, but a module that can be used as part of a larger application, as you did for assignment 4. File pqueue.cpp must contain function implementations and file pqueue.h must describe what pqueue.cpp exports.
Initially, you will assume that items in the priority queue have type const char* and priorities have type double. But it must be possible to change the item type without changing more than one line, and similarly to change the priority type without changing more than one line.
File pqueue.h must contain definitions of types ItemT (the type of an item) and PriorityT (the type of a priority) as follows.typedef const char* ItemT; typedef double PriorityT;
The remainder of pqueue.h and pqueue.cpp must not presume that ItemT is const char* or that PriorityT is double. It must use types ItemT and PriorityT.
The module interface includes the following.
PriorityQueue
PriorityQueue q;must create a new priority queue q that is initially empty.
bool isEmpty(const PriorityQueue& q)
void insert(PriorityQueue& q, ItemT i, PriorityT p)
void remove(PriorityQueue& q, ItemT& i, PriorityT& p)
If q is empty, then remove(q, i, p) must print "Attempt to remove from an empty priority queue" on the standard output and abort the program.
For debugging, you will write a function ShowPriorityQueue that shows the contents of a priority queue.
But when you try to define ShowPriorityQueue, you encounter a problem. Since pqueue.cpp does not know what the definitions of ItemT or PriorityT are, it does not know how to print an item or a priority. If ShowPriorityQueue tries to show a priority under the assumption that PriorityT is double, then it will not work when PriorityT is changed to be int.
When a function does not possess information that it needs, the usual fix is to add a parameter that tells the function the needed information.
That is what we will do here. ShowPriorityQueue asks its caller to tell it how to print an item and how to print a priority. The caller will pass two functions to ShowPriorityQueue, one to print an item and one to print a priority.
Every parameter must have a type, including parameters that are functions. Here are type definitions of types ItemPrinterT and PriorityPrinterT, where a value of type ItemPrinterT is a function that prints an item and a value of type PriorityPrinterT is a function that prints a priority.
typedef void (*ItemPrinterT)(ItemT item); typedef void (*PriorityPrinterT)(PriorityT priority);
void ShowPriorityQueue(const PriorityQueue& q, ItemPrinterT printItem, PriorityPrinterT printPriority)
Neither printItem nor printPriority should write any extra space or line ends. ShowPriorityQueue takes care of formatting the output.
Pqueue.cpp will define one additional type and one additional function.
PQCell
An object of type PQCell contains an item, a priority and a pointer to the next cell in the linked list.
void insertCell(PQCell*& L, ItemT item, PriorityT priority)
Start by creating a directory to hold assignment 6. Download files
into that directory.Create a files pqueue.h and pqueue.cpp. Copy and paste the module-template into pqueue.cpp. Edit pqueue.cpp. Add your name and the assignment number. If you will use tabs, say how far apart the tab stops are. Add line
#include "pqueue.h"to pqueue.cpp.
In pqueue.h, add a forward declaration of type PQCell.
After that, define type PriorityQueue. It must be a structure type with just one field, a pointer to a PQCell, and it must have a constructure that initializes that pointer to NULL.
Document type PriorityQueue. Do not say that a priority queue is a linked list, because it isn't. A priority queue is represented by a linked list that is in nondescending order by priority.
Add prototypes for the functions that are part of the interface to pqueue.h. Include ShowPriorityQueue so that it can be used for debugging. But do not add anything else that is not part of the interface.
Write a contract and definition of isEmpty in pqueue.cpp.
Write a contract and definition of insertCell in pqueue.cpp.
Write a contract and definition of insert in pqueue.cpp. Its body should be one line long, and you will lose points if the body of insert is more than one line long.
Write a contract and definition of remove in pqueue.cpp.
Statement
exit(1);stops the program immediately and returns exit status 1 to the operating system. You will need to include <cstdlib> to use exit.
Be sure that the contract explains all of the parameters clearly.
Write a contract and definition of ShowPriorityQueue in pqueue.cpp.
Test your module by running the automated tester.
Proofread your comments. Check spelling, grammar and correctness. Make sure that comments are written in complete sentences except in numbered lists. Make sure that each contract explains how all of the parameters affect what the function does.
When you are satisified that your program works and is well written, submit your work.
Use the following commands to compile and test your module.
Keep function definitions short and simple, with no more than 16 noncomment lines each.
As always, you must follow the coding standards for this course. Pay attention to the following.
Do not ignore compiler warnings. If you do not understand what a warning means, ask about it.
Every function is required to have a clear, concise and precise contract.
Each structure type must have documentation that describes what a structure of that type represents and what information each field holds.
A function body must not change the value of a call-by-value parameter.
Make each function do its whole job. For example, insertCell should be able to insert a new cell into any linked list that is in nondescending order by priority. Do not limit it to nonempty lists. Pay attention to this! It has been a source of lost points for students in the past.
If you need a local variable of type PQCell*, create a local variable of type PQCell*, not one of type PriorityQueue. That should be common sense, but many students have violated it in the past.
To turn in your work, log into the Linux system, change your directory for the one for assignment 6, use the following command.
~abrahamsonk/2530/bin/submit 6 pqueue.cpp pqueue.hAfter submitting, you should receive confirmation that the submission was successful. If you do not receive confirmation, assume that the submission did not work.
Command
~abrahamsonk/2530/bin/submit 6will show you what you have submitted for assignment 6.
You can do repeated submissions. New submissions will replace old ones.
Late submissions will be accepted for 24 hours after the due date. If you miss a late submission deadline by a microsecond, your work will not be accepted.
To ask a question about your program, first submit it, but use assignment name q6. For example, use command
~abrahamsonk/2530/bin/submit q6 pqueue.cpp pqueue.hAdd testpq.cpp if you have written it. Then send me an email with your question. Do not expect me to read your mind. Tell me what your questions are. I will look at the files that you have submitted as q6. If you have another question later, resubmit your new file as assignment q6.