SlideShare a Scribd company logo
1 of 50
Download to read offline
Write message.cpp and priorityq.cpp. The code in message.cpp and priorityq.cpp doesn't need
any input or output statements.
Do not modify priorityq.h, message.h, or main.cpp.
main.cpp
_____________________________________________________________________________
__
#include iostream
#include fstream
#include new
#include cstddef
#include "priorityq.h"
using namespace std;
int main(int argc, char* argv[])
{
ifstream inputs; // Input file for commands
char op; // Hold operation
int n; // Integer input from file
PriorityQ* ptr = NULL; // Will point to priority queue object
Message msg; // Assembled message
Priorities p; // Message priority
char c; // Message priority input from file
string s; // Message string from file
// Output usage message if one input file name is not provided
if (argc != 2)
{
cout << "Usage: project03  ";
return 1;
}
// Attempt to open input file -- terminate if file does not open
inputs.open(argv[1]);
if (!inputs)
{
cout << "Error - unable to open input file" << endl;
return 1;
}
// Process commands from input file
getline(inputs, s); // Input comment line
cout << s << endl; // Echo comment line
inputs >> op; // Attempt to input first command
while (inputs)
{
switch (op) // Identify and perform operation input from file
{
case '#': // Echo Comment
getline(inputs, s);
cout << "-------- " << op << s << endl;
break;
case 'c': // Constructor
cout << endl << "Constructor()";
try
{
ptr = new PriorityQ;
cout << endl;
}
catch ( std::bad_alloc )
{
cout << "Failed : Terminating now..." << endl;
return 1;
}
break;
case '+': // Enqueue
inputs >> c; // Input message priority from file
inputs.ignore(100, ' '); // Skip one space
getline(inputs,s); // Input rest of line as the message
cout << "Enqueue(" << c << " '" << s << "'" << ")";
try
{
switch (c)
{
case 'H': msg.SetPriority(HIGH); break;
case 'M': msg.SetPriority(MEDIUM); break;
case 'L': msg.SetPriority(LOW); break;
}
msg.SetMessage(s);
ptr->Enqueue(msg);
}
catch (FullPQ)
{
cout << " -- Failed Full PriorityQ";
}
cout << endl;
break;
case '-': // Dequeue
cout << "Dequeue() -- ";
try
{
ptr->Dequeue();
cout << "Successful";
}
catch (EmptyPQ)
{
cout << "Failed Empty PriorityQ";
}
cout << endl;
break;
case 'x': // Purge
inputs >> c; // Input message priority from file
cout << "Purge(" << c << ")";
try
{
switch (c)
{
case 'H': ptr->Purge(HIGH); break;
case 'M': ptr->Purge(MEDIUM); break;
case 'L': ptr->Purge(LOW); break;
}
}
catch (EmptyPQ)
{
cout << " -- Failed Empty PriorityQ";
}
cout << endl;
break;
case '?': // Peek
inputs >> n;
cout << "Peek(" << n << ") -- ";
try
{
ptr->Peek(n).Print();
}
catch (InvalidPeekPQ)
{
cout << "Failed Invalid Peek PriorityQ";
}
cout << endl;
break;
case 'f': // Front
cout << "Front() -- ";
try
{
ptr->Front().Print();
}
catch (EmptyPQ)
{
cout << "Failed Empty PriorityQ";
}
cout << endl;
break;
case 'r': // Rear
cout << "Rear() -- ";
try
{
ptr->Rear().Print();
}
catch (EmptyPQ)
{
cout << "Failed Empty PriorityQ";
}
cout << endl;
break;
case 'p': // Print PriorityQ
cout << "Print() -- ";
ptr->Print();
cout << endl;
break;
case 's': // Size of PriorityQ
cout << "Size() -- " << ptr->Size() << endl;
break;
case 'm': // Make PriorityQ Empty but ready for use
cout << "MakeEmpty()" << endl;
ptr->MakeEmpty();
break;
case 'd': // Destructor
delete ptr;
ptr = NULL;
cout << "Destructor()" << endl << endl;
break;
default: // Error
cout << "Error - unrecognized operation '" << op << "'" << endl;
cout << "Terminating now..." << endl;
return 1;
break;
}
inputs >> op; // Attempt to input next command
}
return 0;
} // End main()
_____________________________________________________________________________
__
priorityq.h
_____________________________________________________________________________
__
#ifndef PRIORITYQ_H
#define PRIORITYQ_H
#include iostream
#include "message.h"
using namespace std;
//
// Exception classes for PriorityQ
//
class EmptyPQ { /* No additional code */ }; // Exception class for empty PriorityQ condition
class FullPQ { /* No additional code */ }; // Exception class for full PriorityQ condition
class InvalidPeekPQ { /* No additional code */ }; // Exception class for invalid PriorityQ peek
condition
//
// Priority Queue Node Structure
//
struct Node // Linked priority queue node structure
{
Message data; // Field for storing data in the priority queue node
Node* nextPtr; // Points to next priority queue node
Node* previousPtr; // Points to previous priority queue node
};
//
// PriorityQ class declaration
//
class PriorityQ // Double linked queue of messages sorted by priority
{
private:
Node* frontPtr; // Points to front node of priority queue
Node* rearPtr; // Points to rear node of priority queue
int count; // Number of values stored in priority queue
public:
/********** Start of functions you must implement for PriorityQ **************/
// Implement the following nine public functions in the file named priorityq.cpp
PriorityQ();
// PriorityQ()
// Initializes all private variables to indicate an empty priority queue
~PriorityQ();
//~PriorityQ()
// Deallocates all priority queue nodes
// No memory leak allowed
void MakeEmpty();
// MakeEmpty()
// Deallocates all priority queue nodes and returns priority queue to empty ready-to-use state
// No memory leak allowed
void Enqueue(Message msg);
// Enqueue()
// Adds value to priority queue in correct position and increments count.
// Duplicates are allowed.
// Highest priority messages must always be at front of queue
// Lowest priority messages must always be at rear of queue
// Add AFTER messages of similar priority
// If queue is already full, throws FullPQ exception.
void Dequeue();
// Dequeue()
// Removes highest priority message from front of priority queue and decrements count.
// If queue is empty, throws EmptyPQ exception
// No memory leak allowed
void Purge(Priorities p);
// Purge()
// Removes all messages of priority p from queue leaving all other messages in priority queue
// If queue is empty, throws EmptyPQ exception
// No memory leak allowed
Message Front() const;
// Front()
// Returns message at front of priority queue
// If queue is empty, throws EmptyPQ
Message Rear() const;
// Rear()
// Returns message at rear of priority queue
// If queue is empty, throws EmptyPQ
Message Peek(int n) const;
// Peek()
// Returns message n positions from front of priority queue
// If position n does not exist, throws InvalidPeekPQ
bool IsFull() const;
// IsFull()
// Returns true if queue is full. Returns false otherwise. DOES NOT MODIFY THE
PRIORITY QUEUE
bool IsEmpty() const;
// IsEmpty()
// Returns true if queue is empty. Returns false otherwise. DOES NOT MODIFY THE
PRIORITY QUEUE
int Size() const;
// Size()
// Returns number of items stored in priority queue. DOES NOT MODIFY THE PRIORITY
QUEUE
/*********** End of functions you must implement for PriorityQ ***************/
void Print() const
// Print() -- DO NOT MODIFY OR RELOCATE THIS FUNCTION
// Prints contents of priority queue without modifying its contents
{
Node* tempPtr = frontPtr;
// Prints queue nodes Front-to-Rear order
cout << "Front { ";
while (tempPtr != NULL)
{
//cout << '(' << tempPtr->data.GetMessage() << ')' << ' ';
tempPtr->data.Print();
cout << ' ';
tempPtr = tempPtr->nextPtr;
}
cout << "} Rear Rear { ";
// Prints queue nodes Rear-to-Front order
tempPtr = rearPtr;
while (tempPtr != NULL)
{
//cout << '(' << tempPtr->data.GetMessage() << ')' << ' ';
tempPtr->data.Print();
cout << ' ';
tempPtr = tempPtr->previousPtr;
}
cout << "} Front";
} // End Print()
};
#endif
_____________________________________________________________________________
__
message.h
_____________________________________________________________________________
__
#ifndef MESSAGE_H
#define MESSAGE_H
#include iostream
using namespace std;
//
// Define enumerated Priorities type
//
enum Priorities {UNKNOWN, LOW, MEDIUM, HIGH};
//
// Message class declaration
//
class Message // Models a single message with priority
{
private:
Priorities priority; // Stores message priority
string message; // Stores message contents
public:
/********** Start of functions you must implement for Message **************/
// Implement the following five public functions in the file named message.cpp
Message(); // Initializes message to empty string with UNKNOWN priority
void SetPriority(Priorities p); // Sets priority equal to p
void SetMessage(string msg); // Sets message equal to msg
Priorities GetPriority() const; // Returns priority value without modification
string GetMessage() const; // Returns message contents without modification
/*********** End of functions you must implement for Message ***************/
void Print() const // DO NOT MODIFY OR RELOCATE THIS FUNCTION
{
cout << "[";
if (priority == HIGH)
cout << "H";
else if (priority == MEDIUM)
cout << "M";
else if (priority == LOW)
cout << "L";
else
cout << "U";
cout << ", " << message << "]";
} // End Print() const
};
#endif
_____________________________________________________________________________
__
_____________________________________________________________________________
__
# p04input1.txt -- Test: PriorityQ(), Enqueue(), Dequeue(), ~PriorityQ()
# Test adding messages H to L (add at rear)
c
+ H Cat Food
+ M Goat Chow
+ L Dog Walk
p
d
# Test adding messages L to H (add at front)
c
+ L Dog Walk
+ M Goat Chow
+ H Cat Food
p
d
# Test adding messages (add in between)
c
+ H Cat Food
+ L Dog Walk
+ M Goat Chow
p
d
# Test adding messages with same priorities
c
+ H Cat Food
+ H Dog Kibble
+ H Fish Food
+ H Cat Food
p
d
# Test adding messages (arbitrary)
c
+ H Cat Food
+ L Dog Walk
+ M Goat Chow
+ H Horse Oats
+ L Rabbit Food
+ M Zebra Stripes
+ H Bird Seed
+ L Dog Walk
p
d
# Test Dequeue normal operation
c
+ H Cat Food
+ L Dog Walk
+ M Goat Chow
p
-
p
-
p
-
p
+ H Cat Food
+ L Dog Walk
+ M Goat Chow
+ H Horse Oats
+ L Rabbit Food
+ M Zebra Stripes
+ H Bird Seed
p
-
p
-
p
-
p
-
p
-
p
-
p
-
p
d
# Test Dequeue error handling
c
-
+ H Cat Food
+ L Dog Walk
+ M Goat Chow
+ H Horse Oats
+ L Rabbit Food
+ M Zebra Stripes
+ H Bird Seed
p
-
p
-
p
-
p
-
p
-
p
-
p
-
p
-
p
d
_____________________________________________________________________________
__
_____________________________________________________________________________
__
# p04input2.txt -- Test: Size(), Front(), Rear(), MakeEmpty()
# Test Size()
c
s
+ H Cat Food
s
+ L Dog Walk
s
+ M Goat Chow
p
s
-
p
s
-
p
s
-
p
s
-
p
s
d
# Test Front() and Rear()
c
f
r
+ H Cat Food
f
r
+ L Dog Walk
f
r
+ M Goat Chow
f
r
+ H Horse Oats
f
r
+ L Rabbit Food
f
r
+ M Zebra Stripes
f
r
+ H Bird Seed
f
r
p
-
f
r
p
-
f
r
p
-
f
r
p
d
# Test MakeEmpty normal operation
c
+ H Cat Food
+ L Dog Walk
+ M Goat Chow
+ H Horse Oats
+ L Rabbit Food
+ M Zebra Stripes
+ H Bird Seed
p
m
p
+ H Cat Food
+ L Dog Walk
+ M Goat Chow
p
m
p
d
# Test MakeEmpty error handling
c
p
m
p
d
_____________________________________________________________________________
__
_____________________________________________________________________________
__
# p04input3.txt -- Test: Peek(), Purge()
# Test Peek() normal operation
c
+ H Cat Food
+ L Dog Walk
+ M Goat Chow
p
? 0
? 1
? 2
p
-
? 0
? 1
p
-
? 0
? 1
? 2
? 3
? 5
p
-
? 0
? 1
? 2
? 3
? 5
d
# Test Peek() error handling
c
? 0
? 1
+ H Cat Food
+ L Dog Walk
+ M Goat Chow
p
? 1
? 3
? 5
-
p
? 1
? 2
? 3
? 5
p
d
# Test Purge(HIGH) normal operation
c
p
+ H Cat Food
+ L Dog Walk
+ M Goat Chow
+ H Horse Oats
+ L Rabbit Food
+ M Zebra Stripes
+ H Bird Seed
p
s
x H
p
s
d
# Test Purge(MEDIUM) normal operation
c
p
+ H Cat Food
+ L Dog Walk
+ M Goat Chow
+ H Horse Oats
+ L Rabbit Food
+ M Zebra Stripes
+ H Bird Seed
p
s
x M
p
s
d
# Test Purge(LOW) normal operation
c
p
+ H Cat Food
+ L Dog Walk
+ M Goat Chow
+ H Horse Oats
+ L Rabbit Food
+ M Zebra Stripes
+ H Bird Seed
p
s
x L
p
s
d
# Test Purge() normal operation
c
p
+ H Cat Food
+ L Dog Walk
+ M Goat Chow
+ H Horse Oats
+ L Rabbit Food
+ M Zebra Stripes
+ H Bird Seed
p
s
x H
p
s
x M
p
s
x L
p
s
+ H Cat Food
+ L Dog Walk
+ M Goat Chow
+ H Horse Oats
+ L Rabbit Food
+ M Zebra Stripes
+ H Bird Seed
p
x L
p
x M
p
x H
p
d
# Test Purge() error handling
c
p
x L
p
s
x M
p
s
x H
p
s
d
_____________________________________________________________________________
__ Complete the provided partial C++ program that will implement a Priority Queue ADT
(abstract data type) in which the internal representation of the priority queue is a double linked
series of dynamically allocated nodes. The Priority Queue will be used to store text messages
read from an input file. Each message has a priority (H for High, M for Medium, L for Low).
When input from the file by the main function, the message and priority information are stored
within an object of the type Message. The text message is stored as a string attribute, and the
message priority is represented by an attribute with type Priorities, an enumerated type provided.
Priority Queue Rules of operation: (1) If the Priority Queue contains messages which have
different priority levels, then the highest priority messages must be removed from the FRONT of
the Priority Queue before any lower priority messages are removed whenever the Dequeue 0
method is invoked HINT: The priority queue is sorted by Priority Level (highest to lowest) as it
is being constructed by Enqueue in much the same way as we discussed with the implementation
of a sorted linked list. (2) Given two messages of the same priority level, the message that has
been in the Priority Queue for the longest time has the highest priority. (FIFo operation within a
priority level)
Solution
Given below are the complete set of files for the question. The output tested with the 3 input
file's is shown... Please don;t forget to rate the answer if it helped. Thank you.
message.h
#ifndef MESSAGE_H
#define MESSAGE_H
#include
using namespace std;
//
// Define enumerated Priorities type
//
enum Priorities {UNKNOWN, LOW, MEDIUM, HIGH};
//
// Message class declaration
//
class Message // Models a single message with priority
{
private:
Priorities priority; // Stores message priority
string message; // Stores message contents
public:
/********** Start of functions you must implement for Message **************/
// Implement the following five public functions in the file named message.cpp
Message(); // Initializes message to empty string with UNKNOWN priority
void SetPriority(Priorities p); // Sets priority equal to p
void SetMessage(string msg); // Sets message equal to msg
Priorities GetPriority() const; // Returns priority value without modification
string GetMessage() const; // Returns message contents without modification
/*********** End of functions you must implement for Message ***************/
void Print() const // DO NOT MODIFY OR RELOCATE THIS FUNCTION
{
cout << "[";
if (priority == HIGH)
cout << "H";
else if (priority == MEDIUM)
cout << "M";
else if (priority == LOW)
cout << "L";
else
cout << "U";
cout << ", " << message << "]";
} // End Print() const
};
#endif
message.cpp
#include "message.h"
Message::Message() // Initializes message to empty string with UNKNOWN priority
{
message = "";
priority = UNKNOWN;
}
void Message::SetPriority(Priorities p) // Sets priority equal to p
{
priority = p;
}
void Message::SetMessage(string msg) // Sets message equal to msg
{
message = msg;
}
Priorities Message::GetPriority() const // Returns priority value without modification
{
return priority;
}
string Message::GetMessage() const // Returns message contents without modification
{
return message;
}
priorityq.h
#ifndef PRIORITYQ_H
#define PRIORITYQ_H
#include
#include "message.h"
using namespace std;
//
// Exception classes for PriorityQ
//
class EmptyPQ { /* No additional code */ }; // Exception class for empty PriorityQ condition
class FullPQ { /* No additional code */ }; // Exception class for full PriorityQ condition
class InvalidPeekPQ { /* No additional code */ }; // Exception class for invalid PriorityQ peek
condition
//
// Priority Queue Node Structure
//
struct Node // Linked priority queue node structure
{
Message data; // Field for storing data in the priority queue node
Node* nextPtr; // Points to next priority queue node
Node* previousPtr; // Points to previous priority queue node
};
//
// PriorityQ class declaration
//
class PriorityQ // Double linked queue of messages sorted by priority
{
private:
Node* frontPtr; // Points to front node of priority queue
Node* rearPtr; // Points to rear node of priority queue
int count; // Number of values stored in priority queue
public:
/********** Start of functions you must implement for PriorityQ **************/
// Implement the following nine public functions in the file named priorityq.cpp
PriorityQ();
// PriorityQ()
// Initializes all private variables to indicate an empty priority queue
~PriorityQ();
//~PriorityQ()
// Deallocates all priority queue nodes
// No memory leak allowed
void MakeEmpty();
// MakeEmpty()
// Deallocates all priority queue nodes and returns priority queue to empty ready-to-use state
// No memory leak allowed
void Enqueue(Message msg);
// Enqueue()
// Adds value to priority queue in correct position and increments count.
// Duplicates are allowed.
// Highest priority messages must always be at front of queue
// Lowest priority messages must always be at rear of queue
// Add AFTER messages of similar priority
// If queue is already full, throws FullPQ exception.
void Dequeue();
// Dequeue()
// Removes highest priority message from front of priority queue and decrements count.
// If queue is empty, throws EmptyPQ exception
// No memory leak allowed
void Purge(Priorities p);
// Purge()
// Removes all messages of priority p from queue leaving all other messages in priority queue
// If queue is empty, throws EmptyPQ exception
// No memory leak allowed
Message Front() const;
// Front()
// Returns message at front of priority queue
// If queue is empty, throws EmptyPQ
Message Rear() const;
// Rear()
// Returns message at rear of priority queue
// If queue is empty, throws EmptyPQ
Message Peek(int n) const;
// Peek()
// Returns message n positions from front of priority queue
// If position n does not exist, throws InvalidPeekPQ
bool IsFull() const;
// IsFull()
// Returns true if queue is full. Returns false otherwise. DOES NOT MODIFY THE PRIORITY
QUEUE
bool IsEmpty() const;
// IsEmpty()
// Returns true if queue is empty. Returns false otherwise. DOES NOT MODIFY THE
PRIORITY QUEUE
int Size() const;
// Size()
// Returns number of items stored in priority queue. DOES NOT MODIFY THE PRIORITY
QUEUE
/*********** End of functions you must implement for PriorityQ ***************/
void Print() const
// Print() -- DO NOT MODIFY OR RELOCATE THIS FUNCTION
// Prints contents of priority queue without modifying its contents
{
Node* tempPtr = frontPtr;
// Prints queue nodes Front-to-Rear order
cout << "Front { ";
while (tempPtr != NULL)
{
//cout << '(' << tempPtr->data.GetMessage() << ')' << ' ';
tempPtr->data.Print();
cout << ' ';
tempPtr = tempPtr->nextPtr;
}
cout << "} Rear Rear { ";
// Prints queue nodes Rear-to-Front order
tempPtr = rearPtr;
while (tempPtr != NULL)
{
//cout << '(' << tempPtr->data.GetMessage() << ')' << ' ';
tempPtr->data.Print();
cout << ' ';
tempPtr = tempPtr->previousPtr;
}
cout << "} Front";
} // End Print()
};
#endif
priorityq.cpp
#include "priorityq.h"
PriorityQ::PriorityQ()
// PriorityQ()
// Initializes all private variables to indicate an empty priority queue
{
frontPtr = NULL;
rearPtr = NULL;
count = 0;
}
PriorityQ::~PriorityQ()
//~PriorityQ()
// Deallocates all priority queue nodes
// No memory leak allowed
{
MakeEmpty();
}
void PriorityQ::MakeEmpty()
// MakeEmpty()
// Deallocates all priority queue nodes and returns priority queue to empty ready-to-use state
// No memory leak allowed
{
Node *temp;
while(frontPtr != NULL)
{
temp = frontPtr->nextPtr;
delete frontPtr;
frontPtr = temp;
}
frontPtr = NULL;
rearPtr = NULL;
count = 0;
}
void PriorityQ::Enqueue(Message msg)
// Enqueue()
// Adds value to priority queue in correct position and increments count.
// Duplicates are allowed.
// Highest priority messages must always be at front of queue
// Lowest priority messages must always be at rear of queue
// Add AFTER messages of similar priority
// If queue is already full, throws FullPQ exception.
{
Node *n = new Node;
n->data = msg;
n->nextPtr = NULL;
n->previousPtr = NULL;
if(IsEmpty())
frontPtr = rearPtr = n;
else
{
Node *curr = rearPtr;
while(curr != NULL && msg.GetPriority() > curr->data.GetPriority())
{
curr = curr->previousPtr;
}
if(curr == NULL) //need to insert as first node
{
n->nextPtr = frontPtr;
frontPtr ->previousPtr = n;
frontPtr = n;
}
else
{
n->nextPtr = curr->nextPtr;
n->previousPtr = curr;
curr->nextPtr = n;
if(n->nextPtr != NULL)
n->nextPtr->previousPtr = n;
else
rearPtr = n;
}
}
count++;
}
void PriorityQ::Dequeue()
// Dequeue()
// Removes highest priority message from front of priority queue and decrements count.
// If queue is empty, throws EmptyPQ exception
// No memory leak allowed
{
if(IsEmpty())
throw EmptyPQ();
Node *next = frontPtr->nextPtr;
if(next != NULL)
next->previousPtr = NULL;
delete frontPtr;
frontPtr = next;
if(frontPtr == NULL)
rearPtr = NULL;
count--;
}
void PriorityQ::Purge(Priorities p)
// Purge()
// Removes all messages of priority p from queue leaving all other messages in priority queue
// If queue is empty, throws EmptyPQ exception
// No memory leak allowed
{
if(IsEmpty())
throw EmptyPQ();
//search from backwards the 1st message matching priority.
Node *curr = rearPtr;
while(curr != NULL)
{
if(curr->data.GetPriority() == p) //found matching
{
Node *delnode = curr;
Node *saved;
Node *next = curr->nextPtr;
//delete all nodes from the found node till we see a node which does not match
while(delnode!= NULL && delnode->data.GetPriority() == p)
{
saved = delnode->previousPtr;
delete delnode;
delnode = saved;
count --;
}
if(delnode == NULL)
frontPtr = next;
else
delnode->nextPtr = next;
if(frontPtr == NULL)
rearPtr = NULL;
if(next != NULL)
next->previousPtr = delnode;
else
rearPtr = delnode;
break;
}
else if(curr->data.GetPriority() > p)
break;
else
curr = curr->previousPtr;
}
}
Message PriorityQ::Front() const
// Front()
// Returns message at front of priority queue
// If queue is empty, throws EmptyPQ
{
if(IsEmpty())
throw EmptyPQ();
return frontPtr->data;
}
Message PriorityQ::Rear() const
// Rear()
// Returns message at rear of priority queue
// If queue is empty, throws EmptyPQ
{
if(IsEmpty())
throw EmptyPQ();
return rearPtr->data;
}
Message PriorityQ::Peek(int n) const
// Peek()
// Returns message n positions from front of priority queue
// If position n does not exist, throws InvalidPeekPQ
{
if(IsEmpty() || n < 0 || n >= count)
throw InvalidPeekPQ();
Node *curr = frontPtr;
for(int p = 0; p < n; p++)
curr = curr->nextPtr;
return curr->data;
}
bool PriorityQ::IsFull() const
// IsFull()
// Returns true if queue is full. Returns false otherwise. DOES NOT MODIFY THE PRIORITY
QUEUE
{
return false;
}
bool PriorityQ::IsEmpty() const
// IsEmpty()
// Returns true if queue is empty. Returns false otherwise. DOES NOT MODIFY THE
PRIORITY QUEUE
{
return count == 0;
}
int PriorityQ::Size() const
// Size()
// Returns number of items stored in priority queue. DOES NOT MODIFY THE PRIORITY
QUEUE
{
return count;
}
main.cpp
#include
#include
#include
#include
#include "priorityq.h"
using namespace std;
int main(int argc, char* argv[])
{
ifstream inputs; // Input file for commands
char op; // Hold operation
int n; // Integer input from file
PriorityQ* ptr = NULL; // Will point to priority queue object
Message msg; // Assembled message
Priorities p; // Message priority
char c; // Message priority input from file
string s; // Message string from file
// Output usage message if one input file name is not provided
if (argc != 2)
{
cout << "Usage: project03  ";
return 1;
}
// Attempt to open input file -- terminate if file does not open
inputs.open(argv[1]);
if (!inputs)
{
cout << "Error - unable to open input file" << endl;
return 1;
}
// Process commands from input file
getline(inputs, s); // Input comment line
cout << s << endl; // Echo comment line
inputs >> op; // Attempt to input first command
while (inputs)
{
switch (op) // Identify and perform operation input from file
{
case '#': // Echo Comment
getline(inputs, s);
cout << "-------- " << op << s << endl;
break;
case 'c': // Constructor
cout << endl << "Constructor()";
try
{
ptr = new PriorityQ;
cout << endl;
}
catch ( std::bad_alloc )
{
cout << "Failed : Terminating now..." << endl;
return 1;
}
break;
case '+': // Enqueue
inputs >> c; // Input message priority from file
inputs.ignore(100, ' '); // Skip one space
getline(inputs,s); // Input rest of line as the message
cout << "Enqueue(" << c << " '" << s << "'" << ")";
try
{
switch (c)
{
case 'H': msg.SetPriority(HIGH); break;
case 'M': msg.SetPriority(MEDIUM); break;
case 'L': msg.SetPriority(LOW); break;
}
msg.SetMessage(s);
ptr->Enqueue(msg);
}
catch (FullPQ)
{
cout << " -- Failed Full PriorityQ";
}
cout << endl;
break;
case '-': // Dequeue
cout << "Dequeue() -- ";
try
{
ptr->Dequeue();
cout << "Successful";
}
catch (EmptyPQ)
{
cout << "Failed Empty PriorityQ";
}
cout << endl;
break;
case 'x': // Purge
inputs >> c; // Input message priority from file
cout << "Purge(" << c << ")";
try
{
switch (c)
{
case 'H': ptr->Purge(HIGH); break;
case 'M': ptr->Purge(MEDIUM); break;
case 'L': ptr->Purge(LOW); break;
}
}
catch (EmptyPQ)
{
cout << " -- Failed Empty PriorityQ";
}
cout << endl;
break;
case '?': // Peek
inputs >> n;
cout << "Peek(" << n << ") -- ";
try
{
ptr->Peek(n).Print();
}
catch (InvalidPeekPQ)
{
cout << "Failed Invalid Peek PriorityQ";
}
cout << endl;
break;
case 'f': // Front
cout << "Front() -- ";
try
{
ptr->Front().Print();
}
catch (EmptyPQ)
{
cout << "Failed Empty PriorityQ";
}
cout << endl;
break;
case 'r': // Rear
cout << "Rear() -- ";
try
{
ptr->Rear().Print();
}
catch (EmptyPQ)
{
cout << "Failed Empty PriorityQ";
}
cout << endl;
break;
case 'p': // Print PriorityQ
cout << "Print() -- ";
ptr->Print();
cout << endl;
break;
case 's': // Size of PriorityQ
cout << "Size() -- " << ptr->Size() << endl;
break;
case 'm': // Make PriorityQ Empty but ready for use
cout << "MakeEmpty()" << endl;
ptr->MakeEmpty();
break;
case 'd': // Destructor
delete ptr;
ptr = NULL;
cout << "Destructor()" << endl << endl;
break;
default: // Error
cout << "Error - unrecognized operation '" << op << "'" << endl;
cout << "Terminating now..." << endl;
return 1;
break;
}
inputs >> op; // Attempt to input next command
}
return 0;
} // End main()
output
amoeba-2:priorityQ raji$ g++ message.cpp priorityq.cpp main.cpp
amoeba-2:priorityQ raji$ ./a.out input1.txt
# p04input1.txt -- Test: PriorityQ(), Enqueue(), Dequeue(), ~PriorityQ()
--------
# Test adding messages H to L (add at rear)
Constructor()
Enqueue(H 'Cat Food')
Enqueue(M 'Goat Chow')
Enqueue(L 'Dog Walk')
Print() -- Front { [H, Cat Food] [M, Goat Chow] [L, Dog Walk] } Rear
Rear { [L, Dog Walk] [M, Goat Chow] [H, Cat Food] } Front
Destructor()
--------
# Test adding messages L to H (add at front)
Constructor()
Enqueue(L 'Dog Walk')
Enqueue(M 'Goat Chow')
Enqueue(H 'Cat Food')
Print() -- Front { [H, Cat Food] [M, Goat Chow] [L, Dog Walk] } Rear
Rear { [L, Dog Walk] [M, Goat Chow] [H, Cat Food] } Front
Destructor()
--------
# Test adding messages (add in between)
Constructor()
Enqueue(H 'Cat Food')
Enqueue(L 'Dog Walk')
Enqueue(M 'Goat Chow')
Print() -- Front { [H, Cat Food] [M, Goat Chow] [L, Dog Walk] } Rear
Rear { [L, Dog Walk] [M, Goat Chow] [H, Cat Food] } Front
Destructor()
--------
# Test adding messages with same priorities
Constructor()
Enqueue(H 'Cat Food')
Enqueue(H 'Dog Kibble')
Enqueue(H 'Fish Food')
Enqueue(H 'Cat Food')
Print() -- Front { [H, Cat Food] [H, Dog Kibble] [H, Fish Food] [H, Cat Food] } Rear
Rear { [H, Cat Food] [H, Fish Food] [H, Dog Kibble] [H, Cat Food] } Front
Destructor()
--------
# Test adding messages (arbitrary)
Constructor()
Enqueue(H 'Cat Food')
Enqueue(L 'Dog Walk')
Enqueue(M 'Goat Chow')
Enqueue(H 'Horse Oats')
Enqueue(L 'Rabbit Food')
Enqueue(M 'Zebra Stripes')
Enqueue(H 'Bird Seed')
Enqueue(L 'Dog Walk')
Print() -- Front { [H, Cat Food] [H, Horse Oats] [H, Bird Seed] [M, Goat Chow] [M, Zebra
Stripes] [L, Dog Walk] [L, Rabbit Food] [L, Dog Walk] } Rear
Rear { [L, Dog Walk] [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] [M, Goat Chow] [H,
Bird Seed] [H, Horse Oats] [H, Cat Food] } Front
Destructor()
--------
# Test Dequeue normal operation
Constructor()
Enqueue(H 'Cat Food')
Enqueue(L 'Dog Walk')
Enqueue(M 'Goat Chow')
Print() -- Front { [H, Cat Food] [M, Goat Chow] [L, Dog Walk] } Rear
Rear { [L, Dog Walk] [M, Goat Chow] [H, Cat Food] } Front
Dequeue() -- Successful
Print() -- Front { [M, Goat Chow] [L, Dog Walk] } Rear
Rear { [L, Dog Walk] [M, Goat Chow] } Front
Dequeue() -- Successful
Print() -- Front { [L, Dog Walk] } Rear
Rear { [L, Dog Walk] } Front
Dequeue() -- Successful
Print() -- Front { } Rear
Rear { } Front
Enqueue(H 'Cat Food')
Enqueue(L 'Dog Walk')
Enqueue(M 'Goat Chow')
Enqueue(H 'Horse Oats')
Enqueue(L 'Rabbit Food')
Enqueue(M 'Zebra Stripes')
Enqueue(H 'Bird Seed')
Print() -- Front { [H, Cat Food] [H, Horse Oats] [H, Bird Seed] [M, Goat Chow] [M, Zebra
Stripes] [L, Dog Walk] [L, Rabbit Food] } Rear
Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] [M, Goat Chow] [H, Bird Seed] [H,
Horse Oats] [H, Cat Food] } Front
Dequeue() -- Successful
Print() -- Front { [H, Horse Oats] [H, Bird Seed] [M, Goat Chow] [M, Zebra Stripes] [L, Dog
Walk] [L, Rabbit Food] } Rear
Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] [M, Goat Chow] [H, Bird Seed] [H,
Horse Oats] } Front
Dequeue() -- Successful
Print() -- Front { [H, Bird Seed] [M, Goat Chow] [M, Zebra Stripes] [L, Dog Walk] [L, Rabbit
Food] } Rear
Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] [M, Goat Chow] [H, Bird Seed] }
Front
Dequeue() -- Successful
Print() -- Front { [M, Goat Chow] [M, Zebra Stripes] [L, Dog Walk] [L, Rabbit Food] } Rear
Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] [M, Goat Chow] } Front
Dequeue() -- Successful
Print() -- Front { [M, Zebra Stripes] [L, Dog Walk] [L, Rabbit Food] } Rear
Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] } Front
Dequeue() -- Successful
Print() -- Front { [L, Dog Walk] [L, Rabbit Food] } Rear
Rear { [L, Rabbit Food] [L, Dog Walk] } Front
Dequeue() -- Successful
Print() -- Front { [L, Rabbit Food] } Rear
Rear { [L, Rabbit Food] } Front
Dequeue() -- Successful
Print() -- Front { } Rear
Rear { } Front
Destructor()
--------
# Test Dequeue error handling
Constructor()
Dequeue() -- Failed Empty PriorityQ
Enqueue(H 'Cat Food')
Enqueue(L 'Dog Walk')
Enqueue(M 'Goat Chow')
Enqueue(H 'Horse Oats')
Enqueue(L 'Rabbit Food')
Enqueue(M 'Zebra Stripes')
Enqueue(H 'Bird Seed')
Print() -- Front { [H, Cat Food] [H, Horse Oats] [H, Bird Seed] [M, Goat Chow] [M, Zebra
Stripes] [L, Dog Walk] [L, Rabbit Food] } Rear
Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] [M, Goat Chow] [H, Bird Seed] [H,
Horse Oats] [H, Cat Food] } Front
Dequeue() -- Successful
Print() -- Front { [H, Horse Oats] [H, Bird Seed] [M, Goat Chow] [M, Zebra Stripes] [L, Dog
Walk] [L, Rabbit Food] } Rear
Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] [M, Goat Chow] [H, Bird Seed] [H,
Horse Oats] } Front
Dequeue() -- Successful
Print() -- Front { [H, Bird Seed] [M, Goat Chow] [M, Zebra Stripes] [L, Dog Walk] [L, Rabbit
Food] } Rear
Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] [M, Goat Chow] [H, Bird Seed] }
Front
Dequeue() -- Successful
Print() -- Front { [M, Goat Chow] [M, Zebra Stripes] [L, Dog Walk] [L, Rabbit Food] } Rear
Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] [M, Goat Chow] } Front
Dequeue() -- Successful
Print() -- Front { [M, Zebra Stripes] [L, Dog Walk] [L, Rabbit Food] } Rear
Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] } Front
Dequeue() -- Successful
Print() -- Front { [L, Dog Walk] [L, Rabbit Food] } Rear
Rear { [L, Rabbit Food] [L, Dog Walk] } Front
Dequeue() -- Successful
Print() -- Front { [L, Rabbit Food] } Rear
Rear { [L, Rabbit Food] } Front
Dequeue() -- Successful
Print() -- Front { } Rear
Rear { } Front
Dequeue() -- Failed Empty PriorityQ
Print() -- Front { } Rear
Rear { } Front
Destructor()
amoeba-2:priorityQ raji$ ./a.out input2.txt
# p04input2.txt -- Test: Size(), Front(), Rear(), MakeEmpty()
--------
# Test Size()
Constructor()
Size() -- 0
Enqueue(H 'Cat Food')
Size() -- 1
Enqueue(L 'Dog Walk')
Size() -- 2
Enqueue(M 'Goat Chow')
Print() -- Front { [H, Cat Food] [M, Goat Chow] [L, Dog Walk] } Rear
Rear { [L, Dog Walk] [M, Goat Chow] [H, Cat Food] } Front
Size() -- 3
Dequeue() -- Successful
Print() -- Front { [M, Goat Chow] [L, Dog Walk] } Rear
Rear { [L, Dog Walk] [M, Goat Chow] } Front
Size() -- 2
Dequeue() -- Successful
Print() -- Front { [L, Dog Walk] } Rear
Rear { [L, Dog Walk] } Front
Size() -- 1
Dequeue() -- Successful
Print() -- Front { } Rear
Rear { } Front
Size() -- 0
Dequeue() -- Failed Empty PriorityQ
Print() -- Front { } Rear
Rear { } Front
Size() -- 0
Destructor()
--------
# Test Front() and Rear()
Constructor()
Front() -- Failed Empty PriorityQ
Rear() -- Failed Empty PriorityQ
Enqueue(H 'Cat Food')
Front() -- [H, Cat Food]
Rear() -- [H, Cat Food]
Enqueue(L 'Dog Walk')
Front() -- [H, Cat Food]
Rear() -- [L, Dog Walk]
Enqueue(M 'Goat Chow')
Front() -- [H, Cat Food]
Rear() -- [L, Dog Walk]
Enqueue(H 'Horse Oats')
Front() -- [H, Cat Food]
Rear() -- [L, Dog Walk]
Enqueue(L 'Rabbit Food')
Front() -- [H, Cat Food]
Rear() -- [L, Rabbit Food]
Enqueue(M 'Zebra Stripes')
Front() -- [H, Cat Food]
Rear() -- [L, Rabbit Food]
Enqueue(H 'Bird Seed')
Front() -- [H, Cat Food]
Rear() -- [L, Rabbit Food]
Print() -- Front { [H, Cat Food] [H, Horse Oats] [H, Bird Seed] [M, Goat Chow] [M, Zebra
Stripes] [L, Dog Walk] [L, Rabbit Food] } Rear
Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] [M, Goat Chow] [H, Bird Seed] [H,
Horse Oats] [H, Cat Food] } Front
Dequeue() -- Successful
Front() -- [H, Horse Oats]
Rear() -- [L, Rabbit Food]
Print() -- Front { [H, Horse Oats] [H, Bird Seed] [M, Goat Chow] [M, Zebra Stripes] [L, Dog
Walk] [L, Rabbit Food] } Rear
Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] [M, Goat Chow] [H, Bird Seed] [H,
Horse Oats] } Front
Dequeue() -- Successful
Front() -- [H, Bird Seed]
Rear() -- [L, Rabbit Food]
Print() -- Front { [H, Bird Seed] [M, Goat Chow] [M, Zebra Stripes] [L, Dog Walk] [L, Rabbit
Food] } Rear
Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] [M, Goat Chow] [H, Bird Seed] }
Front
Dequeue() -- Successful
Front() -- [M, Goat Chow]
Rear() -- [L, Rabbit Food]
Print() -- Front { [M, Goat Chow] [M, Zebra Stripes] [L, Dog Walk] [L, Rabbit Food] } Rear
Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] [M, Goat Chow] } Front
Destructor()
--------
# Test MakeEmpty normal operation
Constructor()
Enqueue(H 'Cat Food')
Enqueue(L 'Dog Walk')
Enqueue(M 'Goat Chow')
Enqueue(H 'Horse Oats')
Enqueue(L 'Rabbit Food')
Enqueue(M 'Zebra Stripes')
Enqueue(H 'Bird Seed')
Print() -- Front { [H, Cat Food] [H, Horse Oats] [H, Bird Seed] [M, Goat Chow] [M, Zebra
Stripes] [L, Dog Walk] [L, Rabbit Food] } Rear
Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] [M, Goat Chow] [H, Bird Seed] [H,
Horse Oats] [H, Cat Food] } Front
MakeEmpty()
Print() -- Front { } Rear
Rear { } Front
Enqueue(H 'Cat Food')
Enqueue(L 'Dog Walk')
Enqueue(M 'Goat Chow')
Print() -- Front { [H, Cat Food] [M, Goat Chow] [L, Dog Walk] } Rear
Rear { [L, Dog Walk] [M, Goat Chow] [H, Cat Food] } Front
MakeEmpty()
Print() -- Front { } Rear
Rear { } Front
Destructor()
--------
# Test MakeEmpty error handling
Constructor()
Print() -- Front { } Rear
Rear { } Front
MakeEmpty()
Print() -- Front { } Rear
Rear { } Front
Destructor()
amoeba-2:priorityQ raji$ ./a.out input3.txt
# p04input3.txt -- Test: Peek(), Purge()
--------
# Test Peek() normal operation
Constructor()
Enqueue(H 'Cat Food')
Enqueue(L 'Dog Walk')
Enqueue(M 'Goat Chow')
Print() -- Front { [H, Cat Food] [M, Goat Chow] [L, Dog Walk] } Rear
Rear { [L, Dog Walk] [M, Goat Chow] [H, Cat Food] } Front
Peek(0) -- [H, Cat Food]
Peek(1) -- [M, Goat Chow]
Peek(2) -- [L, Dog Walk]
Print() -- Front { [H, Cat Food] [M, Goat Chow] [L, Dog Walk] } Rear
Rear { [L, Dog Walk] [M, Goat Chow] [H, Cat Food] } Front
Dequeue() -- Successful
Peek(0) -- [M, Goat Chow]
Peek(1) -- [L, Dog Walk]
Print() -- Front { [M, Goat Chow] [L, Dog Walk] } Rear
Rear { [L, Dog Walk] [M, Goat Chow] } Front
Dequeue() -- Successful
Peek(0) -- [L, Dog Walk]
Peek(1) -- Failed Invalid Peek PriorityQ
Peek(2) -- Failed Invalid Peek PriorityQ
Peek(3) -- Failed Invalid Peek PriorityQ
Peek(5) -- Failed Invalid Peek PriorityQ
Print() -- Front { [L, Dog Walk] } Rear
Rear { [L, Dog Walk] } Front
Dequeue() -- Successful
Peek(0) -- Failed Invalid Peek PriorityQ
Peek(1) -- Failed Invalid Peek PriorityQ
Peek(2) -- Failed Invalid Peek PriorityQ
Peek(3) -- Failed Invalid Peek PriorityQ
Peek(5) -- Failed Invalid Peek PriorityQ
Destructor()
--------
# Test Peek() error handling
Constructor()
Peek(0) -- Failed Invalid Peek PriorityQ
Peek(1) -- Failed Invalid Peek PriorityQ
Enqueue(H 'Cat Food')
Enqueue(L 'Dog Walk')
Enqueue(M 'Goat Chow')
Print() -- Front { [H, Cat Food] [M, Goat Chow] [L, Dog Walk] } Rear
Rear { [L, Dog Walk] [M, Goat Chow] [H, Cat Food] } Front
Peek(1) -- [M, Goat Chow]
Peek(3) -- Failed Invalid Peek PriorityQ
Peek(5) -- Failed Invalid Peek PriorityQ
Dequeue() -- Successful
Print() -- Front { [M, Goat Chow] [L, Dog Walk] } Rear
Rear { [L, Dog Walk] [M, Goat Chow] } Front
Peek(1) -- [L, Dog Walk]
Peek(2) -- Failed Invalid Peek PriorityQ
Peek(3) -- Failed Invalid Peek PriorityQ
Peek(5) -- Failed Invalid Peek PriorityQ
Print() -- Front { [M, Goat Chow] [L, Dog Walk] } Rear
Rear { [L, Dog Walk] [M, Goat Chow] } Front
Destructor()
--------
# Test Purge(HIGH) normal operation
Constructor()
Print() -- Front { } Rear
Rear { } Front
Enqueue(H 'Cat Food')
Enqueue(L 'Dog Walk')
Enqueue(M 'Goat Chow')
Enqueue(H 'Horse Oats')
Enqueue(L 'Rabbit Food')
Enqueue(M 'Zebra Stripes')
Enqueue(H 'Bird Seed')
Print() -- Front { [H, Cat Food] [H, Horse Oats] [H, Bird Seed] [M, Goat Chow] [M, Zebra
Stripes] [L, Dog Walk] [L, Rabbit Food] } Rear
Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] [M, Goat Chow] [H, Bird Seed] [H,
Horse Oats] [H, Cat Food] } Front
Size() -- 7
Purge(H)
Print() -- Front { [M, Goat Chow] [M, Zebra Stripes] [L, Dog Walk] [L, Rabbit Food] } Rear
Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] [M, Goat Chow] } Front
Size() -- 4
Destructor()
--------
# Test Purge(MEDIUM) normal operation
Constructor()
Print() -- Front { } Rear
Rear { } Front
Enqueue(H 'Cat Food')
Enqueue(L 'Dog Walk')
Enqueue(M 'Goat Chow')
Enqueue(H 'Horse Oats')
Enqueue(L 'Rabbit Food')
Enqueue(M 'Zebra Stripes')
Enqueue(H 'Bird Seed')
Print() -- Front { [H, Cat Food] [H, Horse Oats] [H, Bird Seed] [M, Goat Chow] [M, Zebra
Stripes] [L, Dog Walk] [L, Rabbit Food] } Rear
Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] [M, Goat Chow] [H, Bird Seed] [H,
Horse Oats] [H, Cat Food] } Front
Size() -- 7
Purge(M)
Print() -- Front { [H, Cat Food] [H, Horse Oats] [H, Bird Seed] [L, Dog Walk] [L, Rabbit Food]
} Rear
Rear { [L, Rabbit Food] [L, Dog Walk] [H, Bird Seed] [H, Horse Oats] [H, Cat Food] } Front
Size() -- 5
Destructor()
--------
# Test Purge(LOW) normal operation
Constructor()
Print() -- Front { } Rear
Rear { } Front
Enqueue(H 'Cat Food')
Enqueue(L 'Dog Walk')
Enqueue(M 'Goat Chow')
Enqueue(H 'Horse Oats')
Enqueue(L 'Rabbit Food')
Enqueue(M 'Zebra Stripes')
Enqueue(H 'Bird Seed')
Print() -- Front { [H, Cat Food] [H, Horse Oats] [H, Bird Seed] [M, Goat Chow] [M, Zebra
Stripes] [L, Dog Walk] [L, Rabbit Food] } Rear
Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] [M, Goat Chow] [H, Bird Seed] [H,
Horse Oats] [H, Cat Food] } Front
Size() -- 7
Purge(L)
Print() -- Front { [H, Cat Food] [H, Horse Oats] [H, Bird Seed] [M, Goat Chow] [M, Zebra
Stripes] } Rear
Rear { [M, Zebra Stripes] [M, Goat Chow] [H, Bird Seed] [H, Horse Oats] [H, Cat Food] }
Front
Size() -- 5
Destructor()
--------
# Test Purge() normal operation
Constructor()
Print() -- Front { } Rear
Rear { } Front
Enqueue(H 'Cat Food')
Enqueue(L 'Dog Walk')
Enqueue(M 'Goat Chow')
Enqueue(H 'Horse Oats')
Enqueue(L 'Rabbit Food')
Enqueue(M 'Zebra Stripes')
Enqueue(H 'Bird Seed')
Print() -- Front { [H, Cat Food] [H, Horse Oats] [H, Bird Seed] [M, Goat Chow] [M, Zebra
Stripes] [L, Dog Walk] [L, Rabbit Food] } Rear
Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] [M, Goat Chow] [H, Bird Seed] [H,
Horse Oats] [H, Cat Food] } Front
Size() -- 7
Purge(H)
Print() -- Front { [M, Goat Chow] [M, Zebra Stripes] [L, Dog Walk] [L, Rabbit Food] } Rear
Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] [M, Goat Chow] } Front
Size() -- 4
Purge(M)
Print() -- Front { [L, Dog Walk] [L, Rabbit Food] } Rear
Rear { [L, Rabbit Food] [L, Dog Walk] } Front
Size() -- 2
Purge(L)
Print() -- Front { } Rear
Rear { } Front
Size() -- 0
Enqueue(H 'Cat Food')
Enqueue(L 'Dog Walk')
Enqueue(M 'Goat Chow')
Enqueue(H 'Horse Oats')
Enqueue(L 'Rabbit Food')
Enqueue(M 'Zebra Stripes')
Enqueue(H 'Bird Seed')
Print() -- Front { [H, Cat Food] [H, Horse Oats] [H, Bird Seed] [M, Goat Chow] [M, Zebra
Stripes] [L, Dog Walk] [L, Rabbit Food] } Rear
Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] [M, Goat Chow] [H, Bird Seed] [H,
Horse Oats] [H, Cat Food] } Front
Purge(L)
Print() -- Front { [H, Cat Food] [H, Horse Oats] [H, Bird Seed] [M, Goat Chow] [M, Zebra
Stripes] } Rear
Rear { [M, Zebra Stripes] [M, Goat Chow] [H, Bird Seed] [H, Horse Oats] [H, Cat Food] }
Front
Purge(M)
Print() -- Front { [H, Cat Food] [H, Horse Oats] [H, Bird Seed] } Rear
Rear { [H, Bird Seed] [H, Horse Oats] [H, Cat Food] } Front
Purge(H)
Print() -- Front { } Rear
Rear { } Front
Destructor()
--------
# Test Purge() error handling
Constructor()
Print() -- Front { } Rear
Rear { } Front
Purge(L) -- Failed Empty PriorityQ
Print() -- Front { } Rear
Rear { } Front
Size() -- 0
Purge(M) -- Failed Empty PriorityQ
Print() -- Front { } Rear
Rear { } Front
Size() -- 0
Purge(H) -- Failed Empty PriorityQ
Print() -- Front { } Rear
Rear { } Front
Size() -- 0
Destructor()
amoeba-2:priorityQ raji$

