Skip to content

Instantly share code, notes, and snippets.

@shibainurou
Last active December 10, 2015 01:38
Show Gist options
  • Save shibainurou/4359906 to your computer and use it in GitHub Desktop.
Save shibainurou/4359906 to your computer and use it in GitHub Desktop.
循環キュークラス
#include <iostream>
#include <conio.h>
using namespace std;
class CCircleQueue
{
private:
const static int MAX = 4;
int value[MAX];
int headIndex;
int tailIndex;
int num;
public:
CCircleQueue();
~CCircleQueue();
void Enqueue(int val);
int Dequeue();
void Show(int val);
void Show();
};
CCircleQueue::CCircleQueue():
headIndex(0)
, tailIndex(0)
, num(0)
{
}
CCircleQueue::~CCircleQueue()
{
}
void CCircleQueue::Enqueue(int val)
{
if( MAX <= num )
throw "maximum limit error";
value[tailIndex] = val;
++num;
++tailIndex;
if( MAX <= tailIndex )
tailIndex = 0;
}
int CCircleQueue::Dequeue()
{
if( num <= 0 )
throw "minimum limit error";
int val = value[headIndex];
--num;
++headIndex;
if( MAX <= headIndex )
headIndex = 0;
return val;
}
void CCircleQueue::Show(int val)
{
cout << value[val] << endl;
}
void CCircleQueue::Show()
{
cout << "All ";
for( int i = 0, index = headIndex; i < num; ++i ){
index = index + i;
if( MAX <= index )
index = 0;
cout << index << ":" << value[index] << " ";
}
cout << endl;
}
int main(int argc, char const *argv[])
{
CCircleQueue cq = CCircleQueue();
try{
cq.Enqueue(1);
cq.Enqueue(3);
cout << cq.Dequeue() << endl;
cout << cq.Dequeue() << endl;
cq.Enqueue(5);
cq.Enqueue(7);
cout << cq.Dequeue() << endl;
cout << cq.Dequeue() << endl;
cq.Enqueue(10);
cout << cq.Dequeue() << endl;
cq.Enqueue(11);
cq.Enqueue(12);
cout << cq.Dequeue() << endl;
cq.Enqueue(14);
cout << cq.Dequeue() << endl;
cout << cq.Dequeue() << endl;
}
catch(const char* errmsg){
cout << errmsg << endl;
}
getch();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment