64,676
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <cstring>
using namespace std;
template <typename T>
class Stack
{
public:
void pop();
void push(T data);
private:
T *top;
T *bottom;
public:
Stack();
~Stack();
};
template <typename T>
Stack<T>::Stack()
{
bottom=new T;
top=bottom;
*bottom=-1;
cout<<endl<<"Construct New Stack"<<endl;
}
template <typename T>
Stack<T>::~Stack()
{
if (top!=bottom)
top=bottom;
delete bottom;
cout<<endl<<"Release this Stack"<<endl;
}
template <typename T>
void Stack<T>::push(T data)
{
*(++top)=data;
}
template <typename T>
void Stack<T>::pop()
{
if (top==bottom)
cout<<endl<<"The Bottom Data In This Stack Is :"<<*top<<endl;
else
cout<<endl<<"The Current Data In Stack Top Is "<<*(top--)<<endl;
}
void main()
{
Stack<int> stack1;
stack1.push(1);
stack1.push(2);
stack1.push(3);
stack1.pop();
stack1.pop();
stack1.pop();
}
bottom=new T[100]
...
delete []bottom;
;