市直第三幼儿园:请高手帮忙分析一下这个程序!十万火急!!!

来源:百度文库 编辑:高校问答 时间:2024/04/29 12:13:00
这是一个有关队列的问题!以下模板都是做什么用的!请具体说明一下!
// file queue.h
// formual based queue

#ifndef Queue_
#define Queue_

#include <stdlib.h>
#include <iostream.h>
#include "xcept.h"

template<class T>
class Queue {
// FIFO objects
public:
Queue(int MaxQueueSize = 10);
~Queue() {delete [] queue;}
bool IsEmpty() const
{
return front == rear;
}
bool IsFull() const {return (
((rear + 1) % MaxSize == front) ? 1 : 0);}
T First() const; // return front element
T Last() const; // return last element
Queue<T>& Add(const T& x);
Queue<T>& Delete(T& x);
private:
int front; // one counterclockwise from first
int rear; // last element
int MaxSize; // size of array queue
T *queue; // element array
};

template<class T>
Queue<T>::Queue(int MaxQueueSize)
{// Create an empty queue whose capacity
// is MaxQueueSize.
MaxSize = MaxQueueSize + 1;
queue = new T[MaxSize];
front = rear = 0;
}

template<class T>
T Queue<T>::First() const
{// Return first element of queue. Throw
// OutOfBounds exception if the queue is empty.
if (IsEmpty()) throw OutOfBounds();
return queue[(front + 1) % MaxSize];
}

template<class T>
T Queue<T>::Last() const
{// Return last element of queue. Throw
// OutOfBounds exception if the queue is empty.
if (IsEmpty()) throw OutOfBounds();
return queue[rear];
}

template<class T>
Queue<T>& Queue<T>::Add(const T& x)
{// Add x to the rear of the queue. Throw
// NoMem exception if the queue is full.
if (IsFull()) throw NoMem();
rear = (rear + 1) % MaxSize; queue[rear] = x;
return *this;
}

template<class T>
Queue<T>& Queue<T>::Delete(T& x)
{// Delete first element and put in x. Throw
// OutOfBounds exception if the queue is empty.
if (IsEmpty()) throw OutOfBounds();
front = (front + 1) % MaxSize;
x = queue[front];
return *this;
}

#endif
简单说明即可

程序定义了一个队列模版,可以接受任何数据类型作为队列的元素
定义了四个方法,取第一个元素First,取最后一个元素Last,增加一个元素到队列尾部Add,删除队列前部的一个元素Delete