More Related Content

Similar to Write message.cpp and priorityq.cpp. The code in message.cpp and pri.pdf

filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docxfilesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docxssuser454af01
 
I need help completing this C++ code with these requirements.instr.pdf
I need help completing this C++ code with these requirements.instr.pdfI need help completing this C++ code with these requirements.instr.pdf
I need help completing this C++ code with these requirements.instr.pdfeyeonsecuritysystems
 
Write a program that converts an infix expression into an equivalent.pdf
Write a program that converts an infix expression into an equivalent.pdfWrite a program that converts an infix expression into an equivalent.pdf
Write a program that converts an infix expression into an equivalent.pdfmohdjakirfb
 
need help with my computer science lab assignemnt. this assignment i.pdf
need help with my computer science lab assignemnt. this assignment i.pdfneed help with my computer science lab assignemnt. this assignment i.pdf
need help with my computer science lab assignemnt. this assignment i.pdffathimaoptical
 
c++ practical Digvajiya collage Rajnandgaon
c++ practical  Digvajiya collage Rajnandgaonc++ practical  Digvajiya collage Rajnandgaon
c++ practical Digvajiya collage Rajnandgaonyash production
 
#define ENABLE_COMMANDER#define ENABLE_REPORTER#include c.docx
#define ENABLE_COMMANDER#define ENABLE_REPORTER#include c.docx#define ENABLE_COMMANDER#define ENABLE_REPORTER#include c.docx
#define ENABLE_COMMANDER#define ENABLE_REPORTER#include c.docxkatherncarlyle
 
Write a simple program in C (which comiles in gcc a unix environment ).docx
Write a simple program in C (which comiles in gcc a unix environment ).docxWrite a simple program in C (which comiles in gcc a unix environment ).docx
Write a simple program in C (which comiles in gcc a unix environment ).docxnoreendchesterton753
 
For this project, you must complete the provided partial C++ program.pdf
For this project, you must complete the provided partial C++ program.pdfFor this project, you must complete the provided partial C++ program.pdf
For this project, you must complete the provided partial C++ program.pdffathimaoptical
 
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the bfinalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the bChereCheek752
 
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdf
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdfC++, Implement the class BinarySearchTree, as given in listing 16-4 .pdf
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdfrohit219406
 
EmptyCollectionException-java -- - Represents the situation in which.docx
EmptyCollectionException-java --  - Represents the situation in which.docxEmptyCollectionException-java --  - Represents the situation in which.docx
EmptyCollectionException-java -- - Represents the situation in which.docxBlakeSGMHemmingss
 
presentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.pptpresentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.pptSandipPradhan23
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorJussi Pohjolainen
 
proxyc CSAPP Web proxy NAME IMPORTANT Giv.pdf
  proxyc  CSAPP Web proxy   NAME    IMPORTANT Giv.pdf  proxyc  CSAPP Web proxy   NAME    IMPORTANT Giv.pdf
