手写模拟栈

一:关于栈

(1)是一种线性存储结构

(2)限定只能在栈顶进行插入和删除操作。

(3)栈顶与栈底:允许元素插入与删除的一端称为栈顶,另一端称为栈底。

(4)先进后出,就像一个桶,往里放东西,最后放的肯定先被拿走

二:操作

定义st[],tt表示栈顶下标

(1)插入

st[+tt]=x

(2)弹出栈顶

tt--

(3)判断栈是否为空

if(tt>0)则不为空

else 为空

(4)取栈顶

st[tt]

三:基本板子题:

#include<cstdio>
#include<cstring>
#include<vector>
#include<set>
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
typedef long long ll;
const int maxn=1e5+10,maxn2=31*maxn;
int st[maxn];//ÏȽøºó³ö 
int tt;//¶ÓÍ· 
int main()
{
    int m;
    cin>>m;
    tt=0;
    while(m--)
    {
        char op[11];
        cin>>op;
        if(!strcmp(op,"push"))
        {
            int x;
            cin>>x;
            st[++tt]=x;
        }
        else if(!strcmp(op,"pop"))
        {
            tt--;
        }
        else if(!strcmp(op,"empty"))
        {
            if(tt>0)
                cout<<"NO"<<endl;
            else
                cout<<"YES"<<endl;
        }
        else if(!strcmp(op,"query"))
        {
            cout<<st[tt]<<endl;
            
        }
    }
}
原文地址:https://www.cnblogs.com/liyexin/p/13942373.html