栈的链式存储结构--链栈
数据结构中的链栈可以由c++STL的stack容器实现
以下是常见函数:
#include<iostream> #include<stack> using namespace std; /* stack是一种先进后出的数据结构 只有一个出口 栈中只有顶端的元素才可以被外界使用,因此栈不允许有遍历行为 */ int main(){ stack<int> s1;//默认构造形式 s1.push(1);//向栈顶添加元素 s1.push(2); s1.push(3); s1.push(4); cout<<"栈顶元素为"<<s1.top()<<endl;//4 stack<int> s2; s2=s1;//重载了= ,容器直接可以相互赋值 cout<<"栈顶元素为"<<s2.top()<<endl;//4 s1.pop();//从栈顶移除第一个元素 cout<<"栈顶元素为"<<s1.top()<<endl;//3 //top()返回栈顶元素 cout<<"栈是否为空"<<s1.empty()<<endl;//0 cout<<"栈的大小为"<<s1.size()<<endl;//3 return 0; }

浙公网安备 33010602011771号