チェーンキューの実装(c++)
1934 ワード
// :vs2010
// ,
#include
using namespace std;
template
struct Node //
{
DataType data;
Node* next;
};
template
class LinkQueue
{
public:
LinkQueue();//
~LinkQueue();//
void EnQueue(DataType x);//x
DataType DeQueue( ); //
DataType GetQueue( );//
bool Empty( );//
private:
Node* front;// , ,
Node* rear;// ,
};
template
LinkQueue::LinkQueue()
{
front=new Node;
front->next =NULL;
rear=front;
}
template
LinkQueue::~LinkQueue()
{
while(front)
{
Node* p=front;
front=front->next ;
delete p;
}
}
template
void LinkQueue::EnQueue(DataType x)
{
Node* s=new Node;
s->data =x;
s->next =NULL;
rear->next =s;//
rear=s;//
}
template
DataType LinkQueue::DeQueue( )
{
if(Empty())throw " , ";
Node* p=front->next ;
DataType x=p->data ;
front->next =p->next ;//
if(p->next ==NULL)rear=front;//
delete p;//
return x;//
}
template
DataType LinkQueue::GetQueue( )
{
if(Empty())throw " , ";
return front->next ->data ;
}
template
bool LinkQueue::Empty( )
{
if(front==rear)return true;
else return false;
}
void main()
{
LinkQueue mylinkqueue;
int a[]={1,2,3,4,5};
for(int i=0;i<5;i++)
{
cout<