⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 队列.txt

📁 一些很好用的数据结构
💻 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 + -