1、stack基本概念
stack是一种先进后出的数据结构,他只有一个出口
栈中进入的数据叫---入栈push
栈中d出的数据叫---出栈pop
例如,d匣
2、stack常用接口
构造函数:
- stack
stk;采用模板类实现,stack对象的默认构造形式 - stack(const stack &stk);拷贝构造函数
赋值 *** 作:
- stack& operator=(const stack &stk);重载等号运算符
数据存取:
- push(elem);向栈顶添加元素
- pop();从栈顶移除第一个元素
- top();返回栈顶元素
大小 *** 作:
- empty();判断堆栈是否为空
- size();返回栈的大小
#include
using namespace std;
void test01()
{
stack st1;
st1.push(10);
st1.push(20);
st1.push(30);
st1.push(40);
st1.push(50);
cout << st1.size() << endl;
//只要栈 不是空,查看栈顶,执行出栈操作
while (!st1.empty())
{
cout << st1.top() << endl;
st1.pop();
}
cout << st1.size() << endl;
}
int main()
{
cout << "test01" << endl;
test01();
system("pause");
return 0;
}
test01
5
50
40
30
20
10
0
请按任意键继续. . .
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)