Last active
December 10, 2015 01:38
-
-
Save shibainurou/4359906 to your computer and use it in GitHub Desktop.
循環キュークラス
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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