stack.h

来自「把中缀表达式转换为后缀表达后」· C头文件 代码 · 共 73 行

H
73
字号
#ifndef STACK_H_
#define STACK_H_
#include"Node.h"

/****************************************************************/
template<class Stack_entry>
class Stack{
public:
	Stack();
	bool empty()const;
	Error_code push(const Stack_entry &item);
	Error_code pop();
	Error_code top(Stack_entry &item)const;
	int size();
	~Stack();
protected:
	Node<Stack_entry>*top_node;
	int count;
};

/****************************************************************/
template<class Stack_entry>
Error_code Stack<Stack_entry>::push(const Stack_entry &item){
	Node<Stack_entry> *new_top=new Node<Stack_entry>(item,top_node);
	if(new_top==NULL)
		return overflow;
	top_node=new_top;
	count++;
	return success;
}
/*************************************************************/
template<class Stack_entry>
Error_code Stack<Stack_entry>::pop(){
	Node<Stack_entry> *old_top=top_node;
	if(top_node==NULL)
		return underflow;
	top_node=top_node->next;
	delete old_top;
	count--;
	return success;
}
/*************************************************************/
template<class Stack_entry>
Error_code Stack<Stack_entry>::top(Stack_entry &item)const{
	if(top_node==NULL)
		return underflow;
	item=top_node->entry;
	return success;
}
/*************************************************************/
template<class Stack_entry>
int Stack<Stack_entry>::size(){
	return count;
}
/*************************************************************/
template<class Stack_entry>
Stack<Stack_entry>::Stack(){
	top_node=NULL;
	count=0;
}
/*************************************************************/
template<class Stack_entry>
Stack<Stack_entry>::~Stack(){
	while(!empty())
		pop();
}
/*************************************************************/
template<class Stack_entry>
bool Stack<Stack_entry>::empty()const{
	return top_node==NULL;
}
/*************************************************************/
#endif

⌨️ 快捷键说明

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