[완료] C++ 내포 클래스에 대해서...
#include
using namespace std;
#ifndef QUEUETP_H_
#define QUEUETP_H_
template
class QueueTP
{
private:
enum{Q_SIZE = 10};
class Node
{
public:
Item item;
Node * next;
Node(const Item & i) : item(i), next(0) {}
};
Node * front;
Node * rear;
int items;
const int qsize;
QueueTP(const QueueTP & q) : qsize(0) {}
QueueTP & operator=(const QueueTP & q) {cout << "operator" << endl;return * this;}
public:
QueueTP(int qs = Q_SIZE);
~QueueTP();
bool isempty() const { return items == 0; }
bool isfull() const { return items == qsize; }
int queuecount() const { return items;}
bool enqueue(const Item & item);
bool dequeue(Item & item);
};
template
QueueTP::QueueTP(int qs) : qsize(qs)
{
front = rear = 0;
items = 0;
}
template
QueueTP::~QueueTP()
{
Node * temp;
while(front!=0)
{
temp = front;
front = front->next;
delete temp;
}
}
template
bool QueueTP::enqueue(const Item & item)
{
if(isfull())
return false;
Node * add = new Node(item);
if(add == NULL)
return false;
items++;
if(front ==0)
front = add;
else
rear->next = add;
rear = add;
cout << "add : " << add << endl;
cout << "add->item : " << add->item << endl;
cout << "add->next : " << add->next << endl;
cout << "front : " << front << endl;
cout << "front->item : " << front->item << endl;
cout << "front->next : " << front->next << endl;
cout << "rear : " << rear << endl;
cout << "rear->item : " << rear->item << endl;
cout << "rear->next : " << rear->next << endl << endl << endl;
return true;
}
template
bool QueueTP::dequeue(Item & item)
{
if(front == 0)
return false;
item = front->item;
items--;
Node * temp = fornt;
front = front->next;
delete temp;
if(items == 0)
rear = 0;
return true;
}
#endif
#include"queuetp.h"
#include
using namespace std;
int main()
{
QueueTP sinwho;
sinwho.enqueue(10);
sinwho.enqueue(8);
sinwho.enqueue(6);
return 0;
}
만약 위와 같은 코드가 있고 실행시..
add : 0017ED08
add->item : 10
add->next : 00000000
front : 0017ED08
front->item : 10
front->next : 00000000
rear : 0017ED08
rear->item : 10
rear->next : 00000000
add : 0017EF30
add->item : 8
add->next : 00000000
front : 0017ED08
front->item : 10
front->next : 0017EF30
rear : 0017EF30
rear->item : 8
rear->next : 00000000
add : 0017EF68
add->item : 6
add->next : 00000000
front : 0017ED08
front->item : 10
front->next : 0017EF30
rear : 0017EF68
rear->item : 6
rear->next : 00000000
댓글 달기