?? 隊列.txt
字號:
//?éà?ó?????±í
template <class Elem> class Link
{
public:
Elem element;
Link *next;
Link(const Elem& elemval, Link *nextval=NUll)
{element=elemval; next=nextval;}
Link( Link *nextval=NUll)
{ next=nextval;}
};
template <class Elem> class LQueue
{
private:
Link <Elem>* front;
Link<Elem>* rear;
int size;
public:
LQueue()
{front=rear=0;size=0;}
~LQueue(){clear();}
//á′ê??óáDààμ???3y
void clear();
//á′ê??óáDààμ?é?3y?a??
bool enqueue(Elem& );
//á′ê??óáDààμ?ìí?ó?a??
bool dequeue(Elem&);
//á′ê??óáDààμ?í·?úμ??a??
bool frontValue(Elem&)const;
//3¤?è
int length() const;
};
//á′ê??óáDààμ???3y
template <class Elem>
void LQueue<Elem>::clear(){
while(front!=0){
rear=front;
front=front->next;
delete rear;
}
rear=0; size=0;
}
//á′ê??óáDààμ?ìí?ó?a??
template <class Elem>
bool LQueue<Elem>::enqueue(Elem& it){
if(rear==0)
front=rear=new Link<Elem>(it,0);
else{
rear->next=new Link<Elem>(it,0);
rear=rear->next;
}
size++; return true;
}
//á′ê??óáDààμ?é?3y?a??
template <class Elem>
bool LQueue<Elem>::dequeue(Elem& it)
{
if(size==0)return false;
it=front->element;
Link<Elem>*ltemp=front;
front=front->next;
delete ltemp;
if(front==0)rear=0;
size--; return true;
}
//á′ê??óáDààμ?í·?úμ??a??
template <class Elem>
bool LQueue<Elem>::frontValue(Elem& it) const
{
if(size==0) return false;
it=front->element;
return true;
}
//3¤?è
template <class Elem>
int LQueue<Elem>::length()const
{return size;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -