队列.txt

来自「一些很好用的数据结构」· 文本 代码 · 共 87 行

TXT
87
字号
//?éà?ó?????±í
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 + =
减小字号Ctrl + -
显示快捷键?