StaticQueue队列的概念及实现
Posted 阿龙亡命天涯
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了StaticQueue队列的概念及实现相关的知识,希望对你有一定的参考价值。
抽象类无法生成对象
为何用循环计数法?高效!
#ifndef STATICQUEUE_H
#define STATICQUEUE_H
#include "Queue.h"
#include "Exception.h"
namespace DragonLib
template <typename T,int N>
class StaticQueue : public Queue
protected:
T m_space[N];
int m_front;
int m_rear;
int m_length;
public:
StaticQueue()
m_front = 0;
m_rear = 0;
m_length = 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_front = 0;
m_rear = 0;
m_length = 0;
int length() const
return m_length;
;
#endif // STATICQUEUE_H
#include <iostream>
#include "staticqueue.h"
using namespace std;
using namespace DragonLib;
int main()
StaticQueue<int,5> queue;
for(int i=0;i<5;i++)
queue.add(i);
while(queue.length()>0)
cout << queue.front() << endl;
queue.remove();
return 0;
先进先出: 打印0 1 2 3 4
小结
以上是关于StaticQueue队列的概念及实现的主要内容,如果未能解决你的问题,请参考以下文章