#ifndef __QUEUEPRACTICE_H__
#define __QUEUEPRACTICE_H__ 1
namespace FengChen
{
template <class Type> class Queue;
template <class T>
std::ostream& operator<<(std::ostream& os, const Queue<T>& Q)
{
os<<"Queue:<Tail-- ";
for(int i = 0; i < Q.m_Count; i++)
os<<Q.m_Array[ (Q.m_Head - i) % Q.m_Capacity ]<<" ";
if(Q.m_Count < 1) os<<"Empty!";
os<<" --Head>";
return os;
}
template <class Type> class Queue
{
friend std::ostream& operator<< <Type> (std::ostream&, const Queue<Type>&);
public:
Queue(unsigned int Capacity): m_Head(-1), m_Array(0),m_Capacity(Capacity),m_Count(0)
{
if(m_Capacity < 2) throw std::range_error("Invalid Queue Length, too small!");
if( (m_Array = new Type[m_Capacity]) == 0) throw std::runtime_error("Not enough memory!");
}
Queue(): m_Head(-1), m_Array(0),m_Capacity(64),m_Count(0)
{
if( (m_Array = new Type[m_Capacity]) == 0) throw std::runtime_error("Not enough memory!");
}
~Queue()
{
delete [] m_Array;
}
const Type& Head() const
{
if(m_Count < 1) throw std::domain_error("Empty Queue!");
return m_Array[m_Head];
}
void Enqueue(const Type& value)
{
if (m_Count >= m_Capacity) throw std::domain_error("Queue is full!");
m_Array[(++m_Head) % m_Capacity] = value;
m_Count++;
}
void Dequeue(Type& item )
{
if(this->IsEmpty ()) throw std::domain_error("Queue is empty!");
item = m_Array[m_Tail()];
m_Count--;
}
bool IsEmpty(){ return m_Count == 0; }
bool IsFull(){ return m_Count == m_Capacity; }
size_t Count(){ return m_Count; }
private:
unsigned int m_Capacity;
unsigned int m_Count;
Type* m_Array;
int m_Head;
int m_Tail(){ return ( m_Head - m_Count + 1 ) % m_Capacity; }
};
}
#endif
#define __QUEUEPRACTICE_H__ 1
namespace FengChen
{
template <class Type> class Queue;
template <class T>
std::ostream& operator<<(std::ostream& os, const Queue<T>& Q)
{
os<<"Queue:<Tail-- ";
for(int i = 0; i < Q.m_Count; i++)
os<<Q.m_Array[ (Q.m_Head - i) % Q.m_Capacity ]<<" ";
if(Q.m_Count < 1) os<<"Empty!";
os<<" --Head>";
return os;
}
template <class Type> class Queue
{
friend std::ostream& operator<< <Type> (std::ostream&, const Queue<Type>&);
public:
Queue(unsigned int Capacity): m_Head(-1), m_Array(0),m_Capacity(Capacity),m_Count(0)
{
if(m_Capacity < 2) throw std::range_error("Invalid Queue Length, too small!");
if( (m_Array = new Type[m_Capacity]) == 0) throw std::runtime_error("Not enough memory!");
}
Queue(): m_Head(-1), m_Array(0),m_Capacity(64),m_Count(0)
{
if( (m_Array = new Type[m_Capacity]) == 0) throw std::runtime_error("Not enough memory!");
}
~Queue()
{
delete [] m_Array;
}
const Type& Head() const
{
if(m_Count < 1) throw std::domain_error("Empty Queue!");
return m_Array[m_Head];
}
void Enqueue(const Type& value)
{
if (m_Count >= m_Capacity) throw std::domain_error("Queue is full!");
m_Array[(++m_Head) % m_Capacity] = value;
m_Count++;
}
void Dequeue(Type& item )
{
if(this->IsEmpty ()) throw std::domain_error("Queue is empty!");
item = m_Array[m_Tail()];
m_Count--;
}
bool IsEmpty(){ return m_Count == 0; }
bool IsFull(){ return m_Count == m_Capacity; }
size_t Count(){ return m_Count; }
private:
unsigned int m_Capacity;
unsigned int m_Count;
Type* m_Array;
int m_Head;
int m_Tail(){ return ( m_Head - m_Count + 1 ) % m_Capacity; }
};
}
#endif
当然没有STL里面的强大了,不过学习用途也不错。主要是为了学习C++。