Implementing a Queue - Source Co…

来源:互联网 发布:饥荒怎么修改数据 编辑:程序博客网 时间:2024/05/06 01:50

Implementing a Queue - Source Code

by Eric Suh 

This source file is an implementation of the Queue class. The classis implemented with templates, and the size is determineddynamically at initialization (although the default is 500elements). 
For the templated class, the elements must have the operators<, =, and >defined. 

The actual amount of space allocated for the Queue will be one moreelement than the defined maximum size. This is useful forimplementing the Queue in a circularmethod. 

To understand the circular implementation, think of the array as acircle. When an element is dequeued, the Queue doesn't shift all ofthe elements forward to the start of the queue. Instead, the classshifts the start of the queue back. Eventually, the start of thequeue will have shifted so far that the queue will extend beyondthe end of the array. This is where the circle comes in. When thequeue reaches the end of the array, it wraps around to thebeginning of the array.

/ * Code provided by Eric Suh
    |   ===========================================================   |
    |   This Queue Class has been implemented withtemplates and      |
    |   the size is determined dynamically atinitialization.         |
    |                                                        |
    |   The actual amount of space allocated for theQueue will be     |
    |   one more element space than the defined maximumsize. This     |
    |   is useful for implementing the Queue in acircular method.     |
    |                                                        |
    |   To understand the circular implementation, thinkof the       |
    |   array as a circle. When you reach the end of thearray, you    |
    |   wrap around to the beginning of the array.                 |
    |                                                        |
    |   So, when an element is dequeued, the Queuedoesn't shift.     |
    |   Instead, you updated an indicator of the start ofthe queue.   |
    |                                                        |
    -------------------------------------------------------------------
*/

#ifndef __QueueClassH__
#define __QueueClassH__

#include <assert.h>   // For error-checkingpurposes

//-------------------------------------------------
// Main structure of Queue Class:
//-------------------------------------------------

template <class Elem>
class Queue
{
  public:
    Queue(intMaxSize=500);
    Queue(constQueue<Elem>&OtherQueue);
    ~Queue(void);

    void      Enqueue(const Elem&Item);    //Adds Item to Queue end
    Elem      Dequeue(void);             // Returns Item fromQueue
    inline intElemNum(void);              // ReturnsNumber of Elements

  protected:
    Elem    *Data;     // The actual Data array
    const int MAX_NUM;  // The actual spaces will be one more thanthis
    int      Beginning, // Numberedlocation of the start and end
            End;

    // Instead ofcalculating the number of elements, using this variable
    // is much moreconvenient.
    int      ElemCount;
};

//-------------------------------------------------
// Implementation of Queue Class:
//-------------------------------------------------

// Queue Constructor function
template <class Elem>
Queue<Elem>::Queue(int MaxSize):
    MAX_NUM( MaxSize )  // Initialize the constant
{
  // This extra space added will allow us todistinguish between
  // the Beginning and the Endlocations.
  Data     = new Elem[MAX_NUM + 1];
  Beginning = 0;
  End      = 0;
  ElemCount = 0;
}

// Queue Copy Constructor function
template <class Elem>
Queue<Elem>::Queue(const Queue&OtherQueue) :
              MAX_NUM( OtherQueue.MAX_NUM ) // Initialize the constant
{
  Beginning = OtherQueue.Beginning;
  End      = OtherQueue.End;
  ElemCount = OtherQueue.ElemCount;

  Data     = new Elem[MAX_NUM + 1];
  for (int i = 0; i < MAX_NUM;i++)
    Data[i] =OtherQueue.Data[i];
}

// Queue Destructor function
template <class Elem>
Queue<Elem>::~Queue(void)
{
  delete[] Data;
}

// Enqueue() function
template <class Elem>
void Queue<Elem>::Enqueue(constElem &Item)
{
  // Error Check: Make sure we aren'texceeding our maximum storage space
  assert( ElemCount < MAX_NUM);

  Data[ End++ ] = Item;
  ++ElemCount;

  // Check for wrap-around
  if (End > MAX_NUM)
    End -= (MAX_NUM +1);
}

// Dequeue() function
template <class Elem>
ElemQueue<Elem>::Dequeue(void)
{
  // Error Check: Make sure we aren'tdequeueing from an empty queue
  assert( ElemCount > 0);

  Elem ReturnValue = Data[ Beginning++];
  --ElemCount;

  // Check for wrap-around
  if (Beginning >MAX_NUM)
    Beginning -= (MAX_NUM +1);

  return ReturnValue;
}

// ElemNum() function
template <class Elem>
inline intQueue<Elem>::ElemNum(void)
{
  return ElemCount;
}

#endif
原创粉丝点击