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

📄 栈.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 LStack
{
private:
    Link<Elem>* top;
    int size;
public:
    LStack()
     {top=0; size=0;}
	~LStack()
	{
		clear();
	} 
//??3y??
  void clear();
 
//?1è???
  bool push(Elem&);
//íê???¥?a??
  bool pop(Elem&);
//??μ?TOPμ??μ
 bool topValue(Elem&)const;
 int length()const;
};



//??3y??
template <class Elem>
void LStack<Elem>::clear()
{
      while(top!=0)
	  {
           Link<Elem>* temp=top;
           top=top->next;
           delete temp;
       }//while
      size=0;
}//clear
//?1è???
template <class Elem>
bool LStack<Elem>::push(Elem& item){
     top=new Link<Elem>(item,top);
      size++; return true;
   }
//è¥???¥?a??
template <class Elem>
  bool LStack<Elem>::pop(Elem& it){
    if(size==0) return false;
    it=top->element;
    Link<Elem>* ltemp=top->next;
    delete top;
     top=ltemp; size--;
     return true;
   }//pop
//??μ?TOPμ??μ
template <class Elem>
bool LStack<Elem>::topValue(Elem& it) const
{
    if(size==0) return false;
    it=top->element;
    return true; 
}
template <class Elem>
 int LStack<Elem>::length()const {return size;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -