队列(数组形式)实现_c++
2014-04-16 20:56
453 查看
template<class T> class Queue{ friend ostream& operator<< <T> (ostream&, const Queue<T>&); public: Queue(int maxcount = 10):head(0),tail(0), count(maxcount) { data = new T[count](); } Queue(const Queue& rhs):head(rhs.head), tail(rhs.tail), count(rhs.count) { data = new T[count](); memcpy(data, rhs.data, sizeof(T)*count); } Queue& operator=(const Queue& rhs) { if (this == &rhs) { return *this; } head = rhs.head; tail = rhs.tail; count = rhs.count; delete []data; data = new T[count](); memcpy(data, rhs.data, sizeof(T)*count); return *this; } ~Queue() { delete []data; } T& Front() const; void Push(const T&); void Pop(); size_t Size() const; inline bool Empty() const; inline bool Full() const; private: int head; int tail; int count; T* data; }; template<class T> T& Queue<T>::Front() const { if (Empty()) { exit(1); } return data[head]; } template<class T> void Queue<T>::Push(const T& val) { if (!Full()) { data[tail] = val; tail = (tail+1)%count; } } template<class T> void Queue<T>::Pop() { if (!Empty()) { head = (head+1)%count; } } template<class T> size_t Queue<T>::Size() const { return (tail-head+count)%count; } template<class T> bool Queue<T>::Empty() const { return head == tail; } template<class T> bool Queue<T>::Full() const { return (tail+1)%count == head; } template<class T> ostream& operator<<(ostream& os, const Queue<T>& q) { if (!q.Empty()) { for (int i = q.head; i != q.tail; i = (i+1)%q.count) { os<<q.data[i]<<' '; } } return os; }
相关文章推荐
- c#与C++类型转换
- C++继承与派生 笔记
- boost::asio学习之[六]简单聊天程序
- C/C++ return返回值的问题
- 北大OJ百练——4074:积水量(C语言)
- 北大OJ百练——4074:积水量(C语言)
- c++中new和delete的使用方法
- 栈(数组形式)实现_c++
- QT加载muPDF静态库的问题和解决(导入C语言静态库,QT的运行时库设定)
- [C++]inline函数
- 网友gforceca1900关于C++11的评论
- C++连接SQL数据库分步骤进行
- VC++动态链接库(DLL)编程深入浅出(zz)
- 头文件string与string.h的区别
- C++ 虚函数表解析
- 【跟着贺老师做C++项目学习】WEEK1日期结构体
- c++学习总结(第五篇)
- C++中各种类型的成员变量的初始化方法(ZZ)
- C语言字符串翻转
- C语言谜题解析