#include <iostream>
using namespace std;
template <class DataType>
struct Node {
DataType data;
Node<DataType>* next
};
template <class dataType>
class LinkStack
{
private:
Node<DataType>* top;
public:
LinkStack() { top = 0; } ///构造函数,初始化一个空链表
~LinkStack(); ///析构函数,释放链栈中个结点的存储空间
void Push(DataType x); ///入栈操作,将元素x入栈
DataType Pop(); ///出栈操作,将栈顶元素出栈
DataType GetTop() {
if (top)
return top->data;
} ///取栈顶元素(并不删除)
int Empty() {
if (top == NULL)
return 1;
else
return 0;
} ///判空操作,判断链栈是否为空栈
};
template <class dataType>
void LinkStack<DataType>::Push(DataType x)
{
void LinkStack::Push(DataType x) {
Node* s;
s = new Node; s->data = x; ///申请一个数据域为x的结点
s->next = top; top = s; ///将结点s插在栈顶
}
template <class dataType>
DataType LinkStack<DataType>::Pop() {
DataType LinkStack::Pop() {
Node* p;
if (top == 0)
throw "下溢";
x = top->data; p = top; ///暂存栈顶元素
top = top->next; ///将栈顶结点摘链
delete p;
return x;
}
int main() {
return 0;
}