How to Create a Queue Collection

Because the standard list collection has both a head and a tail, it is also easy to create a derived list collection that mimics the behavior of a first-in-first-out queue. A queue is like a line of people in a cafeteria. The first person in line is the first to be served. As more people come, they go to the end of the line to wait their turn. The list collection member functions AddTail and RemoveHead can be used to add and remove elements specifically from the head or tail of the list; thus the most recently added element is always the last to be removed.

·To create a queue collection:

Derive a new list class from one of the predefined list classes provided with the Microsoft Foundation Class Library and add more member functions to support the semantics of queue operations.

The following example shows how you can append member functions to add an element to the end of the queue and get the element from the front of the queue.

class CPerson : public CObject { ... };

class CQueue : public CObList

{

public:

// go to the end of the line

void AddToEnd( CPerson* newPerson )

{ AddTail( newPerson ); } // end of the queue

// get first element in line

CPerson* GetFromFront()

{ return IsEmpty() ? NULL : (CPerson*)RemoveHead(); }

};