template<class T>
class MyStack//链栈
{
private:struct StackNode{T data;StackNode* next;StackNode(const T& val = T(), StackNode* p = nullptr) :data(val), next(p) {}//};StackNode* top;int cursize;void clone(const MyStack& s){Clear();cursize = s.cursize;StackNode* p = s.top;if (p == nullptr)return;top = new StackNode(p->data);StackNode* tail = top;p = p->next;while (p != nullptr){tail=tail->next = new StackNode(p->data);p = p->next;}}
public:MyStack():cursize(0), top(nullptr) {}MyStack(const MyStack& s) :top(nullptr), cursize(s.cursize){clone(s);}MyStack& operator=(const MyStack& s){if (this != &s){clone(s);}return *this;}~MyStack(){Clear();}void Clear(){while (top != nullptr){StackNode* q = top;top = q->next;delete q;}cursize = 0;}int Size()const{return cursize;}bool Empty()const{return Size() == 0;}void Push(const T& val)//入栈{top = new StackNode(val, top);//头插cursize += 1;}const T& Top()const//返回栈顶元素{return top->data;}void Pop()//出栈{StackNode* q = top;top = q->next;delete q;cursize -= 1;}bool GetTop(T& val)//得到栈顶元素并出栈{if (Empty())return false;val = top->data;StackNode* q = top;top = q->next;delete q;cursize -= 1;return true;}};int main()
{MyStack<char>cmys;MyStack<int>imys;MyStack<double> dmys;for (char c = 'a'; c < 'z'; c++){cmys.Push(c);}char val;while (cmys.GetTop(val)){cout << val << endl;}return 0;
}