#include "stdafx.h"
#include <iostream>
using namespace std;
const int EXPLENGHT=20;
const int STACK_INIT_SIZE=20;
const int STACK_INCRMENT=10;
template<typename ElemType>
class Stack
{
public:
Stack()
{
m_base=(ElemType*)malloc(STACK_INIT_SIZE*sizeof(ElemType));
if(!m_base)
exit(0);
m_top=m_base;
m_size=STACK_INIT_SIZE;
m_item=0;
}
~Stack()
{
free(m_base);
m_base=m_top=NULL;
}
void push(ElemType e)
{
if(m_top-m_base>=m_size)
{
m_base=(ElemType*)realloc(m_base,(m_size+STACK_INCRMENT)*sizeof(ElemType));
if(!m_base)
exit(0);
m_top=m_base+m_size;
m_size+=STACK_INCRMENT;
}
*(m_top)=e;
m_top++;
m_item++;
}
void pop(ElemType &e)
{
if(m_top==m_base)
return;
m_top--;
e=*m_top;
m_i
栈应用——表达式求值
最新推荐文章于 2024-10-23 15:28:59 发布