neka
全部博文(8)
2008年(8)
分类: C/C++
2008-07-22 11:27:48
#ifndef __QUEUE_H__#define __QUEUE_H__template <class T>class Queue{public: Queue(int MaxQueueSize=10); ~Queue() { delete [] queue; } bool IsEmpty() const { return front == rear; } bool IsFull() { return ((rear+1) % MaxSize == front ? true : false); } 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; return queue[(front+1) % MaxSize];}template <class T>T Queue<T>::Last() const{ if (IsEmpty()) throw; return queue[rear];}template <class T>Queue<T>& Queue<T>::Add(const T& x){ if (IsFull()) throw; rear = (rear+1) % MaxSize; queue[rear] = x; return *this;}template<class T>Queue<T>& Queue<T>::Delete(T& x){ if (IsEmpty()) throw; front = (front + 1) % MaxSize; x = queue[front]; return *this;}#endif
上一篇:数据结构算法与应用-C++语言描述笔记(线性表)
下一篇:http 头信息详解
登录 注册