queueADT를 이용한 간단한 응용프로그램
queueADT.h
template<typename TYPE>
struct NODE
{
TYPE data;
NODE<TYPE>* next;
};
template<typename TYPE>
class Queue
{
private:
NODE<TYPE>* front;
NODE<TYPE>* rear;
int count;
public:
Queue();
~Queue();
bool EnQueue(TYPE dataln);
bool DeQueue(TYPE& dataOut);
int QueueCount();
bool QueueFront(TYPE& dataOut);
bool QueueRear(TYPE& dataOut);
bool EmptyQueue();
bool FullQueue();
};
template<typename TYPE>
Queue<TYPE>::Queue()
{
front =NULL;
rear = NULL;
count = 0;
}
template<typename TYPE>
Queue<TYPE>::~Queue()
{
NODE<TYPE>* deletePtr;
while(front!=NULL)
{
deletePtr = front;
front=front->next;
delete deletePtr;
}
}
template<typename TYPE>
bool Queue<TYPE>::EnQueue(TYPE dataln)
{
NODE<TYPE>* newPtr;
newPtr = new NODE<TYPE>;
if(newPtr==NULL) //메모리 FULL
{
return false;
}
newPtr->data = dataln;
newPtr->next = NULL;
if(count ==0)
{
front = newPtr;
}
else
{
rear->next = newPtr;
}
count++;
rear = newPtr;
return true;
}
template<typename TYPE>
bool Queue<TYPE>::DeQueue(TYPE& dataOut)
{
NODE<TYPE>* deleteLoc;
if(count==0)
{
return false;
}
dataOut=front->data;
deleteLoc = front;
if(count==1)
{
rear= NULL;
}
front = front->next;
count--;
delete deleteLoc;
return true;
}
template<typename TYPE>
int Queue<TYPE>::QueueCount()
{
return count;
}
template<typename TYPE>
bool Queue<TYPE>::QueueFront(TYPE& dataOut)
{
if(count==0)
{
return false;
//underflow
}
else
{
dataOut = front->data;
return true;
}
}
template<typename TYPE>
bool Queue<TYPE>::QueueRear(TYPE& dataOut)
{
if(count==0)
{
return false;
}
else
{
dataOut = rear->data;
return true;
}
}
template<typename TYPE>
bool Queue<TYPE>::EmptyQueue()
{
return(count==0);
}
template<typename TYPE>
bool Queue<TYPE>::FullQueue()
{
NODE<TYPE>* tempPtr;
temp = new NODE<TYPE>;
if(temp!=NULL)
{
delete temp;
return false;
}
return true;
}
main.cpp
#include <iostream>
#include "queueADT.h"
#include <iomanip>
#include <string>
#include <ctime> // 랜덤변수를 넣기 위해
using namespace std;
void fillQueues(Queue<int>& q0to9, Queue<int>& q10to19, Queue<int>& q20to29, Queue<int>& qOver29);
void printQueues(Queue<int>& q0to9, Queue<int>& q10to19, Queue<int>& q20to29, Queue<int>& qOver29);
void printQueue(Queue<int>& anyQueue);
int main()
{
Queue<int> q0to9;
Queue<int> q10to19;
Queue<int> q20to29;
Queue<int> qOver29;
fillQueues(q0to9, q10to19, q20to29, qOver29);
printQueues(q0to9, q10to19, q20to29, qOver29);
//printQueues(anyQueue);
return 0;
}
void fillQueues(Queue<int>& q0to9, Queue<int>& q10to19, Queue<int>& q20to29, Queue<int>& qOver29)
{
srand(time(0));
// srand(time(0)); // 씨앗(현재시간을 기본으로)
int rnum;
int category;
for(int i=0; i<25; i++)
{
rnum = rand() % 51; // 랜덤 0~50 으로 고정하기 위해 %51로 나머지 연산자 사용.
category = rnum/10; // 0~50의 수의 categorization
cout<< setw(3) << rnum;
switch(category)
{
case 0:
q0to9.EnQueue(rnum);
break;
case 1:
q10to19.EnQueue(rnum);
break;
case 2:
q20to29.EnQueue(rnum);
break;
default :
qOver29.EnQueue(rnum);
break;
}
}
}
void printQueues(Queue<int>& q0to9, Queue<int>& q10to19, Queue<int>& q20to29, Queue<int>& qOver29)
{
cout<<endl;
cout<<endl;
cout<< "DATA 0~9 : " << endl;
printQueue(q0to9);
cout<< "DATA 10~19 : " << endl;
printQueue(q10to19);
cout<< "DATA 20~29 : " << endl;
printQueue(q20to29);
cout<< "DATA OVER : " << endl;
printQueue(qOver29);
}
void printQueue(Queue<int>& anyQueue)
{
int itemp=0;
int data;
while(anyQueue.DeQueue(data))
{
if(itemp++ >=10)
{
itemp=1;
cout<<endl;
}
cout << setw(3)<<data;
}
cout<<endl;
}
'1. IT Story > Development' 카테고리의 다른 글
ChatClient (0) | 2012.03.29 |
---|---|
MultiChatServer (0) | 2012.03.29 |
MultiChatClient (0) | 2012.03.29 |
큐를 이용한 간단한 구직Pro (0) | 2012.03.29 |
circularAry 큐를 사용하여 복사하기 (0) | 2012.03.29 |
StackADT를 이용한 중위식구하기 (0) | 2012.03.29 |
배열로 구현한 Stack을 이용한 중위식구하기 (0) | 2012.03.29 |
seqSearch (0) | 2012.03.29 |