#include<iostream>
using namespace std;
template<class T>
class my_stack
{
public:
my_stack(int n = 10);
~my_stack();
bool empty()const;
bool full()const;
bool push(const T& element);
bool pop();
bool get_top(T& top)const;
private:
T* a;
int m_size;
int m_max_size;
};
template<class T>
my_stack<T>::my_stack(int n)
{
m_size = 0;
a = new T[n];
if(a == NULL)
{
m_max_size = 0;
}
else
{
m_max_size = n;
}
}
template<class T>
my_stack<T>::~my_stack()
{
delete []a;
}
template<class T>
bool my_stack<T>::empty()const
{
return m_size == 0 ? true : false;
}
template<class T>
bool my_stack<T>::full()const
{
return m_size == m_max_size ? true : false;
}
template<class T>
bool my_stack<T>::push(const T& element)
{
if(full())
{
return false;
}
else
{
a[m_size++] = element;
return true;
}
}
template<class T>
bool my_stack<T>::get_top(T& top)const
{
if(empty())
{
return false;
}
else
{
top = a[m_size-1];
return true;
}
}
template<class T>
bool my_stack<T>::pop()
{
if(empty())
{
return false;
}
else
{
m_size--;
return true;
}
}
int main()
{
my_stack<int> s(20);
//进栈
cout<<"进栈"<<endl;
for(int i = 0; i < 20; i++)
{
s.push(i);
int t = -1;
if(s.get_top(t))
{
cout<<t<<' ';
}
}
cout<<endl;
cout<<"出栈"<<endl;
//出栈
for(int j = 0; j < 20; j++)
{
int t = -1;
if(s.get_top(t))
{
cout<<t<<' ';
}
if(s.pop())
{
}
}
return 0;
}
using namespace std;
template<class T>
class my_stack
{
public:
my_stack(int n = 10);
~my_stack();
bool empty()const;
bool full()const;
bool push(const T& element);
bool pop();
bool get_top(T& top)const;
private:
T* a;
int m_size;
int m_max_size;
};
template<class T>
my_stack<T>::my_stack(int n)
{
m_size = 0;
a = new T[n];
if(a == NULL)
{
m_max_size = 0;
}
else
{
m_max_size = n;
}
}
template<class T>
my_stack<T>::~my_stack()
{
delete []a;
}
template<class T>
bool my_stack<T>::empty()const
{
return m_size == 0 ? true : false;
}
template<class T>
bool my_stack<T>::full()const
{
return m_size == m_max_size ? true : false;
}
template<class T>
bool my_stack<T>::push(const T& element)
{
if(full())
{
return false;
}
else
{
a[m_size++] = element;
return true;
}
}
template<class T>
bool my_stack<T>::get_top(T& top)const
{
if(empty())
{
return false;
}
else
{
top = a[m_size-1];
return true;
}
}
template<class T>
bool my_stack<T>::pop()
{
if(empty())
{
return false;
}
else
{
m_size--;
return true;
}
}
int main()
{
my_stack<int> s(20);
//进栈
cout<<"进栈"<<endl;
for(int i = 0; i < 20; i++)
{
s.push(i);
int t = -1;
if(s.get_top(t))
{
cout<<t<<' ';
}
}
cout<<endl;
cout<<"出栈"<<endl;
//出栈
for(int j = 0; j < 20; j++)
{
int t = -1;
if(s.get_top(t))
{
cout<<t<<' ';
}
if(s.pop())
{
}
}
return 0;
}