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

📄 lstack.h

📁 不带括号算术式求值另外一个版本 可以自动判断优先级 排除空格等字符影响
💻 H
字号:
#ifndef LLSTACK_H
#define LLSTACK_H

#include "link.h"
#include "stack.h"

template<class Elem>
class LStack :public Stack<Elem>
{
	private:
		Link<Elem> *top;
    public:
        LStack()
		{
			top=NULL;
		}
        ~LStack()
		{
			clear();
		}

        bool empty() const;
	    void clear();
        bool push(const Elem &item);
        bool pop();
        bool topValue(Elem &item) const;
};

template<class Elem>
bool LStack<Elem>::empty() const
{
  if(top==NULL)
     return 1;
  else
     return 0;
}

template<class Elem>
void LStack<Elem>::clear() 
{
   while (!empty())
      pop();
}

template<class Elem>
bool LStack<Elem>::push(const Elem &item)
{
   Link<Elem> *new_top = new Link<Elem>(item, top);
   if (new_top == NULL) return 0;
   top = new_top;
   return 1;
}


template<class Elem>
bool LStack<Elem>::pop()
{
   Link<Elem> *old_top = top;
   if (top == NULL) return 0;
   top = old_top->next;
   delete old_top;
   return 1;
}

template<class Elem>
bool LStack<Elem>::topValue(Elem &item) const
{
   if(empty())
     return 0;
   else{
     item=top->element;
     return 1;
   }
}

#endif

⌨️ 快捷键说明

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