proxyc CSAPP Web proxy NAME IMPORTANT Giv.pdfajay1317
 
Frequency .java Word frequency counter package frequ.pdf
Frequency .java  Word frequency counter  package frequ.pdfFrequency .java  Word frequency counter  package frequ.pdf
Frequency .java Word frequency counter package frequ.pdfarshiartpalace
 
Unit 4
Unit 4Unit 4
Unit 4siddr
 
Virtual function
Virtual functionVirtual function
Virtual functionharman kaur
 
1- The design of a singly-linked list below is a picture of the functi (1).pdf
1- The design of a singly-linked list below is a picture of the functi (1).pdf1- The design of a singly-linked list below is a picture of the functi (1).pdf
1- The design of a singly-linked list below is a picture of the functi (1).pdfafgt2012
 

Similar to Write message.cpp and priorityq.cpp. The code in message.cpp and pri.pdf (20)

filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docxfilesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
 
I need help completing this C++ code with these requirements.instr.pdf
I need help completing this C++ code with these requirements.instr.pdfI need help completing this C++ code with these requirements.instr.pdf
I need help completing this C++ code with these requirements.instr.pdf
 
Write a program that converts an infix expression into an equivalent.pdf
Write a program that converts an infix expression into an equivalent.pdfWrite a program that converts an infix expression into an equivalent.pdf
Write a program that converts an infix expression into an equivalent.pdf
 
need help with my computer science lab assignemnt. this assignment i.pdf
need help with my computer science lab assignemnt. this assignment i.pdfneed help with my computer science lab assignemnt. this assignment i.pdf
need help with my computer science lab assignemnt. this assignment i.pdf
 
c++ practical Digvajiya collage Rajnandgaon
c++ practical  Digvajiya collage Rajnandgaonc++ practical  Digvajiya collage Rajnandgaon
c++ practical Digvajiya collage Rajnandgaon
 
Stack prgs
Stack prgsStack prgs
Stack prgs
 
#define ENABLE_COMMANDER#define ENABLE_REPORTER#include c.docx
#define ENABLE_COMMANDER#define ENABLE_REPORTER#include c.docx#define ENABLE_COMMANDER#define ENABLE_REPORTER#include c.docx
#define ENABLE_COMMANDER#define ENABLE_REPORTER#include c.docx
 
Write a simple program in C (which comiles in gcc a unix environment ).docx
Write a simple program in C (which comiles in gcc a unix environment ).docxWrite a simple program in C (which comiles in gcc a unix environment ).docx
Write a simple program in C (which comiles in gcc a unix environment ).docx
 
For this project, you must complete the provided partial C++ program.pdf
For this project, you must complete the provided partial C++ program.pdfFor this project, you must complete the provided partial C++ program.pdf
For this project, you must complete the provided partial C++ program.pdf
 
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the bfinalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
 
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdf
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdfC++, Implement the class BinarySearchTree, as given in listing 16-4 .pdf
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdf
 
EmptyCollectionException-java -- - Represents the situation in which.docx
EmptyCollectionException-java --  - Represents the situation in which.docxEmptyCollectionException-java --  - Represents the situation in which.docx
EmptyCollectionException-java -- - Represents the situation in which.docx
 
presentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.pptpresentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.ppt
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operator
 
proxyc CSAPP Web proxy NAME IMPORTANT Giv.pdf
  proxyc  CSAPP Web proxy   NAME    IMPORTANT Giv.pdf  proxyc  CSAPP Web proxy   NAME    IMPORTANT Giv.pdf
proxyc CSAPP Web proxy NAME IMPORTANT Giv.pdf
 
w10 (1).ppt
w10 (1).pptw10 (1).ppt
w10 (1).ppt
 
Frequency .java Word frequency counter package frequ.pdf
Frequency .java  Word frequency counter  package frequ.pdfFrequency .java  Word frequency counter  package frequ.pdf
Frequency .java Word frequency counter package frequ.pdf
 
Unit 4
Unit 4Unit 4
Unit 4
 
Virtual function
Virtual functionVirtual function
Virtual function
 
1- The design of a singly-linked list below is a picture of the functi (1).pdf
1- The design of a singly-linked list below is a picture of the functi (1).pdf1- The design of a singly-linked list below is a picture of the functi (1).pdf
1- The design of a singly-linked list below is a picture of the functi (1).pdf
 

More from eyeonsecuritysystems

For each step of DNA replication, predict the outcome if • concen.pdf
For each step of DNA replication, predict the outcome if • concen.pdfFor each step of DNA replication, predict the outcome if • concen.pdf
For each step of DNA replication, predict the outcome if • concen.pdfeyeonsecuritysystems
 
Explain why a virus cant infect every cell Explain why a viru.pdf
Explain why a virus cant infect every cell Explain why a viru.pdfExplain why a virus cant infect every cell Explain why a viru.pdf
Explain why a virus cant infect every cell Explain why a viru.pdfeyeonsecuritysystems
 
