#创作计划#以小学生看待栈
2025-05-24 19:29:43
发布于:上海
点赞过50,主播更新下一期(我不配当主播,不要取关我!!!!!!)
😍
@AC君
点个赞呗(你们不给,会伤害一个小朋友的心的)
不知道,各位学习栈的时候,有没有“两眼一黑,倒地狗带”的感觉,的确公式总是会串在一起(老师要默写);现在,这些代码已经铭记于心了。
首先呢,头文件十分重要:
//死记硬背
#include <stack>
//我:其实#include <bits/stdc++.h>也可以
//老师:你万能头玩的真好
//我:我默写头文件就默写这个
//老师:*****。
然后,就是栈是什么了:
我的回答:栈就是一种容器,后进先出,就像一把枪,先进的子弹,最后发射
老师的回答:栈就是一种STL容器,STL是什么,后面再说。在笔记本上写好,栈的特点就是后进先出,抬头,看这幅图...
AI的回答:栈是一种特殊的线性表,它只能在表的一端(称为栈顶)进行插入和删除操作。这就像一个只能从顶部取放物品的容器,最后放入的物品会最先被取出,因此栈具有 “后进先出”(Last In First Out,LIFO)的特点。
看不懂没事:
请看VCR:
(这东西只有我能画出来了)
这下来,就是代码了:
我来说,就那么几个part(部分):
1.定义:
stack<int>st;
//固定名称 + 数组变量类型 + DIY栈名称
2.上图第一个步骤:入栈
本意就是将一个元素进入这个容器栈
int x;
cin >> x;
st.push(x);
//将x(输入来的元素)压进栈
3.出栈
st.pop();
//将最上面的元素弹出
4.输出栈顶元素(最上面的元素):
cout << st.top() << " ";
5.判断栈是否为空:
cout << st.empty();
//如果栈为空时,输出1,否则,输出0;
6.栈de大小
cout << st.size();
//输出栈的大小;
理清知识点,下一步就是做题了:
老师:大家看一下这题题目,用其他方法的给我抄十遍代码。。。
我:残暴的老师
于是我给出了以下代码:
/*
#include<iostream>
using namespace std;
int st[105];
int top=0;
void push(int x){
top++;
st[top]=x;
}
void pop(){
top--;
}
int TOP(){
return st[top];
}
int size(){
return top;
}
bool empty(){
return top==0;
}
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
string zl;
cin>>zl;
if(zl=="push"){
int x;
cin>>x;
push(x);
}
else if(zl=="pop"){
if(empty())
cout<<"pop fail"<<endl;
else{
cout<<"pop "<< TOP()<<endl;
pop();
}
}
else if(zl=="top"){
if(empty())
cout<<"top fail"<<endl;
else
cout<<"top = "<<TOP()<<endl;
}
else if(zl=="size")
cout<<"size = "<<size()<<endl;
else if(zl=="empty"){
if(empty())
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
}
}
return 0;
}
*/
#include <iostream>
#include <stack>
using namespace std;
int main()
{
int n, x;
cin >> n;
string c;
stack <int> s;
for(int i = 1; i <= n; ++i)
{
cin >> c;
if(c == "push")
{
cin >> x;
s.push(x);
}
else if(c == "pop")
{
if(!s.empty())
{
cout << "pop " << s.top() << endl;
s.pop();
}
else
{
cout << "pop fail" << endl;
}
}
else if(c == "top")
{
if(!s.empty())
{
cout << "top = " << s.top() << endl;
}
else
{
cout << "top fail" << endl;
}
}
else if(c == "size")
{
cout << "size = " << s.size() << endl;
}
else if(c == "empty")
{
if(!s.empty())
{
cout << "no" << endl;
}
else
{
cout << "yes" << endl;
}
}
}
return 0;
}
//老师:这么写是吧!
//我:老师罚不了我
//老师:这么喜欢写代码,给我抄俩遍
//我:为我花生!!!
各位学会栈了吗?
打个广告
全部评论 53
打个广告
2025-05-24 来自 上海
3能否加我团队,各位大佬
2025-05-06 来自 上海
2https://www.acgo.cn/team/1812809403946418176
2025-05-06 来自 上海
1你加了我的团队吗?
2025-05-10 来自 浙江
0我好似早就加了
2025-05-10 来自 浙江
0
2025-05-25 来自 北京
1@C++真真真难啊啊啊 点赞过50了!!该更下一期了
2025-05-24 来自 广东
1OK
2025-05-24 来自 上海
0三天后,见第二篇
2025-05-24 来自 上海
03天过了
1周前 来自 上海
0
我一个小学生学通了线段树,真实事件,我还在小码王上课来着
2025-05-12 来自 浙江
16
2025-05-13 来自 江西
0其实线段树也没那么难,就是代码多点,前期确实很简单
2025-05-14 来自 浙江
0o
2025-05-14 来自 江西
0
互关
2025-05-11 来自 广东
1建议把标题改为 #创作计划#以小学生看待栈 ,这样更有可能被@AC君推荐
2025-05-06 来自 北京
1THANK YOU
2025-05-06 来自 上海
0
58个人觉得很赞
2025-05-30 来自 浙江
0d
2025-05-24 来自 上海
0dd
2025-05-24 来自 上海
0dd
2025-05-24 来自 上海
0d
2025-05-24 来自 上海
0dd
2025-05-24 来自 上海
0dd
2025-05-24 来自 上海
0dd
2025-05-24 来自 上海
0d
2025-05-24 来自 上海
0d
2025-05-24 来自 上海
0d
2025-05-24 来自 上海
0d
2025-05-24 来自 上海
0
有帮助,赞一个