連接藉助前面的雙循環鏈表實現ios
#ifndef QUEUE_HPP #define QUEUE_HPP #include <iostream> #include "exceptions.hpp" template <typename T> class Queue { public: Queue(){} virtual ~Queue() {} /** * @brief dequeue 出隊 * @return */ virtual T dequeue() = 0; /** * @brief enqueue 入隊 * @param t */ virtual void enqueue(const T& t) = 0; /** * @brief isEmpty 隊空 * @return */ virtual bool isEmpty() const = 0; /** * @brief traverse 遍歷 * @param s 分割符 * @param os 輸出流 */ virtual void traverse(const std::string &s, std::ostream &os) const = 0; virtual int length() const = 0; // virtual int capacity() const = 0; virtual void clear() = 0; }; #endif // QUEUE_HPP #ifndef CIRCULARSEQQUEUE_HPP #define CIRCULARSEQQUEUE_HPP #include "queue.hpp" template <typename T> /** * @brief The CircularSeqQueue class 循環隊列 */ class CircularSeqQueue : public Queue<T> { public: CircularSeqQueue(int _size = DEFAULTSIZE); ~CircularSeqQueue(); int length() const; T dequeue(); void enqueue(const T &t); bool isEmpty() const; bool isFull() const; void traverse(const std::string &s = std::string(), std::ostream &os = std::cout) const; /** * @brief capacity 順序實現時的數組的長度 * @return */ int capacity() const { return size; } void clear(); private: enum {DEFAULTSIZE = 10}; int size; int front; int tail; T *d_p; }; template <typename T> inline CircularSeqQueue<T>::CircularSeqQueue(int _size): size(_size+1),//循環隊列有一個爲空,來實現空隊和滿隊的區別 front(0), tail(0) { d_p = new T[size]; } template <typename T> CircularSeqQueue<T>::~CircularSeqQueue() { delete[] d_p; } template <typename T> inline bool CircularSeqQueue<T>::isEmpty() const { return front == tail; } template <typename T> inline bool CircularSeqQueue<T>::isFull() const { return (tail+1)%size == front; } template <typename T> inline int CircularSeqQueue<T>::length() const { return (tail - front + size) % size; } template <typename T> void CircularSeqQueue<T>::enqueue(const T &t) { if (isFull()) throw OutOfBoundary(); tail = (tail+1) % size; d_p[tail] = t; } template <typename T> T CircularSeqQueue<T>::dequeue() { if (isEmpty()) throw OutOfBoundary(); front = (front+1) % size; return d_p[front]; } template <typename T> inline void CircularSeqQueue<T>::clear() { front = tail = 0; } template <typename T> void CircularSeqQueue<T>::traverse(const std::string &s, std::ostream &os) const { if (tail >= front) { for (int i = front+1;i <= tail;++i) { os << d_p[i] << s; } } else { for (int i = front+1;i < size;++i) { os << d_p[i] << s; } for (int i = 0;i <= tail;++i) { os << d_p[i] << s; } } } #endif // CIRCULARSEQQUEUE_HPP #ifndef LINKQUEUE_HPP #define LINKQUEUE_HPP #include "queue.hpp" #include "linkList.hpp" template <typename T> /** * @brief The LinkQueue class 鏈隊 */ class LinkQueue : public Queue<T> { public: LinkQueue(); ~LinkQueue(); void enqueue(const T &t); T dequeue(); int length() const { return queue.length(); } bool isEmpty() const; void clear(); void traverse(const std::string &s = std::string(), std::ostream &os = std::cout) const; private: LinkList<T> queue;//藉助雙循環鏈表實現 }; template <typename T> inline LinkQueue<T>::LinkQueue() { } template <typename T> LinkQueue<T>::~LinkQueue() { } template <typename T> inline bool LinkQueue<T>::isEmpty() const { return queue.length() == 0; } template <typename T> /** * @brief LinkQueue<T>::enqueue 入隊 * @param t */ void LinkQueue<T>::enqueue(const T &t) { queue.push_tail(t); } template <typename T> /** * @brief LinkQueue<T>::dequeue 出隊 * @return */ T LinkQueue<T>::dequeue() { return queue.pop_front(); } template <typename T> void LinkQueue<T>::clear() { queue.clear(); } template <typename T> void LinkQueue<T>::traverse(const std::string &/*s*/, std::ostream &os) const { queue.traverse(os);//鏈表的沒加分割符 } #endif // LINKQUEUE_HPP