Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a Queue.cnn . Queue.cpp contains the member function implementations for Queue.h . Queue.h = class Queue { public: Queue ( ) ; / /

Create a Queue.cnn. Queue.cpp contains the member function implementations for Queue.h.
Queue.h =
class Queue {
public:
Queue(); // ctor inits a new empty Queue
~Queue(); // dtor erases any remaining QueueItems
void addItem(char* pData);
void removeItem();
void print();
void erase();
private:
QueueItem*_pHead; // always points to first QueueItem in the list
QueueItem*_pTail; // always points to last QueueItem in the list
int _itemCounter; // always increasing for a unique id to assign to each new QueueItem
}; // end definitionclass Queue
The Queue class member functions should not have access to the private members of QueueItem objects. They call the public member functions of QueueItem.
As an example, the outline of the Queue::addItem() member function is shown below. It must add a new QueueItem at the tail of the Queue, and update the _pTail pointer to point to it. The first item placed in the Queue becomes both the head and the tail of the list.:
void Queue::addItem(const char *pData)
{
// dynamically create and init a new QueueItem object
QueueItem *pItem = new QueueItem(pData,++_itemCounter);
if (0==_pHead)// check for empty queue
_pHead =_pTail = pItem;
else
{
// link new item onto tail of list using _pTail pointer
...
}
}
The removeItem() method removes the head QueueItem from the queue, and should release the memory using the C++ delete operator. It updates _pHead to point at the following item (if any) as the new head. If the list becomes empty, both _pHead and _pTail must be set to null (0). It does not change the value of _itemCounter (which is always incremented when a new item is added). If called on an empty Queue, it does nothing.
The erase() method removes all the items in the queue and should release the memory. To implement, you could loop calling removeItem() until the queue is empty.
The Queue destructor should ensure that all items are removed from the queue. The easiest way is to call the erase() method from the destructor.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 19th International Conference Dexa 2008 Turin Italy September 2008 Proceedings Lncs 5181

Authors: Sourav S. Bhowmick ,Josef Kung ,Roland Wagner

2008th Edition

3540856536, 978-3540856535

More Books

Students also viewed these Databases questions