StaticQueue
生活随笔
收集整理的這篇文章主要介紹了
StaticQueue
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 1 StaticQueue實(shí)現(xiàn)
- 1.1 隊(duì)列的順序?qū)崿F(xiàn)
- 1.2 StaticQueue設(shè)計(jì)要點(diǎn)
- 1.3 StaticQueue實(shí)現(xiàn)要點(diǎn)(循環(huán)計(jì)數(shù)法)
- 2 StaticQueue實(shí)現(xiàn)
- 3 小結(jié)
1 StaticQueue實(shí)現(xiàn)
1.1 隊(duì)列的順序?qū)崿F(xiàn)
1.2 StaticQueue設(shè)計(jì)要點(diǎn)
類(lèi)模板
- 使用原生數(shù)組作為隊(duì)列的存儲(chǔ)空間。
- 使用模板參數(shù)決定隊(duì)列的最大容量。
1.3 StaticQueue實(shí)現(xiàn)要點(diǎn)(循環(huán)計(jì)數(shù)法)
關(guān)鍵操作:
- 進(jìn)隊(duì)列:m_space[m_rear] = e; m_rear = (m_rear + 1) % N;
- 出隊(duì)列:m_front = (m_front + 1) % N;
隊(duì)列的狀態(tài):
- 對(duì)空:(m_length == 0) && (m_front == m_rear)
- 對(duì)滿:(m_length == N) && (m_front == m_rear)
2 StaticQueue實(shí)現(xiàn)
StaticQueue.h
#ifndef STATICQUEUE_H #define STATICQUEUE_H#include "Queue.h" #include "Exception.h"namespace LemonLib { template <typename T, int N> class StaticQueue : public Queue<T> { protected:T m_space[N];int m_front;int m_rear;int m_length;public:StaticQueue(){m_length = 0;m_front = 0;m_rear = 0;}int capacity() const{return N;}void add(const T& e){if (m_length < N){m_space[m_rear] = e;m_rear = (m_rear + 1) % N;m_length++;}else{THROW_EXCEPTION(InvalidOperationException, "No space in current queue ...");}}void remove(){if (m_length > 0){m_front = (m_front + 1) % N;m_length--;}else{THROW_EXCEPTION(InvalidOperationException, "No element in current queue ...");}}T front() const{if (m_length > 0){return m_space[m_front];}else{THROW_EXCEPTION(InvalidOperationException, "No element in current queue ...");}}void clear(){m_length = 0;m_front = 0;m_rear = 0;}int length() const{return m_length;}~StaticQueue(){clear();} }; }#endif // STATICQUEUE_H3 小結(jié)
- 隊(duì)列是一種特殊的線性表,具有先進(jìn)先出的特性
- 隊(duì)列只允許在線性表的兩端進(jìn)行操作,一端進(jìn),一端出
- StaticQueue使用原聲數(shù)組作為內(nèi)部存儲(chǔ)空間
- StaticQueue的最大容量由模板參數(shù)決定
- StaticQueue采用循環(huán)計(jì)數(shù)法提高隊(duì)列操作的效率
總結(jié)
以上是生活随笔為你收集整理的StaticQueue的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: win8怎么链接宽带连接不上网络连接 w
- 下一篇: LinkQueue