使用模板类实现Queue
2011-04-13 15:59
357 查看
template<class T>
class Queue
{
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;
T Last()const;
Queue<T>& Add(const T& x);
Queue<T>& Delete(T& x);
private:
int front;
int rear;
int MaxSize;
T * queue;
};
////////////////////////////////////////////
template<class T>
Queue<T>::Queue(int MaxQueueSize)
{
MaxSize=MaxQueueSize+1;
queue=new T[MaxSize];
front=rear=0;
}
template<class T>
T Queue<T>::First() const
{
//if(IsEmpty())throw OutOfBounds();
if(IsEmpty())exit(0);
return queue[(front+1)%MaxSize];
}
template<class T>
T Queue<T>::Last() const
{
//if(IsEmpty())throw OutOfBounds();
if(IsEmpty())exit(0);
return queue[rear];
}
template<class T>
Queue<T>& Queue<T>::Add(const T &x)
{
//if(IsFull())throw NoMem();
if(IsFull())exit(0);
rear=(rear+1)%MaxSize;
queue[rear]=x;
return *this;
}
template<class T>
Queue<T>& Queue<T>::Delete(T &x)
{
if(IsEmpty())throw OutOfBounds();
front=(front+1)%MaxSize;
x=queue[front];
return *this;
}
class Queue
{
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;
T Last()const;
Queue<T>& Add(const T& x);
Queue<T>& Delete(T& x);
private:
int front;
int rear;
int MaxSize;
T * queue;
};
////////////////////////////////////////////
template<class T>
Queue<T>::Queue(int MaxQueueSize)
{
MaxSize=MaxQueueSize+1;
queue=new T[MaxSize];
front=rear=0;
}
template<class T>
T Queue<T>::First() const
{
//if(IsEmpty())throw OutOfBounds();
if(IsEmpty())exit(0);
return queue[(front+1)%MaxSize];
}
template<class T>
T Queue<T>::Last() const
{
//if(IsEmpty())throw OutOfBounds();
if(IsEmpty())exit(0);
return queue[rear];
}
template<class T>
Queue<T>& Queue<T>::Add(const T &x)
{
//if(IsFull())throw NoMem();
if(IsFull())exit(0);
rear=(rear+1)%MaxSize;
queue[rear]=x;
return *this;
}
template<class T>
Queue<T>& Queue<T>::Delete(T &x)
{
if(IsEmpty())throw OutOfBounds();
front=(front+1)%MaxSize;
x=queue[front];
return *this;
}
相关文章推荐
- 【C++ STL应用与实现】13: 如何使用std::queue和std::priority_queue
- Ruby中使用多线程队列(Queue)实现下载博客文章保存到本地文件
- java中使用FIFO队列:java.util.Queue实现多台服务器发邮件的代码
- 使用 MySQL 实现无锁任务队列(using MySQL as a job queue)
- golang使用sync.Cond实现CappedBlockingQueue
- Handler实现与机制 && Blocking Queue && IdleHandler使用
- 如何使用ASINetWorkQueue下载实现
- cpp: 使用类模板以及链表实现队列Queue
- sicily 使用deque实现stack && 使用list实现queue
- 使用List模拟实现STL中的Queue
- CPP(七):priority_queue的实现与大根堆的使用
- 循环队列Queue–使用链式存储结构实现
- 队列(queue)的模板类及其成员函数的实现
- 使用priority_queue 实现数据结构——堆
- java 5并发中的阻塞队列ArrayBlockingQueue的使用以及案例实现
- 使用bsd的queue.h实现简单队列
- 使用WeakReference 与 ReferenceQueue 简单实现弱引用缓存
- 标准类库 queue模板类的简单实现
- 循环队列Queue--使用顺序存储结构(数组)实现
- 使用两个栈(stack)实现一个队列(queue)