栈的介绍_栈的概念

2023-03-31 08:26:52

 

1.栈的概念和性质

栈栈是一种特殊的线性表,主要的特点是元素的后进先出 插入元素的操作被称为入栈,删除栈顶元素的操作被称为出栈以下为栈的一些基本操作:boolcreateStack(SqStack&s,intn)//建容量为n的堆栈

2.栈的基本概念

{if(n<0)returnfalse;s.top=-1;s.n=n;s.elem=newElemTp[n];if(!s.elem)returnfalse;returntrue;}voiddestroyStack

3.栈到底是什么

(SqStack&s)//销毁堆栈存贮空间 {s.n=0;s.top=-1;delete[]s.elem;}voidclear()(SqStack&s){//置空栈 s.top=-1;}boolempty

4.栈的含义是什么

(){//判断栈空 returns.top==-1;}boolpush(Sqstack&s,intx){//入栈 if(s.top>=s.n-1)returnfalse;s.elem[++s.top]=

5.栈的基本知识

x;returntrue;}boolpop(Sqstack&s,int&x){//出栈 if(s.top==-1)returnfalse;x=s[s.top--];returnfalse;}栈和卡特兰数有着紧密的联系

6.栈是干嘛的

下面通过一道例题来感受 https://www.luogu.com.cn/problem/P1044 此题方法有很多,下面放一种dp解法 AC代码:#includeusing

7.栈有啥用

namespacestd;usingll=longlong;#define pb push_back #define ff first #define se second #define pii pair

8.栈的概念和特点

constintmaxn=1e5+5;lldp[25][25];intmain(){ios::sync_with_stdio(false);cin.tie(nullptr);intn;cin>>n;for

9.栈有什么作用

(inti=1;i<=n;++i)dp[0][i]=1;for(inti=1;i<=n;++i)for(intj=0;j<=n;++j){if(j==0)dp[i][j]=dp[i-1][j+1];else

10.栈的作用

dp[i][j]=dp[i-1][j+1]+dp[i][j-1];}cout<

luogu.com.cn/problem/P1449AC代码:#includeusingnamespacestd;usingll=longlong;#define pb push_back

#define ff first #define se second #define pii pair constintmaxn=1e5+5;intmain(){ios::sync_with_stdio

(false);cin.tie(nullptr);strings;stackq;cin>>s;inti=0;inta=0;while(s[i]!=@){if(s[i]>=0&&s[i]<=9){

a=10*a+(s[i]-0);}elseif(s[i]==.){q.push(a);a=0;}elseif(s[i]==+){intq2=q.top();q.pop();intq1=q.top();q

.pop();q.push(q1+q2);}elseif(s[i]==-){intq2=q.top();q.pop();intq1=q.top();q.pop();q.push(q1-q2);}else

if(s[i]==*){intq2=q.top();q.pop();intq1=q.top();q.pop();q.push(q1*q2);}else{intq2=q.top();q.pop();int

q1=q.top();q.pop();q.push(q1/q2);}++i;}cout<

实现+, -两个一元运算符(即正、负号); 操作数为整型参考代码#include#includeusingnamespacestd;inta[256];structStack

{chars[251];inttop;}opnd;voidpush(Stack&q,charx){//将元素x压入栈q q.s[q.top++]=x;}voidpop(Stack&q){//弹出栈顶元素

--q.top;}charTop(Stack&q){//获取栈顶元素 returnq.s[q.top-1];}intmain(){opnd.top=0;//初始化 intflag=1;a[*]=7;a[

/]=7;a[+]=6;a[-]=6;a[(]=1;a[)]=1;a[#]=0;a[P]=8;a[N]=9;push(opnd,#);chart;cin>>t;if(t==+)//处理在开头的一元运算符

{push(opnd,P);cin>>t;}elseif(t==-){push(opnd,N);cin>>t;}while(t!=#||Top(opnd)!=#){//运算完成后退出循环 if(t>=0

&&t=a&&t=A&&t=0&&t=a&&t=

A&&t<=Z){cout<>t;}cout<<;}else{if(a[Top(opnd)]

{push(opnd,t);flag=1;}elseif(Top(opnd)==(&&flag&&t==+)push(opnd,P);elseif(Top(opnd)==(&&flag&&t==-)push

(opnd,N);elsepush(opnd,t);cin>>t;}else{//栈内的优先级低则出栈 if(Top(opnd)!=()cout<t;pop(

opnd);}}}return0;}

 


以上就是关于《栈的介绍_栈的概念》的全部内容,本文网址:https://www.7ca.cn/baike/10062.shtml,如对您有帮助可以分享给好友,谢谢。
标签:
声明

排行榜