stack可以用数组或链表来实现。
先进后出。
#include <iostream>
using namespace std;
#include <cstdio>
#include <cstring>
#include <stack>
#include <cstring>
#include <string>
struct node
{
char a[10];
string b;
};
int main()
{
stack<int> s1;
/*stack<node> s2;
stack<float> s3;
stack<double> s4;
stack<long long> s5;
stack<char> s6;*/
s1.push(1); //push
s1.push(4);
s1.push(0);
s1.push(2);
if(s1.empty()) //empty
printf("空\n");
else
printf("不空\n");
//cout<<s1.size()<<endl; //size
cout<<s1.top()<<endl; //top
s1.pop(); //pop
cout<<s1.top()<<endl;
s1.pop();
s1.pop();
s1.pop(); //多了会出现内存问题,没有元素就不能在top
cout<<s1.size()<<endl;
return 0;
}
1,
C++ Stacks(堆栈)
C++ Stack(堆栈) 是一个容器类的改编,为程序员提供了堆栈的全部功能,——也就是说实现了一个先进后出(FILO)的数据结构。
操作 | 比较和分配堆栈 |
empty() | 堆栈为空则返回真 |
pop() | 移除栈顶元素 |
push() | 在栈顶增加元素 |
size() | 返回栈中元素数目 |
top() | 返回栈顶元素 |