基于上一次业已实现的链表数据结构,这里贴出基于其实现的Stack数据结构,供大家学习参考交流。
#pragma once
#include"Vector.h"
template <typename T>
class Stack : public Vector<T>
{
public:
void push(T const& e)
{
Vector<T>::insert(e);
}
T pop()
{
return Vector<T>::remove(Vector<T>::size() - 1);
}
T& top()
{
return (*this)[Vector<T>::size() - 1];
}
};
int main()
{
int i;
Stack<int>stk;
for (i = 0; i < 10; i++)
{
stk.push(i);
}
cout << "-------------------01.取顶操作--------------------------------------------" << endl;
int top = stk.top();
cout << "the current top element of this stack is:" << top << endl;
cout << endl;
cout << "-------------------02.不断地弹栈,直到栈为空,并输出栈中的内容-------------" << endl;
while (!stk.empty())
{
cout << stk.top() << " ";
stk.pop();
}
cout << endl;
cout << endl << "test over!" << endl;
system("pause");
return 0;
}