数据结构简单总结(1)——栈

记载目前遇到的数据结构,以及对他们的掌握情况。

一、栈(stack)

栈目前只遇到过朴素的栈,并没有那么多特别的属性。
普通的栈的性质:
①后入先出(FILO)。

项目掌握情况
stl应用100%
数组(array)实现100%
链表(list)实现100%
堆(heap)实现100%

1.stl应用

#include <iostream>
#include <stack>
using namespace std;
int main(){
    stack<int> s;
    //error 1 出栈时/引用栈顶 
    //if(s.size()==0)puts("Stack underflow!");

    s.push(1);
    cout<<"pushed "<<1<<endl;
    cout<<"top:"<<s.top()<<endl;
    cout<<"empty?"<<(s.empty()?"YES":"NO")<<endl;
    cout<<"size:"<<s.size()<<endl;
    s.push(2);
    cout<<"pushed "<<2<<endl;
    cout<<"top:"<<s.top()<<endl;
    cout<<"empty?"<<(s.empty()?"YES":"NO")<<endl;
    cout<<"size:"<<s.size()<<endl;
    s.pop();
    cout<<"poped "<<2<<endl;
    cout<<"top:"<<s.top()<<endl;
    cout<<"empty?"<<(s.empty()?"YES":"NO")<<endl;
    cout<<"size:"<<s.size()<<endl;
    s.pop();
    cout<<"poped "<<1<<endl;
    cout<<"empty?"<<(s.empty()?"YES":"NO")<<endl;
    cout<<"size:"<<s.size()<<endl;
}
/*
pushed 1
top:1
empty?NO
size:1
pushed 2
top:2
empty?NO
size:2
poped 2
top:1
empty?NO
size:1
poped 1
empty?YES
size:0
*/

2.数组实现

#include <iostream>
using namespace std;
const int Stack_size=100000;
int stack[Stack_size],top;
int main(){
    //error 1 入栈时 
    //if(top==Stack_size)puts("Stack overflow!");
    //error 2 出栈时/引用栈顶 
    //if(top==0)puts("Stack underflow!");

    //push
    stack[top++]=1;
    cout<<"pushed "<<stack[top-1]<<endl;
    cout<<"top:"<<stack[top-1]<<endl;
    cout<<"empty?"<<(top==0?"YES":"NO")<<endl;
    cout<<"size:"<<top<<endl;
    //push
    stack[top++]=2;
    cout<<"pushed "<<stack[top-1]<<endl;
    cout<<"top:"<<stack[top-1]<<endl;
    cout<<"empty?"<<(top==0?"YES":"NO")<<endl;
    cout<<"size:"<<top<<endl;
    //pop
    top--;
    cout<<"poped "<<2<<endl;
    cout<<"top:"<<stack[top-1]<<endl;
    cout<<"empty?"<<(top==0?"YES":"NO")<<endl;
    cout<<"size:"<<top<<endl;
    //pop
    top--;
    cout<<"poped "<<1<<endl;
    cout<<"empty?"<<(top==0?"YES":"NO")<<endl;
    cout<<"size:"<<top<<endl;
}
/*
pushed 1
top:1
empty?NO
size:1
pushed 2
top:2
empty?NO
size:2
poped 2
top:1
empty?NO
size:1
poped 1
empty?YES
size:0
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值