栈的顺序存储c++
#include<iostream>
using namespace std;
template<class T>
class sqlist {
public:
sqlist(int n);
~sqlist();
void travel_sqlist();
int len_sqlist();
bool isempty();
bool ispull();
bool push_back(T e);
T pop_back(T &e);
private:
T *elem;
int top;
int len;
int maxsize;
};
template<class T>
sqlist<T>::sqlist(int n)
{
this->elem = new T[n];
this->top = -1;
this->maxsize = n;
}
template<class T>
sqlist<T>::~sqlist()
{
delete this->elem;
this->elem = NULL;
}
template<class T>
void sqlist<T>::travel_sqlist()
{
for (int i = this->top; i >= 0; i--)
{
cout << this->elem[i] << " ";
}cout << endl;
}
template<class T>
bool sqlist<T>::isempty()
{
return this->top == -1;
}
template<class T>
bool sqlist<T>::ispull()
{
return this->top == this->maxsize - 1;
}
template<class T>
int sqlist<T>::len_sqlist()
{
return this->top + 1;
}
template<class T>
bool sqlist<T>::push_back(T e)
{
if (this->ispull())
return 0;
else
{
this->top++;
this->elem[this->top] = e;
return 1;
}
}
template<class T>
T sqlist<T>::pop_back(T &e)
{
if (this->isempty())
return 0;
else
{
e = this->elem[this->top];
this->top--;
return 1;
}
}
int main()
{
sqlist<int> s(8);
int e = 0;
cout << "请输入要录入的4个元素" << endl;
for (int i = 0; i < 4; i++)
{
int temp_x;
cin >> temp_x;
s.push_back(temp_x);
}
if (s.isempty())
cout << "是空顺序栈" << endl;
else
cout << "不是空顺序栈" << endl;
int Length = s.len_sqlist();
cout << "顺序栈的长度为:\n" << Length << endl;
s.travel_sqlist();
s.pop_back(e);
s.travel_sqlist();
system("pause");
return 0;
}