선형큐의 단점을 보완한 원형 큐..배열이라 최대값이 정해져있다.
그래서 다음에 나오는것이 연결리스트로 구현한 원형 큐..
#include<iostream>
#include<conio.h>
using namespace std;
class CircleQueueNode
{
public:
int data;
public:
CircleQueueNode(int num=0)
{
data=num;
}
};
class CircleQueue
{
public:
int CurrentCount;
int MaxCount;
int Front;
int Rear;
CircleQueueNode* pElment;
public:
CircleQueue(int max)
{
CurrentCount=0;
MaxCount=max;
Front=0;
Rear=0;
pElment=new CircleQueueNode[MaxCount];
}
~CircleQueue()
{
delete [] pElment;
}
bool QueueFull()
{
if(CurrentCount==MaxCount)
return true;
else
return false;
}
void EnQueue(CircleQueueNode node)
{
if(!QueueFull())
{
pElment[Rear]=node;
Rear=(Rear+1)%MaxCount;
cout << Rear << endl;
CurrentCount++;
}
else
{
cout << "큐에 자리가 없음"<< endl;
}
}
void DeQueue()
{
CircleQueueNode* temp;
temp=&pElment[Front];
Front=(Front+1)%MaxCount;
cout << "디큐데이터" << " " << temp->data << endl;
CurrentCount--;
}
void ShowAllData()
{
cout << "리어" << Rear << " " << "프론트" << Front << endl;
cout << "현재값" << CurrentCount << endl;
int Result;
int temp=Front+CurrentCount; ⁄⁄ 4 + 3 = 7 Rear 0
for(int i=Front;i<temp;i++)
{
Result=i%MaxCount;
cout << Result << " " << "데이터" ;
cout << pElment[Result].data << endl;
}
}
};
int main()
{
CircleQueue que(5);
CircleQueueNode a(1), b(2), c(3), d(4), e(5);
que.EnQueue(a);
que.EnQueue(b);
que.EnQueue(c);
que.EnQueue(d);
que.EnQueue(e);
que.ShowAllData();
cout << endl;
que.DeQueue();
que.ShowAllData();
que.DeQueue();
que.DeQueue();
que.DeQueue();
que.ShowAllData();
cout << endl;
que.EnQueue(a);
que.EnQueue(a);
que.ShowAllData();
cout << endl;
getch();
return 0;
}