Explain advantages and disadvantages of designing an OS kernel in la.pdf
Explain advantages and disadvantages of designing an OS kernel in la.pdfExplain advantages and disadvantages of designing an OS kernel in la.pdf
Explain advantages and disadvantages of designing an OS kernel in la.pdfeyeonsecuritysystems
 
Explain three similarities and two diffrences in anaerobic and aerob.pdf
Explain three similarities and two diffrences in anaerobic and aerob.pdfExplain three similarities and two diffrences in anaerobic and aerob.pdf
Explain three similarities and two diffrences in anaerobic and aerob.pdfeyeonsecuritysystems
 
Exercise 7-8 Kickapoo Company uses an imprest petty cash system. The .pdf
Exercise 7-8 Kickapoo Company uses an imprest petty cash system. The .pdfExercise 7-8 Kickapoo Company uses an imprest petty cash system. The .pdf
Exercise 7-8 Kickapoo Company uses an imprest petty cash system. The .pdfeyeonsecuritysystems
 
Discussions Grades people Syllabus Modules Google Drive D Question 1 .pdf
Discussions Grades people Syllabus Modules Google Drive D Question 1 .pdfDiscussions Grades people Syllabus Modules Google Drive D Question 1 .pdf
Discussions Grades people Syllabus Modules Google Drive D Question 1 .pdfeyeonsecuritysystems
 
Discuss the range of interactions that occur between human beings an.pdf
Discuss the range of interactions that occur between human beings an.pdfDiscuss the range of interactions that occur between human beings an.pdf
Discuss the range of interactions that occur between human beings an.pdfeyeonsecuritysystems
 
Critique the customer contact model. What are its strengths and weak.pdf
Critique the customer contact model. What are its strengths and weak.pdfCritique the customer contact model. What are its strengths and weak.pdf
Critique the customer contact model. What are its strengths and weak.pdfeyeonsecuritysystems
 
Case Study Synlait Milk is an innovative dairy processing company th.pdf
Case Study Synlait Milk is an innovative dairy processing company th.pdfCase Study Synlait Milk is an innovative dairy processing company th.pdf
Case Study Synlait Milk is an innovative dairy processing company th.pdfeyeonsecuritysystems
 
An entity with two multivalued attributes will be mapped as how many.pdf
An entity with two multivalued attributes will be mapped as how many.pdfAn entity with two multivalued attributes will be mapped as how many.pdf
An entity with two multivalued attributes will be mapped as how many.pdfeyeonsecuritysystems
 
Assume you are the new CEO of the Delta Phi Corporation. When the Bo.pdf
Assume you are the new CEO of the Delta Phi Corporation. When the Bo.pdfAssume you are the new CEO of the Delta Phi Corporation. When the Bo.pdf
Assume you are the new CEO of the Delta Phi Corporation. When the Bo.pdfeyeonsecuritysystems
 
A manager typically spends the least amount of time Communicating Mo.pdf
A manager typically spends the least amount of time Communicating Mo.pdfA manager typically spends the least amount of time Communicating Mo.pdf
A manager typically spends the least amount of time Communicating Mo.pdfeyeonsecuritysystems
 
Will a Pigovian tax always be equal to the marginal external cost of.pdf
Will a Pigovian tax always be equal to the marginal external cost of.pdfWill a Pigovian tax always be equal to the marginal external cost of.pdf
Will a Pigovian tax always be equal to the marginal external cost of.pdfeyeonsecuritysystems
 
Would you identify UTC’s approach to supply chain managemet as ce.pdf
Would you identify UTC’s approach to supply chain managemet as ce.pdfWould you identify UTC’s approach to supply chain managemet as ce.pdf
Would you identify UTC’s approach to supply chain managemet as ce.pdfeyeonsecuritysystems
 
White collar crimes are best defined as a wide variety of nonviolent.pdf
White collar crimes are best defined as a wide variety of nonviolent.pdfWhite collar crimes are best defined as a wide variety of nonviolent.pdf
White collar crimes are best defined as a wide variety of nonviolent.pdfeyeonsecuritysystems
 
Why do we diverge from the Rational Choice Paradigm when identifying.pdf
Why do we diverge from the Rational Choice Paradigm when identifying.pdfWhy do we diverge from the Rational Choice Paradigm when identifying.pdf
Why do we diverge from the Rational Choice Paradigm when identifying.pdfeyeonsecuritysystems
 
Why is lactose much more soluble in water than in ethanolSolu.pdf
Why is lactose much more soluble in water than in ethanolSolu.pdfWhy is lactose much more soluble in water than in ethanolSolu.pdf
Why is lactose much more soluble in water than in ethanolSolu.pdfeyeonsecuritysystems
 
Which of the following is a technology that takes an Internet sig.pdf
Which of the following is a technology that takes an Internet sig.pdfWhich of the following is a technology that takes an Internet sig.pdf
Which of the following is a technology that takes an Internet sig.pdfeyeonsecuritysystems
 
What aspect of Ethernet can be a problem for security What mechanis.pdf
What aspect of Ethernet can be a problem for security What mechanis.pdfWhat aspect of Ethernet can be a problem for security What mechanis.pdf
What aspect of Ethernet can be a problem for security What mechanis.pdfeyeonsecuritysystems
 
This project has been designed for you to get experience with basic .pdf
This project has been designed for you to get experience with basic .pdfThis project has been designed for you to get experience with basic .pdf
This project has been designed for you to get experience with basic .pdfeyeonsecuritysystems
 

More from eyeonsecuritysystems (20)

For each step of DNA replication, predict the outcome if • concen.pdf
For each step of DNA replication, predict the outcome if • concen.pdfFor each step of DNA replication, predict the outcome if • concen.pdf
For each step of DNA replication, predict the outcome if • concen.pdf
 
Explain why a virus cant infect every cell Explain why a viru.pdf
Explain why a virus cant infect every cell Explain why a viru.pdfExplain why a virus cant infect every cell Explain why a viru.pdf
Explain why a virus cant infect every cell Explain why a viru.pdf
 
Explain advantages and disadvantages of designing an OS kernel in la.pdf
Explain advantages and disadvantages of designing an OS kernel in la.pdfExplain advantages and disadvantages of designing an OS kernel in la.pdf
Explain advantages and disadvantages of designing an OS kernel in la.pdf
 
Explain three similarities and two diffrences in anaerobic and aerob.pdf
Explain three similarities and two diffrences in anaerobic and aerob.pdfExplain three similarities and two diffrences in anaerobic and aerob.pdf
Explain three similarities and two diffrences in anaerobic and aerob.pdf
 
Exercise 7-8 Kickapoo Company uses an imprest petty cash system. The .pdf
Exercise 7-8 Kickapoo Company uses an imprest petty cash system. The .pdfExercise 7-8 Kickapoo Company uses an imprest petty cash system. The .pdf
Exercise 7-8 Kickapoo Company uses an imprest petty cash system. The .pdf
 
Discussions Grades people Syllabus Modules Google Drive D Question 1 .pdf
Discussions Grades people Syllabus Modules Google Drive D Question 1 .pdfDiscussions Grades people Syllabus Modules Google Drive D Question 1 .pdf
Discussions Grades people Syllabus Modules Google Drive D Question 1 .pdf
 
Discuss the range of interactions that occur between human beings an.pdf
Discuss the range of interactions that occur between human beings an.pdfDiscuss the range of interactions that occur between human beings an.pdf
Discuss the range of interactions that occur between human beings an.pdf
 
Critique the customer contact model. What are its strengths and weak.pdf
Critique the customer contact model. What are its strengths and weak.pdfCritique the customer contact model. What are its strengths and weak.pdf
Critique the customer contact model. What are its strengths and weak.pdf
 
Case Study Synlait Milk is an innovative dairy processing company th.pdf
Case Study Synlait Milk is an innovative dairy processing company th.pdfCase Study Synlait Milk is an innovative dairy processing company th.pdf
Case Study Synlait Milk is an innovative dairy processing company th.pdf
 
An entity with two multivalued attributes will be mapped as how many.pdf
An entity with two multivalued attributes will be mapped as how many.pdfAn entity with two multivalued attributes will be mapped as how many.pdf
An entity with two multivalued attributes will be mapped as how many.pdf
 
Assume you are the new CEO of the Delta Phi Corporation. When the Bo.pdf
Assume you are the new CEO of the Delta Phi Corporation. When the Bo.pdfAssume you are the new CEO of the Delta Phi Corporation. When the Bo.pdf
Assume you are the new CEO of the Delta Phi Corporation. When the Bo.pdf
 
A manager typically spends the least amount of time Communicating Mo.pdf
A manager typically spends the least amount of time Communicating Mo.pdfA manager typically spends the least amount of time Communicating Mo.pdf
A manager typically spends the least amount of time Communicating Mo.pdf
 
Will a Pigovian tax always be equal to the marginal external cost of.pdf
Will a Pigovian tax always be equal to the marginal external cost of.pdfWill a Pigovian tax always be equal to the marginal external cost of.pdf
Will a Pigovian tax always be equal to the marginal external cost of.pdf
 
Would you identify UTC’s approach to supply chain managemet as ce.pdf
Would you identify UTC’s approach to supply chain managemet as ce.pdfWould you identify UTC’s approach to supply chain managemet as ce.pdf
Would you identify UTC’s approach to supply chain managemet as ce.pdf
 
White collar crimes are best defined as a wide variety of nonviolent.pdf
White collar crimes are best defined as a wide variety of nonviolent.pdfWhite collar crimes are best defined as a wide variety of nonviolent.pdf
White collar crimes are best defined as a wide variety of nonviolent.pdf
 
Why do we diverge from the Rational Choice Paradigm when identifying.pdf
Why do we diverge from the Rational Choice Paradigm when identifying.pdfWhy do we diverge from the Rational Choice Paradigm when identifying.pdf
Why do we diverge from the Rational Choice Paradigm when identifying.pdf
 
Why is lactose much more soluble in water than in ethanolSolu.pdf
Why is lactose much more soluble in water than in ethanolSolu.pdfWhy is lactose much more soluble in water than in ethanolSolu.pdf
Why is lactose much more soluble in water than in ethanolSolu.pdf
 
Which of the following is a technology that takes an Internet sig.pdf
Which of the following is a technology that takes an Internet sig.pdfWhich of the following is a technology that takes an Internet sig.pdf
Which of the following is a technology that takes an Internet sig.pdf
 
What aspect of Ethernet can be a problem for security What mechanis.pdf
What aspect of Ethernet can be a problem for security What mechanis.pdfWhat aspect of Ethernet can be a problem for security What mechanis.pdf
What aspect of Ethernet can be a problem for security What mechanis.pdf
 
This project has been designed for you to get experience with basic .pdf
This project has been designed for you to get experience with basic .pdfThis project has been designed for you to get experience with basic .pdf
This project has been designed for you to get experience with basic .pdf
 

Recently uploaded

Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 

Recently uploaded (20)

Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 

Write message.cpp and priorityq.cpp. The code in message.cpp and pri.pdf

  • 1. Write message.cpp and priorityq.cpp. The code in message.cpp and priorityq.cpp doesn't need any input or output statements. Do not modify priorityq.h, message.h, or main.cpp. main.cpp _____________________________________________________________________________ __ #include iostream #include fstream #include new #include cstddef #include "priorityq.h" using namespace std; int main(int argc, char* argv[]) { ifstream inputs; // Input file for commands char op; // Hold operation int n; // Integer input from file PriorityQ* ptr = NULL; // Will point to priority queue object Message msg; // Assembled message Priorities p; // Message priority char c; // Message priority input from file string s; // Message string from file // Output usage message if one input file name is not provided if (argc != 2) { cout << "Usage: project03 "; return 1; } // Attempt to open input file -- terminate if file does not open inputs.open(argv[1]); if (!inputs) { cout << "Error - unable to open input file" << endl;
  • 2. return 1; } // Process commands from input file getline(inputs, s); // Input comment line cout << s << endl; // Echo comment line inputs >> op; // Attempt to input first command while (inputs) { switch (op) // Identify and perform operation input from file { case '#': // Echo Comment getline(inputs, s); cout << "-------- " << op << s << endl; break; case 'c': // Constructor cout << endl << "Constructor()"; try { ptr = new PriorityQ; cout << endl; } catch ( std::bad_alloc ) { cout << "Failed : Terminating now..." << endl; return 1; } break; case '+': // Enqueue inputs >> c; // Input message priority from file inputs.ignore(100, ' '); // Skip one space getline(inputs,s); // Input rest of line as the message cout << "Enqueue(" << c << " '" << s << "'" << ")"; try { switch (c) {
  • 3. case 'H': msg.SetPriority(HIGH); break; case 'M': msg.SetPriority(MEDIUM); break; case 'L': msg.SetPriority(LOW); break; } msg.SetMessage(s); ptr->Enqueue(msg); } catch (FullPQ) { cout << " -- Failed Full PriorityQ"; } cout << endl; break; case '-': // Dequeue cout << "Dequeue() -- "; try { ptr->Dequeue(); cout << "Successful"; } catch (EmptyPQ) { cout << "Failed Empty PriorityQ"; } cout << endl; break; case 'x': // Purge inputs >> c; // Input message priority from file cout << "Purge(" << c << ")"; try { switch (c) { case 'H': ptr->Purge(HIGH); break; case 'M': ptr->Purge(MEDIUM); break; case 'L': ptr->Purge(LOW); break;
  • 4. } } catch (EmptyPQ) { cout << " -- Failed Empty PriorityQ"; } cout << endl; break; case '?': // Peek inputs >> n; cout << "Peek(" << n << ") -- "; try { ptr->Peek(n).Print(); } catch (InvalidPeekPQ) { cout << "Failed Invalid Peek PriorityQ"; } cout << endl; break; case 'f': // Front cout << "Front() -- "; try { ptr->Front().Print(); } catch (EmptyPQ) { cout << "Failed Empty PriorityQ"; } cout << endl; break; case 'r': // Rear cout << "Rear() -- "; try
  • 5. { ptr->Rear().Print(); } catch (EmptyPQ) { cout << "Failed Empty PriorityQ"; } cout << endl; break; case 'p': // Print PriorityQ cout << "Print() -- "; ptr->Print(); cout << endl; break; case 's': // Size of PriorityQ cout << "Size() -- " << ptr->Size() << endl; break; case 'm': // Make PriorityQ Empty but ready for use cout << "MakeEmpty()" << endl; ptr->MakeEmpty(); break; case 'd': // Destructor delete ptr; ptr = NULL; cout << "Destructor()" << endl << endl; break; default: // Error cout << "Error - unrecognized operation '" << op << "'" << endl; cout << "Terminating now..." << endl; return 1; break; }
  • 6. inputs >> op; // Attempt to input next command } return 0; } // End main() _____________________________________________________________________________ __ priorityq.h _____________________________________________________________________________ __ #ifndef PRIORITYQ_H #define PRIORITYQ_H #include iostream #include "message.h" using namespace std; // // Exception classes for PriorityQ // class EmptyPQ { /* No additional code */ }; // Exception class for empty PriorityQ condition class FullPQ { /* No additional code */ }; // Exception class for full PriorityQ condition class InvalidPeekPQ { /* No additional code */ }; // Exception class for invalid PriorityQ peek condition // // Priority Queue Node Structure // struct Node // Linked priority queue node structure { Message data; // Field for storing data in the priority queue node Node* nextPtr; // Points to next priority queue node Node* previousPtr; // Points to previous priority queue node }; // // PriorityQ class declaration //
  • 7. class PriorityQ // Double linked queue of messages sorted by priority { private: Node* frontPtr; // Points to front node of priority queue Node* rearPtr; // Points to rear node of priority queue int count; // Number of values stored in priority queue public: /********** Start of functions you must implement for PriorityQ **************/ // Implement the following nine public functions in the file named priorityq.cpp PriorityQ(); // PriorityQ() // Initializes all private variables to indicate an empty priority queue ~PriorityQ(); //~PriorityQ() // Deallocates all priority queue nodes // No memory leak allowed void MakeEmpty(); // MakeEmpty() // Deallocates all priority queue nodes and returns priority queue to empty ready-to-use state // No memory leak allowed void Enqueue(Message msg); // Enqueue() // Adds value to priority queue in correct position and increments count. // Duplicates are allowed. // Highest priority messages must always be at front of queue // Lowest priority messages must always be at rear of queue // Add AFTER messages of similar priority // If queue is already full, throws FullPQ exception. void Dequeue(); // Dequeue() // Removes highest priority message from front of priority queue and decrements count.
  • 8. // If queue is empty, throws EmptyPQ exception // No memory leak allowed void Purge(Priorities p); // Purge() // Removes all messages of priority p from queue leaving all other messages in priority queue // If queue is empty, throws EmptyPQ exception // No memory leak allowed Message Front() const; // Front() // Returns message at front of priority queue // If queue is empty, throws EmptyPQ Message Rear() const; // Rear() // Returns message at rear of priority queue // If queue is empty, throws EmptyPQ Message Peek(int n) const; // Peek() // Returns message n positions from front of priority queue // If position n does not exist, throws InvalidPeekPQ bool IsFull() const; // IsFull() // Returns true if queue is full. Returns false otherwise. DOES NOT MODIFY THE PRIORITY QUEUE bool IsEmpty() const; // IsEmpty() // Returns true if queue is empty. Returns false otherwise. DOES NOT MODIFY THE PRIORITY QUEUE int Size() const; // Size() // Returns number of items stored in priority queue. DOES NOT MODIFY THE PRIORITY QUEUE /*********** End of functions you must implement for PriorityQ ***************/
  • 9. void Print() const // Print() -- DO NOT MODIFY OR RELOCATE THIS FUNCTION // Prints contents of priority queue without modifying its contents { Node* tempPtr = frontPtr; // Prints queue nodes Front-to-Rear order cout << "Front { "; while (tempPtr != NULL) { //cout << '(' << tempPtr->data.GetMessage() << ')' << ' '; tempPtr->data.Print(); cout << ' '; tempPtr = tempPtr->nextPtr; } cout << "} Rear Rear { "; // Prints queue nodes Rear-to-Front order tempPtr = rearPtr; while (tempPtr != NULL) { //cout << '(' << tempPtr->data.GetMessage() << ')' << ' '; tempPtr->data.Print(); cout << ' '; tempPtr = tempPtr->previousPtr; } cout << "} Front"; } // End Print() }; #endif _____________________________________________________________________________ __ message.h
  • 10. _____________________________________________________________________________ __ #ifndef MESSAGE_H #define MESSAGE_H #include iostream using namespace std; // // Define enumerated Priorities type // enum Priorities {UNKNOWN, LOW, MEDIUM, HIGH}; // // Message class declaration // class Message // Models a single message with priority { private: Priorities priority; // Stores message priority string message; // Stores message contents public: /********** Start of functions you must implement for Message **************/ // Implement the following five public functions in the file named message.cpp Message(); // Initializes message to empty string with UNKNOWN priority void SetPriority(Priorities p); // Sets priority equal to p void SetMessage(string msg); // Sets message equal to msg Priorities GetPriority() const; // Returns priority value without modification string GetMessage() const; // Returns message contents without modification /*********** End of functions you must implement for Message ***************/ void Print() const // DO NOT MODIFY OR RELOCATE THIS FUNCTION { cout << "[";
  • 11. if (priority == HIGH) cout << "H"; else if (priority == MEDIUM) cout << "M"; else if (priority == LOW) cout << "L"; else cout << "U"; cout << ", " << message << "]"; } // End Print() const }; #endif _____________________________________________________________________________ __ _____________________________________________________________________________ __ # p04input1.txt -- Test: PriorityQ(), Enqueue(), Dequeue(), ~PriorityQ() # Test adding messages H to L (add at rear) c + H Cat Food + M Goat Chow + L Dog Walk p d # Test adding messages L to H (add at front) c + L Dog Walk + M Goat Chow + H Cat Food p d # Test adding messages (add in between) c + H Cat Food + L Dog Walk
  • 12. + M Goat Chow p d # Test adding messages with same priorities c + H Cat Food + H Dog Kibble + H Fish Food + H Cat Food p d # Test adding messages (arbitrary) c + H Cat Food + L Dog Walk + M Goat Chow + H Horse Oats + L Rabbit Food + M Zebra Stripes + H Bird Seed + L Dog Walk p d # Test Dequeue normal operation c + H Cat Food + L Dog Walk + M Goat Chow p - p - p - p + H Cat Food
  • 13. + L Dog Walk + M Goat Chow + H Horse Oats + L Rabbit Food + M Zebra Stripes + H Bird Seed p - p - p - p - p - p - p - p d # Test Dequeue error handling c - + H Cat Food + L Dog Walk + M Goat Chow + H Horse Oats + L Rabbit Food + M Zebra Stripes + H Bird Seed p - p
  • 15. - p s - p s d # Test Front() and Rear() c f r + H Cat Food f r + L Dog Walk f r + M Goat Chow f r + H Horse Oats f r + L Rabbit Food f r + M Zebra Stripes f r + H Bird Seed f r p - f
  • 16. r p - f r p - f r p d # Test MakeEmpty normal operation c + H Cat Food + L Dog Walk + M Goat Chow + H Horse Oats + L Rabbit Food + M Zebra Stripes + H Bird Seed p m p + H Cat Food + L Dog Walk + M Goat Chow p m p d # Test MakeEmpty error handling c p m p
  • 17. d _____________________________________________________________________________ __ _____________________________________________________________________________ __ # p04input3.txt -- Test: Peek(), Purge() # Test Peek() normal operation c + H Cat Food + L Dog Walk + M Goat Chow p ? 0 ? 1 ? 2 p - ? 0 ? 1 p - ? 0 ? 1 ? 2 ? 3 ? 5 p - ? 0 ? 1 ? 2 ? 3 ? 5 d # Test Peek() error handling
  • 18. c ? 0 ? 1 + H Cat Food + L Dog Walk + M Goat Chow p ? 1 ? 3 ? 5 - p ? 1 ? 2 ? 3 ? 5 p d # Test Purge(HIGH) normal operation c p + H Cat Food + L Dog Walk + M Goat Chow + H Horse Oats + L Rabbit Food + M Zebra Stripes + H Bird Seed p s x H p s d # Test Purge(MEDIUM) normal operation c
  • 19. p + H Cat Food + L Dog Walk + M Goat Chow + H Horse Oats + L Rabbit Food + M Zebra Stripes + H Bird Seed p s x M p s d # Test Purge(LOW) normal operation c p + H Cat Food + L Dog Walk + M Goat Chow + H Horse Oats + L Rabbit Food + M Zebra Stripes + H Bird Seed p s x L p s d # Test Purge() normal operation c p + H Cat Food + L Dog Walk + M Goat Chow
  • 20. + H Horse Oats + L Rabbit Food + M Zebra Stripes + H Bird Seed p s x H p s x M p s x L p s + H Cat Food + L Dog Walk + M Goat Chow + H Horse Oats + L Rabbit Food + M Zebra Stripes + H Bird Seed p x L p x M p x H p d # Test Purge() error handling c p x L p s
  • 21. x M p s x H p s d _____________________________________________________________________________ __ Complete the provided partial C++ program that will implement a Priority Queue ADT (abstract data type) in which the internal representation of the priority queue is a double linked series of dynamically allocated nodes. The Priority Queue will be used to store text messages read from an input file. Each message has a priority (H for High, M for Medium, L for Low). When input from the file by the main function, the message and priority information are stored within an object of the type Message. The text message is stored as a string attribute, and the message priority is represented by an attribute with type Priorities, an enumerated type provided. Priority Queue Rules of operation: (1) If the Priority Queue contains messages which have different priority levels, then the highest priority messages must be removed from the FRONT of the Priority Queue before any lower priority messages are removed whenever the Dequeue 0 method is invoked HINT: The priority queue is sorted by Priority Level (highest to lowest) as it is being constructed by Enqueue in much the same way as we discussed with the implementation of a sorted linked list. (2) Given two messages of the same priority level, the message that has been in the Priority Queue for the longest time has the highest priority. (FIFo operation within a priority level) Solution Given below are the complete set of files for the question. The output tested with the 3 input file's is shown... Please don;t forget to rate the answer if it helped. Thank you. message.h #ifndef MESSAGE_H #define MESSAGE_H #include using namespace std; // // Define enumerated Priorities type //
  • 22. enum Priorities {UNKNOWN, LOW, MEDIUM, HIGH}; // // Message class declaration // class Message // Models a single message with priority { private: Priorities priority; // Stores message priority string message; // Stores message contents public: /********** Start of functions you must implement for Message **************/ // Implement the following five public functions in the file named message.cpp Message(); // Initializes message to empty string with UNKNOWN priority void SetPriority(Priorities p); // Sets priority equal to p void SetMessage(string msg); // Sets message equal to msg Priorities GetPriority() const; // Returns priority value without modification string GetMessage() const; // Returns message contents without modification /*********** End of functions you must implement for Message ***************/ void Print() const // DO NOT MODIFY OR RELOCATE THIS FUNCTION { cout << "["; if (priority == HIGH) cout << "H"; else if (priority == MEDIUM) cout << "M"; else if (priority == LOW) cout << "L"; else cout << "U";
  • 23. cout << ", " << message << "]"; } // End Print() const }; #endif message.cpp #include "message.h" Message::Message() // Initializes message to empty string with UNKNOWN priority { message = ""; priority = UNKNOWN; } void Message::SetPriority(Priorities p) // Sets priority equal to p { priority = p; } void Message::SetMessage(string msg) // Sets message equal to msg { message = msg; } Priorities Message::GetPriority() const // Returns priority value without modification { return priority; } string Message::GetMessage() const // Returns message contents without modification { return message; } priorityq.h #ifndef PRIORITYQ_H #define PRIORITYQ_H #include #include "message.h" using namespace std; // // Exception classes for PriorityQ //
  • 24. class EmptyPQ { /* No additional code */ }; // Exception class for empty PriorityQ condition class FullPQ { /* No additional code */ }; // Exception class for full PriorityQ condition class InvalidPeekPQ { /* No additional code */ }; // Exception class for invalid PriorityQ peek condition // // Priority Queue Node Structure // struct Node // Linked priority queue node structure { Message data; // Field for storing data in the priority queue node Node* nextPtr; // Points to next priority queue node Node* previousPtr; // Points to previous priority queue node }; // // PriorityQ class declaration // class PriorityQ // Double linked queue of messages sorted by priority { private: Node* frontPtr; // Points to front node of priority queue Node* rearPtr; // Points to rear node of priority queue int count; // Number of values stored in priority queue public: /********** Start of functions you must implement for PriorityQ **************/ // Implement the following nine public functions in the file named priorityq.cpp PriorityQ(); // PriorityQ() // Initializes all private variables to indicate an empty priority queue ~PriorityQ(); //~PriorityQ() // Deallocates all priority queue nodes // No memory leak allowed
  • 25. void MakeEmpty(); // MakeEmpty() // Deallocates all priority queue nodes and returns priority queue to empty ready-to-use state // No memory leak allowed void Enqueue(Message msg); // Enqueue() // Adds value to priority queue in correct position and increments count. // Duplicates are allowed. // Highest priority messages must always be at front of queue // Lowest priority messages must always be at rear of queue // Add AFTER messages of similar priority // If queue is already full, throws FullPQ exception. void Dequeue(); // Dequeue() // Removes highest priority message from front of priority queue and decrements count. // If queue is empty, throws EmptyPQ exception // No memory leak allowed void Purge(Priorities p); // Purge() // Removes all messages of priority p from queue leaving all other messages in priority queue // If queue is empty, throws EmptyPQ exception // No memory leak allowed Message Front() const; // Front() // Returns message at front of priority queue // If queue is empty, throws EmptyPQ Message Rear() const; // Rear() // Returns message at rear of priority queue // If queue is empty, throws EmptyPQ Message Peek(int n) const; // Peek() // Returns message n positions from front of priority queue // If position n does not exist, throws InvalidPeekPQ
  • 26. bool IsFull() const; // IsFull() // Returns true if queue is full. Returns false otherwise. DOES NOT MODIFY THE PRIORITY QUEUE bool IsEmpty() const; // IsEmpty() // Returns true if queue is empty. Returns false otherwise. DOES NOT MODIFY THE PRIORITY QUEUE int Size() const; // Size() // Returns number of items stored in priority queue. DOES NOT MODIFY THE PRIORITY QUEUE /*********** End of functions you must implement for PriorityQ ***************/ void Print() const // Print() -- DO NOT MODIFY OR RELOCATE THIS FUNCTION // Prints contents of priority queue without modifying its contents { Node* tempPtr = frontPtr; // Prints queue nodes Front-to-Rear order cout << "Front { "; while (tempPtr != NULL) { //cout << '(' << tempPtr->data.GetMessage() << ')' << ' '; tempPtr->data.Print(); cout << ' '; tempPtr = tempPtr->nextPtr; } cout << "} Rear Rear { "; // Prints queue nodes Rear-to-Front order tempPtr = rearPtr; while (tempPtr != NULL)
  • 27. { //cout << '(' << tempPtr->data.GetMessage() << ')' << ' '; tempPtr->data.Print(); cout << ' '; tempPtr = tempPtr->previousPtr; } cout << "} Front"; } // End Print() }; #endif priorityq.cpp #include "priorityq.h" PriorityQ::PriorityQ() // PriorityQ() // Initializes all private variables to indicate an empty priority queue { frontPtr = NULL; rearPtr = NULL; count = 0; } PriorityQ::~PriorityQ() //~PriorityQ() // Deallocates all priority queue nodes // No memory leak allowed { MakeEmpty(); } void PriorityQ::MakeEmpty() // MakeEmpty() // Deallocates all priority queue nodes and returns priority queue to empty ready-to-use state // No memory leak allowed { Node *temp;
  • 28. while(frontPtr != NULL) { temp = frontPtr->nextPtr; delete frontPtr; frontPtr = temp; } frontPtr = NULL; rearPtr = NULL; count = 0; } void PriorityQ::Enqueue(Message msg) // Enqueue() // Adds value to priority queue in correct position and increments count. // Duplicates are allowed. // Highest priority messages must always be at front of queue // Lowest priority messages must always be at rear of queue // Add AFTER messages of similar priority // If queue is already full, throws FullPQ exception. { Node *n = new Node; n->data = msg; n->nextPtr = NULL; n->previousPtr = NULL; if(IsEmpty()) frontPtr = rearPtr = n; else { Node *curr = rearPtr; while(curr != NULL && msg.GetPriority() > curr->data.GetPriority()) { curr = curr->previousPtr; } if(curr == NULL) //need to insert as first node { n->nextPtr = frontPtr; frontPtr ->previousPtr = n;
  • 29. frontPtr = n; } else { n->nextPtr = curr->nextPtr; n->previousPtr = curr; curr->nextPtr = n; if(n->nextPtr != NULL) n->nextPtr->previousPtr = n; else rearPtr = n; } } count++; } void PriorityQ::Dequeue() // Dequeue() // Removes highest priority message from front of priority queue and decrements count. // If queue is empty, throws EmptyPQ exception // No memory leak allowed { if(IsEmpty()) throw EmptyPQ(); Node *next = frontPtr->nextPtr; if(next != NULL) next->previousPtr = NULL; delete frontPtr; frontPtr = next; if(frontPtr == NULL) rearPtr = NULL; count--; } void PriorityQ::Purge(Priorities p) // Purge() // Removes all messages of priority p from queue leaving all other messages in priority queue
  • 30. // If queue is empty, throws EmptyPQ exception // No memory leak allowed { if(IsEmpty()) throw EmptyPQ(); //search from backwards the 1st message matching priority. Node *curr = rearPtr; while(curr != NULL) { if(curr->data.GetPriority() == p) //found matching { Node *delnode = curr; Node *saved; Node *next = curr->nextPtr; //delete all nodes from the found node till we see a node which does not match while(delnode!= NULL && delnode->data.GetPriority() == p) { saved = delnode->previousPtr; delete delnode; delnode = saved; count --; } if(delnode == NULL) frontPtr = next; else delnode->nextPtr = next; if(frontPtr == NULL) rearPtr = NULL; if(next != NULL) next->previousPtr = delnode;
  • 31. else rearPtr = delnode; break; } else if(curr->data.GetPriority() > p) break; else curr = curr->previousPtr; } } Message PriorityQ::Front() const // Front() // Returns message at front of priority queue // If queue is empty, throws EmptyPQ { if(IsEmpty()) throw EmptyPQ(); return frontPtr->data; } Message PriorityQ::Rear() const // Rear() // Returns message at rear of priority queue // If queue is empty, throws EmptyPQ { if(IsEmpty()) throw EmptyPQ(); return rearPtr->data; } Message PriorityQ::Peek(int n) const // Peek() // Returns message n positions from front of priority queue // If position n does not exist, throws InvalidPeekPQ {
  • 32. if(IsEmpty() || n < 0 || n >= count) throw InvalidPeekPQ(); Node *curr = frontPtr; for(int p = 0; p < n; p++) curr = curr->nextPtr; return curr->data; } bool PriorityQ::IsFull() const // IsFull() // Returns true if queue is full. Returns false otherwise. DOES NOT MODIFY THE PRIORITY QUEUE { return false; } bool PriorityQ::IsEmpty() const // IsEmpty() // Returns true if queue is empty. Returns false otherwise. DOES NOT MODIFY THE PRIORITY QUEUE { return count == 0; } int PriorityQ::Size() const // Size() // Returns number of items stored in priority queue. DOES NOT MODIFY THE PRIORITY QUEUE { return count; } main.cpp #include #include #include #include #include "priorityq.h"
  • 33. using namespace std; int main(int argc, char* argv[]) { ifstream inputs; // Input file for commands char op; // Hold operation int n; // Integer input from file PriorityQ* ptr = NULL; // Will point to priority queue object Message msg; // Assembled message Priorities p; // Message priority char c; // Message priority input from file string s; // Message string from file // Output usage message if one input file name is not provided if (argc != 2) { cout << "Usage: project03 "; return 1; } // Attempt to open input file -- terminate if file does not open inputs.open(argv[1]); if (!inputs) { cout << "Error - unable to open input file" << endl; return 1; } // Process commands from input file getline(inputs, s); // Input comment line cout << s << endl; // Echo comment line inputs >> op; // Attempt to input first command while (inputs) { switch (op) // Identify and perform operation input from file { case '#': // Echo Comment getline(inputs, s);
  • 34. cout << "-------- " << op << s << endl; break; case 'c': // Constructor cout << endl << "Constructor()"; try { ptr = new PriorityQ; cout << endl; } catch ( std::bad_alloc ) { cout << "Failed : Terminating now..." << endl; return 1; } break; case '+': // Enqueue inputs >> c; // Input message priority from file inputs.ignore(100, ' '); // Skip one space getline(inputs,s); // Input rest of line as the message cout << "Enqueue(" << c << " '" << s << "'" << ")"; try { switch (c) { case 'H': msg.SetPriority(HIGH); break; case 'M': msg.SetPriority(MEDIUM); break; case 'L': msg.SetPriority(LOW); break; } msg.SetMessage(s); ptr->Enqueue(msg); } catch (FullPQ) { cout << " -- Failed Full PriorityQ"; } cout << endl;
  • 35. break; case '-': // Dequeue cout << "Dequeue() -- "; try { ptr->Dequeue(); cout << "Successful"; } catch (EmptyPQ) { cout << "Failed Empty PriorityQ"; } cout << endl; break; case 'x': // Purge inputs >> c; // Input message priority from file cout << "Purge(" << c << ")"; try { switch (c) { case 'H': ptr->Purge(HIGH); break; case 'M': ptr->Purge(MEDIUM); break; case 'L': ptr->Purge(LOW); break; } } catch (EmptyPQ) { cout << " -- Failed Empty PriorityQ"; } cout << endl; break; case '?': // Peek inputs >> n; cout << "Peek(" << n << ") -- "; try
  • 36. { ptr->Peek(n).Print(); } catch (InvalidPeekPQ) { cout << "Failed Invalid Peek PriorityQ"; } cout << endl; break; case 'f': // Front cout << "Front() -- "; try { ptr->Front().Print(); } catch (EmptyPQ) { cout << "Failed Empty PriorityQ"; } cout << endl; break; case 'r': // Rear cout << "Rear() -- "; try { ptr->Rear().Print(); } catch (EmptyPQ) { cout << "Failed Empty PriorityQ"; } cout << endl; break; case 'p': // Print PriorityQ cout << "Print() -- ";
  • 37. ptr->Print(); cout << endl; break; case 's': // Size of PriorityQ cout << "Size() -- " << ptr->Size() << endl; break; case 'm': // Make PriorityQ Empty but ready for use cout << "MakeEmpty()" << endl; ptr->MakeEmpty(); break; case 'd': // Destructor delete ptr; ptr = NULL; cout << "Destructor()" << endl << endl; break; default: // Error cout << "Error - unrecognized operation '" << op << "'" << endl; cout << "Terminating now..." << endl; return 1; break; } inputs >> op; // Attempt to input next command } return 0; } // End main() output amoeba-2:priorityQ raji$ g++ message.cpp priorityq.cpp main.cpp amoeba-2:priorityQ raji$ ./a.out input1.txt # p04input1.txt -- Test: PriorityQ(), Enqueue(), Dequeue(), ~PriorityQ() -------- # Test adding messages H to L (add at rear) Constructor()
  • 38. Enqueue(H 'Cat Food') Enqueue(M 'Goat Chow') Enqueue(L 'Dog Walk') Print() -- Front { [H, Cat Food] [M, Goat Chow] [L, Dog Walk] } Rear Rear { [L, Dog Walk] [M, Goat Chow] [H, Cat Food] } Front Destructor() -------- # Test adding messages L to H (add at front) Constructor() Enqueue(L 'Dog Walk') Enqueue(M 'Goat Chow') Enqueue(H 'Cat Food') Print() -- Front { [H, Cat Food] [M, Goat Chow] [L, Dog Walk] } Rear Rear { [L, Dog Walk] [M, Goat Chow] [H, Cat Food] } Front Destructor() -------- # Test adding messages (add in between) Constructor() Enqueue(H 'Cat Food') Enqueue(L 'Dog Walk') Enqueue(M 'Goat Chow') Print() -- Front { [H, Cat Food] [M, Goat Chow] [L, Dog Walk] } Rear Rear { [L, Dog Walk] [M, Goat Chow] [H, Cat Food] } Front Destructor() -------- # Test adding messages with same priorities Constructor() Enqueue(H 'Cat Food') Enqueue(H 'Dog Kibble') Enqueue(H 'Fish Food') Enqueue(H 'Cat Food') Print() -- Front { [H, Cat Food] [H, Dog Kibble] [H, Fish Food] [H, Cat Food] } Rear Rear { [H, Cat Food] [H, Fish Food] [H, Dog Kibble] [H, Cat Food] } Front Destructor() -------- # Test adding messages (arbitrary)
  • 39. Constructor() Enqueue(H 'Cat Food') Enqueue(L 'Dog Walk') Enqueue(M 'Goat Chow') Enqueue(H 'Horse Oats') Enqueue(L 'Rabbit Food') Enqueue(M 'Zebra Stripes') Enqueue(H 'Bird Seed') Enqueue(L 'Dog Walk') Print() -- Front { [H, Cat Food] [H, Horse Oats] [H, Bird Seed] [M, Goat Chow] [M, Zebra Stripes] [L, Dog Walk] [L, Rabbit Food] [L, Dog Walk] } Rear Rear { [L, Dog Walk] [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] [M, Goat Chow] [H, Bird Seed] [H, Horse Oats] [H, Cat Food] } Front Destructor() -------- # Test Dequeue normal operation Constructor() Enqueue(H 'Cat Food') Enqueue(L 'Dog Walk') Enqueue(M 'Goat Chow') Print() -- Front { [H, Cat Food] [M, Goat Chow] [L, Dog Walk] } Rear Rear { [L, Dog Walk] [M, Goat Chow] [H, Cat Food] } Front Dequeue() -- Successful Print() -- Front { [M, Goat Chow] [L, Dog Walk] } Rear Rear { [L, Dog Walk] [M, Goat Chow] } Front Dequeue() -- Successful Print() -- Front { [L, Dog Walk] } Rear Rear { [L, Dog Walk] } Front Dequeue() -- Successful Print() -- Front { } Rear Rear { } Front Enqueue(H 'Cat Food') Enqueue(L 'Dog Walk') Enqueue(M 'Goat Chow') Enqueue(H 'Horse Oats') Enqueue(L 'Rabbit Food')
  • 40. Enqueue(M 'Zebra Stripes') Enqueue(H 'Bird Seed') Print() -- Front { [H, Cat Food] [H, Horse Oats] [H, Bird Seed] [M, Goat Chow] [M, Zebra Stripes] [L, Dog Walk] [L, Rabbit Food] } Rear Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] [M, Goat Chow] [H, Bird Seed] [H, Horse Oats] [H, Cat Food] } Front Dequeue() -- Successful Print() -- Front { [H, Horse Oats] [H, Bird Seed] [M, Goat Chow] [M, Zebra Stripes] [L, Dog Walk] [L, Rabbit Food] } Rear Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] [M, Goat Chow] [H, Bird Seed] [H, Horse Oats] } Front Dequeue() -- Successful Print() -- Front { [H, Bird Seed] [M, Goat Chow] [M, Zebra Stripes] [L, Dog Walk] [L, Rabbit Food] } Rear Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] [M, Goat Chow] [H, Bird Seed] } Front Dequeue() -- Successful Print() -- Front { [M, Goat Chow] [M, Zebra Stripes] [L, Dog Walk] [L, Rabbit Food] } Rear Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] [M, Goat Chow] } Front Dequeue() -- Successful Print() -- Front { [M, Zebra Stripes] [L, Dog Walk] [L, Rabbit Food] } Rear Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] } Front Dequeue() -- Successful Print() -- Front { [L, Dog Walk] [L, Rabbit Food] } Rear Rear { [L, Rabbit Food] [L, Dog Walk] } Front Dequeue() -- Successful Print() -- Front { [L, Rabbit Food] } Rear Rear { [L, Rabbit Food] } Front Dequeue() -- Successful Print() -- Front { } Rear Rear { } Front Destructor() -------- # Test Dequeue error handling Constructor() Dequeue() -- Failed Empty PriorityQ
  • 41. Enqueue(H 'Cat Food') Enqueue(L 'Dog Walk') Enqueue(M 'Goat Chow') Enqueue(H 'Horse Oats') Enqueue(L 'Rabbit Food') Enqueue(M 'Zebra Stripes') Enqueue(H 'Bird Seed') Print() -- Front { [H, Cat Food] [H, Horse Oats] [H, Bird Seed] [M, Goat Chow] [M, Zebra Stripes] [L, Dog Walk] [L, Rabbit Food] } Rear Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] [M, Goat Chow] [H, Bird Seed] [H, Horse Oats] [H, Cat Food] } Front Dequeue() -- Successful Print() -- Front { [H, Horse Oats] [H, Bird Seed] [M, Goat Chow] [M, Zebra Stripes] [L, Dog Walk] [L, Rabbit Food] } Rear Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] [M, Goat Chow] [H, Bird Seed] [H, Horse Oats] } Front Dequeue() -- Successful Print() -- Front { [H, Bird Seed] [M, Goat Chow] [M, Zebra Stripes] [L, Dog Walk] [L, Rabbit Food] } Rear Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] [M, Goat Chow] [H, Bird Seed] } Front Dequeue() -- Successful Print() -- Front { [M, Goat Chow] [M, Zebra Stripes] [L, Dog Walk] [L, Rabbit Food] } Rear Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] [M, Goat Chow] } Front Dequeue() -- Successful Print() -- Front { [M, Zebra Stripes] [L, Dog Walk] [L, Rabbit Food] } Rear Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] } Front Dequeue() -- Successful Print() -- Front { [L, Dog Walk] [L, Rabbit Food] } Rear Rear { [L, Rabbit Food] [L, Dog Walk] } Front Dequeue() -- Successful Print() -- Front { [L, Rabbit Food] } Rear Rear { [L, Rabbit Food] } Front Dequeue() -- Successful Print() -- Front { } Rear Rear { } Front
  • 42. Dequeue() -- Failed Empty PriorityQ Print() -- Front { } Rear Rear { } Front Destructor() amoeba-2:priorityQ raji$ ./a.out input2.txt # p04input2.txt -- Test: Size(), Front(), Rear(), MakeEmpty() -------- # Test Size() Constructor() Size() -- 0 Enqueue(H 'Cat Food') Size() -- 1 Enqueue(L 'Dog Walk') Size() -- 2 Enqueue(M 'Goat Chow') Print() -- Front { [H, Cat Food] [M, Goat Chow] [L, Dog Walk] } Rear Rear { [L, Dog Walk] [M, Goat Chow] [H, Cat Food] } Front Size() -- 3 Dequeue() -- Successful Print() -- Front { [M, Goat Chow] [L, Dog Walk] } Rear Rear { [L, Dog Walk] [M, Goat Chow] } Front Size() -- 2 Dequeue() -- Successful Print() -- Front { [L, Dog Walk] } Rear Rear { [L, Dog Walk] } Front Size() -- 1 Dequeue() -- Successful Print() -- Front { } Rear Rear { } Front Size() -- 0 Dequeue() -- Failed Empty PriorityQ Print() -- Front { } Rear Rear { } Front Size() -- 0 Destructor() --------
  • 43. # Test Front() and Rear() Constructor() Front() -- Failed Empty PriorityQ Rear() -- Failed Empty PriorityQ Enqueue(H 'Cat Food') Front() -- [H, Cat Food] Rear() -- [H, Cat Food] Enqueue(L 'Dog Walk') Front() -- [H, Cat Food] Rear() -- [L, Dog Walk] Enqueue(M 'Goat Chow') Front() -- [H, Cat Food] Rear() -- [L, Dog Walk] Enqueue(H 'Horse Oats') Front() -- [H, Cat Food] Rear() -- [L, Dog Walk] Enqueue(L 'Rabbit Food') Front() -- [H, Cat Food] Rear() -- [L, Rabbit Food] Enqueue(M 'Zebra Stripes') Front() -- [H, Cat Food] Rear() -- [L, Rabbit Food] Enqueue(H 'Bird Seed') Front() -- [H, Cat Food] Rear() -- [L, Rabbit Food] Print() -- Front { [H, Cat Food] [H, Horse Oats] [H, Bird Seed] [M, Goat Chow] [M, Zebra Stripes] [L, Dog Walk] [L, Rabbit Food] } Rear Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] [M, Goat Chow] [H, Bird Seed] [H, Horse Oats] [H, Cat Food] } Front Dequeue() -- Successful Front() -- [H, Horse Oats] Rear() -- [L, Rabbit Food] Print() -- Front { [H, Horse Oats] [H, Bird Seed] [M, Goat Chow] [M, Zebra Stripes] [L, Dog Walk] [L, Rabbit Food] } Rear Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] [M, Goat Chow] [H, Bird Seed] [H, Horse Oats] } Front
  • 44. Dequeue() -- Successful Front() -- [H, Bird Seed] Rear() -- [L, Rabbit Food] Print() -- Front { [H, Bird Seed] [M, Goat Chow] [M, Zebra Stripes] [L, Dog Walk] [L, Rabbit Food] } Rear Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] [M, Goat Chow] [H, Bird Seed] } Front Dequeue() -- Successful Front() -- [M, Goat Chow] Rear() -- [L, Rabbit Food] Print() -- Front { [M, Goat Chow] [M, Zebra Stripes] [L, Dog Walk] [L, Rabbit Food] } Rear Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] [M, Goat Chow] } Front Destructor() -------- # Test MakeEmpty normal operation Constructor() Enqueue(H 'Cat Food') Enqueue(L 'Dog Walk') Enqueue(M 'Goat Chow') Enqueue(H 'Horse Oats') Enqueue(L 'Rabbit Food') Enqueue(M 'Zebra Stripes') Enqueue(H 'Bird Seed') Print() -- Front { [H, Cat Food] [H, Horse Oats] [H, Bird Seed] [M, Goat Chow] [M, Zebra Stripes] [L, Dog Walk] [L, Rabbit Food] } Rear Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] [M, Goat Chow] [H, Bird Seed] [H, Horse Oats] [H, Cat Food] } Front MakeEmpty() Print() -- Front { } Rear Rear { } Front Enqueue(H 'Cat Food') Enqueue(L 'Dog Walk') Enqueue(M 'Goat Chow') Print() -- Front { [H, Cat Food] [M, Goat Chow] [L, Dog Walk] } Rear Rear { [L, Dog Walk] [M, Goat Chow] [H, Cat Food] } Front MakeEmpty()
  • 45. Print() -- Front { } Rear Rear { } Front Destructor() -------- # Test MakeEmpty error handling Constructor() Print() -- Front { } Rear Rear { } Front MakeEmpty() Print() -- Front { } Rear Rear { } Front Destructor() amoeba-2:priorityQ raji$ ./a.out input3.txt # p04input3.txt -- Test: Peek(), Purge() -------- # Test Peek() normal operation Constructor() Enqueue(H 'Cat Food') Enqueue(L 'Dog Walk') Enqueue(M 'Goat Chow') Print() -- Front { [H, Cat Food] [M, Goat Chow] [L, Dog Walk] } Rear Rear { [L, Dog Walk] [M, Goat Chow] [H, Cat Food] } Front Peek(0) -- [H, Cat Food] Peek(1) -- [M, Goat Chow] Peek(2) -- [L, Dog Walk] Print() -- Front { [H, Cat Food] [M, Goat Chow] [L, Dog Walk] } Rear Rear { [L, Dog Walk] [M, Goat Chow] [H, Cat Food] } Front Dequeue() -- Successful Peek(0) -- [M, Goat Chow] Peek(1) -- [L, Dog Walk] Print() -- Front { [M, Goat Chow] [L, Dog Walk] } Rear Rear { [L, Dog Walk] [M, Goat Chow] } Front Dequeue() -- Successful Peek(0) -- [L, Dog Walk] Peek(1) -- Failed Invalid Peek PriorityQ Peek(2) -- Failed Invalid Peek PriorityQ
  • 46. Peek(3) -- Failed Invalid Peek PriorityQ Peek(5) -- Failed Invalid Peek PriorityQ Print() -- Front { [L, Dog Walk] } Rear Rear { [L, Dog Walk] } Front Dequeue() -- Successful Peek(0) -- Failed Invalid Peek PriorityQ Peek(1) -- Failed Invalid Peek PriorityQ Peek(2) -- Failed Invalid Peek PriorityQ Peek(3) -- Failed Invalid Peek PriorityQ Peek(5) -- Failed Invalid Peek PriorityQ Destructor() -------- # Test Peek() error handling Constructor() Peek(0) -- Failed Invalid Peek PriorityQ Peek(1) -- Failed Invalid Peek PriorityQ Enqueue(H 'Cat Food') Enqueue(L 'Dog Walk') Enqueue(M 'Goat Chow') Print() -- Front { [H, Cat Food] [M, Goat Chow] [L, Dog Walk] } Rear Rear { [L, Dog Walk] [M, Goat Chow] [H, Cat Food] } Front Peek(1) -- [M, Goat Chow] Peek(3) -- Failed Invalid Peek PriorityQ Peek(5) -- Failed Invalid Peek PriorityQ Dequeue() -- Successful Print() -- Front { [M, Goat Chow] [L, Dog Walk] } Rear Rear { [L, Dog Walk] [M, Goat Chow] } Front Peek(1) -- [L, Dog Walk] Peek(2) -- Failed Invalid Peek PriorityQ Peek(3) -- Failed Invalid Peek PriorityQ Peek(5) -- Failed Invalid Peek PriorityQ Print() -- Front { [M, Goat Chow] [L, Dog Walk] } Rear Rear { [L, Dog Walk] [M, Goat Chow] } Front Destructor() -------- # Test Purge(HIGH) normal operation
  • 47. Constructor() Print() -- Front { } Rear Rear { } Front Enqueue(H 'Cat Food') Enqueue(L 'Dog Walk') Enqueue(M 'Goat Chow') Enqueue(H 'Horse Oats') Enqueue(L 'Rabbit Food') Enqueue(M 'Zebra Stripes') Enqueue(H 'Bird Seed') Print() -- Front { [H, Cat Food] [H, Horse Oats] [H, Bird Seed] [M, Goat Chow] [M, Zebra Stripes] [L, Dog Walk] [L, Rabbit Food] } Rear Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] [M, Goat Chow] [H, Bird Seed] [H, Horse Oats] [H, Cat Food] } Front Size() -- 7 Purge(H) Print() -- Front { [M, Goat Chow] [M, Zebra Stripes] [L, Dog Walk] [L, Rabbit Food] } Rear Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] [M, Goat Chow] } Front Size() -- 4 Destructor() -------- # Test Purge(MEDIUM) normal operation Constructor() Print() -- Front { } Rear Rear { } Front Enqueue(H 'Cat Food') Enqueue(L 'Dog Walk') Enqueue(M 'Goat Chow') Enqueue(H 'Horse Oats') Enqueue(L 'Rabbit Food') Enqueue(M 'Zebra Stripes') Enqueue(H 'Bird Seed') Print() -- Front { [H, Cat Food] [H, Horse Oats] [H, Bird Seed] [M, Goat Chow] [M, Zebra Stripes] [L, Dog Walk] [L, Rabbit Food] } Rear Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] [M, Goat Chow] [H, Bird Seed] [H, Horse Oats] [H, Cat Food] } Front
  • 48. Size() -- 7 Purge(M) Print() -- Front { [H, Cat Food] [H, Horse Oats] [H, Bird Seed] [L, Dog Walk] [L, Rabbit Food] } Rear Rear { [L, Rabbit Food] [L, Dog Walk] [H, Bird Seed] [H, Horse Oats] [H, Cat Food] } Front Size() -- 5 Destructor() -------- # Test Purge(LOW) normal operation Constructor() Print() -- Front { } Rear Rear { } Front Enqueue(H 'Cat Food') Enqueue(L 'Dog Walk') Enqueue(M 'Goat Chow') Enqueue(H 'Horse Oats') Enqueue(L 'Rabbit Food') Enqueue(M 'Zebra Stripes') Enqueue(H 'Bird Seed') Print() -- Front { [H, Cat Food] [H, Horse Oats] [H, Bird Seed] [M, Goat Chow] [M, Zebra Stripes] [L, Dog Walk] [L, Rabbit Food] } Rear Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] [M, Goat Chow] [H, Bird Seed] [H, Horse Oats] [H, Cat Food] } Front Size() -- 7 Purge(L) Print() -- Front { [H, Cat Food] [H, Horse Oats] [H, Bird Seed] [M, Goat Chow] [M, Zebra Stripes] } Rear Rear { [M, Zebra Stripes] [M, Goat Chow] [H, Bird Seed] [H, Horse Oats] [H, Cat Food] } Front Size() -- 5 Destructor() -------- # Test Purge() normal operation Constructor() Print() -- Front { } Rear Rear { } Front
  • 49. Enqueue(H 'Cat Food') Enqueue(L 'Dog Walk') Enqueue(M 'Goat Chow') Enqueue(H 'Horse Oats') Enqueue(L 'Rabbit Food') Enqueue(M 'Zebra Stripes') Enqueue(H 'Bird Seed') Print() -- Front { [H, Cat Food] [H, Horse Oats] [H, Bird Seed] [M, Goat Chow] [M, Zebra Stripes] [L, Dog Walk] [L, Rabbit Food] } Rear Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] [M, Goat Chow] [H, Bird Seed] [H, Horse Oats] [H, Cat Food] } Front Size() -- 7 Purge(H) Print() -- Front { [M, Goat Chow] [M, Zebra Stripes] [L, Dog Walk] [L, Rabbit Food] } Rear Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] [M, Goat Chow] } Front Size() -- 4 Purge(M) Print() -- Front { [L, Dog Walk] [L, Rabbit Food] } Rear Rear { [L, Rabbit Food] [L, Dog Walk] } Front Size() -- 2 Purge(L) Print() -- Front { } Rear Rear { } Front Size() -- 0 Enqueue(H 'Cat Food') Enqueue(L 'Dog Walk') Enqueue(M 'Goat Chow') Enqueue(H 'Horse Oats') Enqueue(L 'Rabbit Food') Enqueue(M 'Zebra Stripes') Enqueue(H 'Bird Seed') Print() -- Front { [H, Cat Food] [H, Horse Oats] [H, Bird Seed] [M, Goat Chow] [M, Zebra Stripes] [L, Dog Walk] [L, Rabbit Food] } Rear Rear { [L, Rabbit Food] [L, Dog Walk] [M, Zebra Stripes] [M, Goat Chow] [H, Bird Seed] [H, Horse Oats] [H, Cat Food] } Front Purge(L)
  • 50. Print() -- Front { [H, Cat Food] [H, Horse Oats] [H, Bird Seed] [M, Goat Chow] [M, Zebra Stripes] } Rear Rear { [M, Zebra Stripes] [M, Goat Chow] [H, Bird Seed] [H, Horse Oats] [H, Cat Food] } Front Purge(M) Print() -- Front { [H, Cat Food] [H, Horse Oats] [H, Bird Seed] } Rear Rear { [H, Bird Seed] [H, Horse Oats] [H, Cat Food] } Front Purge(H) Print() -- Front { } Rear Rear { } Front Destructor() -------- # Test Purge() error handling Constructor() Print() -- Front { } Rear Rear { } Front Purge(L) -- Failed Empty PriorityQ Print() -- Front { } Rear Rear { } Front Size() -- 0 Purge(M) -- Failed Empty PriorityQ Print() -- Front { } Rear Rear { } Front Size() -- 0 Purge(H) -- Failed Empty PriorityQ Print() -- Front { } Rear Rear { } Front Size() -- 0 Destructor() amoeba-2:priorityQ raji$