模板实现一个通用栈
2011-06-20 16:04
323 查看
模板实现一个通用栈
template <class T,int SIZE> class CArrayStackTemp
{
public:
CArrayStackTemp () //缺省构造函数,构造一个空堆栈
{
top= -1;
};
~ CArrayStackTemp (){};//析构函数
void SetEmpty (); //置空堆栈
bool IsEmpty(); //判断堆栈是否为空
bool Push(T element); //入栈
bool Pop(T& element);//出栈
private:
T Buffer[SIZE];
int top;
};
与堆栈的基本操作相对应的成员函数的实现如下:
template <class T, int SIZE> void CArrayStackTemp<T, SIZ
E>:: SetEmpty ()
{
top= -1; //将栈顶指针赋 -1,并不实际清除数组元素
}
template <class T, int SIZE> bool CArrayStackTemp<T, SIZ
E>:: IsEmpty ()
{
return(top == -1);
}
template <class T, int SIZE> bool CArrayStackTemp<T, SIZ
E>:: Push (T element)
{
top++;
if (top>SIZE-1)
{
top--;
return false; //堆栈已满,不能执行入栈操作
}
Buffer[top]=element;
return true;
}
template <class T, int SIZE> void CArrayStackTemp<T, SIZ
E>:: Pop (T& element)
{
if (IsEmpty())
return false;
element =Buffer[top];
top--;
return true;
}
根据实际需要,还可以扩充堆栈功能。例如:加入取栈顶元素、求
堆栈长度等操作,其方法如上。
通用链栈的实现
模板类中允许使用指针和定义自己的结构,这就为实现链式结构
提供了保证。这里采用一个单链表来实现堆栈,栈顶指针指向链表的
第一个结点,入栈和出栈均在链表的头进行。该模板类的定义如下:
template <class T> class CLinkStackTemp
{
public:
//类的缺省构造函数,生成一个空堆栈
CLinkStackTemp ()
{
top=NULL;
};
~ClinkStackTemp(){}; //析构函数
//定义结点结构
struct node
{
T
data; //入栈元素
node* next; //指向下一结点的指针
};
void SetEmpty(); //置空堆栈
bool IsEmpty(); //判断堆栈是否为空
bool Push(T element); //压入堆栈
bool Pop(T& element);//弹出堆栈
private:
node* top;
};
该类的成员函数实现如下:
template <class T> void CLinkStackTemp <T>::SetEmpty()
{
//释放堆栈占用的内存
node* temp;
while (top!=NULL)
{
temp=top;
top=top->next;
delete temp;
}
}
template <class T> bool CLinkStackTemp <T>::IsEmpty()
{
return (top==NULL);
}
template <class T> bool CLinkStackTemp <T>::Push(T element)
{
node* temp=new node();
if (temp ==NULL)
return false ;
temp->data=element;
temp->next=top;
top=temp;
return true;
}
template <class T> bool CLinkStackTemp <T>::Pop(T& element)
{
if ( IsEmpty())
return false;
node* q = top;
element = top->data;
top=top->next;
delete q;
return true;
}
template <class T,int SIZE> class CArrayStackTemp
{
public:
CArrayStackTemp () //缺省构造函数,构造一个空堆栈
{
top= -1;
};
~ CArrayStackTemp (){};//析构函数
void SetEmpty (); //置空堆栈
bool IsEmpty(); //判断堆栈是否为空
bool Push(T element); //入栈
bool Pop(T& element);//出栈
private:
T Buffer[SIZE];
int top;
};
与堆栈的基本操作相对应的成员函数的实现如下:
template <class T, int SIZE> void CArrayStackTemp<T, SIZ
E>:: SetEmpty ()
{
top= -1; //将栈顶指针赋 -1,并不实际清除数组元素
}
template <class T, int SIZE> bool CArrayStackTemp<T, SIZ
E>:: IsEmpty ()
{
return(top == -1);
}
template <class T, int SIZE> bool CArrayStackTemp<T, SIZ
E>:: Push (T element)
{
top++;
if (top>SIZE-1)
{
top--;
return false; //堆栈已满,不能执行入栈操作
}
Buffer[top]=element;
return true;
}
template <class T, int SIZE> void CArrayStackTemp<T, SIZ
E>:: Pop (T& element)
{
if (IsEmpty())
return false;
element =Buffer[top];
top--;
return true;
}
根据实际需要,还可以扩充堆栈功能。例如:加入取栈顶元素、求
堆栈长度等操作,其方法如上。
通用链栈的实现
模板类中允许使用指针和定义自己的结构,这就为实现链式结构
提供了保证。这里采用一个单链表来实现堆栈,栈顶指针指向链表的
第一个结点,入栈和出栈均在链表的头进行。该模板类的定义如下:
template <class T> class CLinkStackTemp
{
public:
//类的缺省构造函数,生成一个空堆栈
CLinkStackTemp ()
{
top=NULL;
};
~ClinkStackTemp(){}; //析构函数
//定义结点结构
struct node
{
T
data; //入栈元素
node* next; //指向下一结点的指针
};
void SetEmpty(); //置空堆栈
bool IsEmpty(); //判断堆栈是否为空
bool Push(T element); //压入堆栈
bool Pop(T& element);//弹出堆栈
private:
node* top;
};
该类的成员函数实现如下:
template <class T> void CLinkStackTemp <T>::SetEmpty()
{
//释放堆栈占用的内存
node* temp;
while (top!=NULL)
{
temp=top;
top=top->next;
delete temp;
}
}
template <class T> bool CLinkStackTemp <T>::IsEmpty()
{
return (top==NULL);
}
template <class T> bool CLinkStackTemp <T>::Push(T element)
{
node* temp=new node();
if (temp ==NULL)
return false ;
temp->data=element;
temp->next=top;
top=temp;
return true;
}
template <class T> bool CLinkStackTemp <T>::Pop(T& element)
{
if ( IsEmpty())
return false;
node* q = top;
element = top->data;
top=top->next;
delete q;
return true;
}
相关文章推荐
- 【8.17】建立一个用来实现求3个数和的类模板(将成员函数定义在类模板的内部),并写出调用此类模板的完整程序。
- 中级JavaScript例子, 如何实现一个简单实用的模板套用机制, GXTemplate , 第一章(估计要写9章)
- 中级JavaScript例子, 如何实现一个简单实用的模板套用机制, GXTemplate , 第2章(估计要写9章)
- 实现一个Hibernate模板
- C++ 智能指针的一个实现(基于模板和Shared_ptr)
- C++模板来实现一个通用的内存池.
- 将任意类型映射到一个唯一整数(C++模板实现TypeList)
- 一个简洁的归并排序实现模板
- 一个Javascript模板引擎的实现和优化
- 一个简单的Java模板工具类(二)—简单表达式解析实现
- 实现将student中的所有信息在一个模板页上显示,格式参考课件中的图片。并实现编辑功能
- 一个不错的模板Heap实现及Heap排序的实现
- C++ 模板应用 实现一个Queue 队列
- 写一个【栈】模板,并实现“任意进制转换器”
- 【python】简单实现一个模板引擎
- C++ 使用模板实现一个List的实例
- 模板 traits的一个经典例子 , 解决 void func(T t)根据t的类型不同来做不同的实现
- Prototype功能预览八:一个注解实现短信邮件内容模板
- 一个利用CAN实现车辆控制的简单模板
- [转载]Juicer – 一个Javascript模板引擎的实现和优化