#ifndef CIRCULAR_QUEUE_H
#define CIRCULAR_QUEUE_H
template <class T> class CircularQueue
{
public:
CircularQueue():cq(NULL), front(-1), rear(-1), mCount(0)
{
}
virtual ~CircularQueue()
{
}
bool Init(int count)
{
mCount=count;
cq = new T[count];
if (NULL == cq) {
return false;
}
return true;
}
bool IsEmpty()
{
if (front==rear)
return true;
return false;
}
bool IsFull()
{
if (((front+1)%mCount) == rear)
return true;
if ((rear== -1) && ((front+1) == mCount)) {
return true;
}
return false;
}
bool set(int value) {
if(IsFull()) return false;
front = (front+1)%mCount;
cq[front] = value;
return true;
}
bool get(int &value) {
if(IsEmpty()) return false;
rear = (rear+1)%mCount;
value = cq[rear];
return true;
}
private:
T *cq;
int front;
int rear;
int mCount;
};
#endif //#ifndef CIRCULAR_QUEUE_H
_________________________________________________________________________________
#include <iostream>
#include <string>
#include "CircularQueue.hpp"
#define MAX_COUNT_ 5
void Log_(bool flag, char* msg)
{
if (true == flag)
{
std::cout<<"SUCCESS In "<<msg<<std::endl;
}
else {
std::cout<<"FAILURE In "<<msg<<std::endl;
}
}
int main_()
{
CircularQueue<long> cq;
bool flag = false;
int value = 0;
flag = cq.Init(MAX_COUNT);
Log(flag, "Init CQ with Int");
flag = cq.IsEmpty();
Log(flag, "Queue is empty");
flag = cq.IsFull();
Log(flag, "Queue is full");
flag = cq.set(5);
Log(flag, "set 5");
flag = cq.set(3);
Log(flag, "set 3");
flag = cq.set(4);
Log(flag, "set 4");
flag = cq.IsEmpty();
Log(flag, "Queue is empty");
flag = cq.IsFull();
Log(flag, "Queue is full");
flag = cq.set(1);
Log(flag, "set 1");
flag = cq.set(2);
Log(flag, "set 2");
flag = cq.IsEmpty();
Log(flag, "Queue is empty");
flag = cq.IsFull();
Log(flag, "Queue is full");
flag = cq.set(6);
Log(flag, "set 1");
flag = cq.get(value);
Log(flag, "get ");
flag = cq.get(value);
Log(flag, "get ");
flag = cq.get(value);
Log(flag, "get ");
flag = cq.get(value);
Log(flag, "get ");
flag = cq.get(value);
Log(flag, "get ");
flag = cq.get(value);
Log(flag, "get ");
return 0;
}
#define CIRCULAR_QUEUE_H
template <class T> class CircularQueue
{
public:
CircularQueue():cq(NULL), front(-1), rear(-1), mCount(0)
{
}
virtual ~CircularQueue()
{
}
bool Init(int count)
{
mCount=count;
cq = new T[count];
if (NULL == cq) {
return false;
}
return true;
}
bool IsEmpty()
{
if (front==rear)
return true;
return false;
}
bool IsFull()
{
if (((front+1)%mCount) == rear)
return true;
if ((rear== -1) && ((front+1) == mCount)) {
return true;
}
return false;
}
bool set(int value) {
if(IsFull()) return false;
front = (front+1)%mCount;
cq[front] = value;
return true;
}
bool get(int &value) {
if(IsEmpty()) return false;
rear = (rear+1)%mCount;
value = cq[rear];
return true;
}
private:
T *cq;
int front;
int rear;
int mCount;
};
#endif //#ifndef CIRCULAR_QUEUE_H
_________________________________________________________________________________
#include <iostream>
#include <string>
#include "CircularQueue.hpp"
#define MAX_COUNT_ 5
void Log_(bool flag, char* msg)
{
if (true == flag)
{
std::cout<<"SUCCESS In "<<msg<<std::endl;
}
else {
std::cout<<"FAILURE In "<<msg<<std::endl;
}
}
int main_()
{
CircularQueue<long> cq;
bool flag = false;
int value = 0;
flag = cq.Init(MAX_COUNT);
Log(flag, "Init CQ with Int");
flag = cq.IsEmpty();
Log(flag, "Queue is empty");
flag = cq.IsFull();
Log(flag, "Queue is full");
flag = cq.set(5);
Log(flag, "set 5");
flag = cq.set(3);
Log(flag, "set 3");
flag = cq.set(4);
Log(flag, "set 4");
flag = cq.IsEmpty();
Log(flag, "Queue is empty");
flag = cq.IsFull();
Log(flag, "Queue is full");
flag = cq.set(1);
Log(flag, "set 1");
flag = cq.set(2);
Log(flag, "set 2");
flag = cq.IsEmpty();
Log(flag, "Queue is empty");
flag = cq.IsFull();
Log(flag, "Queue is full");
flag = cq.set(6);
Log(flag, "set 1");
flag = cq.get(value);
Log(flag, "get ");
flag = cq.get(value);
Log(flag, "get ");
flag = cq.get(value);
Log(flag, "get ");
flag = cq.get(value);
Log(flag, "get ");
flag = cq.get(value);
Log(flag, "get ");
flag = cq.get(value);
Log(flag, "get ");
return 0;
}
No comments:
Post a Comment