stack操作

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()返回栈顶元素

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值