首页 > 其他分享 >栈的用法详解

栈的用法详解

时间:2022-10-01 20:23:10浏览次数:55  
标签:end int 样例 stk ++ 详解 st 用法

栈的用法详解

目录

栈是一种先入后出(FILO)的数据结构。

利用数组模拟栈


#include <bits/stdc++.h>
using namespace std;
int stk[10005];
int end = 0;
void pop()
{
    stk[end] = 0;
    end--;
}
void push(int t)
{
    stk[end] = t;
    end++;
}
int main()
{
    for (int i = 0; i < 10005; i++)
    {
        stk[i] = 0;
    }
    while (cin.eof() == false)
    {
        char ch;
        cin >> ch;
        if (ch == 'P')
        {
            int t;
            cin >> t;
            push(t);
        }
        if (ch == 'O')
        {
            pop();
        }
        if (ch == 'Q')
        {
            for (int i = 0; i < end; i++)
                cout <<stk[i]<<" ";
            cout<<endl;
        }
    }
    return 0;
}


代码中,利用变量end维护栈底,正常情况下不能通过直接访问数组来获取栈中内容,但是这是数组,所以……

STL stack

#include <bits/stdc++.h>
using namespace std;
stack<int> st;
int main()
{
    for (int i = 0; i < 10; i++)
    {
        st.push(i);
    }
    cout << st.top() << endl;
    cout << st.size() << endl;
    cout << st.empty() << endl;
    st.pop();
    cout << st.size() << endl;
    cout << st.top() << endl;
    return 0;
}

经典的STL函数应用,输出为

9
10
0
9
8

其中可以发现st.empty()返回的是empty()?true:false,所以在实际应用上要记得改成!st.empty()


典型例题:洛谷P1427 小鱼的数字游戏

题目描述

小鱼最近被要求参加一个数字游戏,要求它把看到的一串数字 \(a_i\)(长度不一定,以 \(0\) 结束),记住了然后反着念出来(表示结束的数字 \(0\) 就不要念出来了)。这对小鱼的那点记忆力来说实在是太难了,你也不想想小鱼的整个脑袋才多大,其中一部分还是好吃的肉!所以请你帮小鱼编程解决这个问题。

输入格式

一行内输入一串整数,以 \(0\) 结束,以空格间隔。

输出格式

一行内倒着输出这一串整数,以空格间隔。

样例 #1

样例输入 #1

3 65 23 5 34 1 30 0

样例输出 #1

30 1 34 5 23 65 3

提示

数据规模与约定

对于 \(100\%\) 的数据,保证 \(0 \leq a_i \leq 2^{31} - 1\),数字个数不超过 \(100\)。

#include <bits/stdc++.h>
using namespace std;
int stk[10005];
int end = 0;
void pop()
{
    cout << stk[end] << " ";
    stk[end] = 0;
    end--;
}
void push(int t)
{
    stk[end] = t;
    end++;
}
int main()
{
    /*
    int n;
    cin>>n;
    for (int i = 0; i <n;i++){
        cin>>stk[i];
    }
    reverse(stk,stk+n-1);
    for(int i = 0; i < n; i++){
        cout<<stk[i];
    }
    */
    // int n;
    // cin >> n;
    for (int i = 0; i < 10005; i++)
    {
        int t;
        cin >> t;
        if (t == 0)
            break;
        push(t);
    }
    for (int i = 0; i < end; i++)
    {
        pop();
    }
    return 0;
}
// stl_main
int main_stl()
{
    stack<int> st;

    for (int i = 0; i < n; i++)
    {
        int t;
        cin >> t;
        if (t == 0)
            break;
        st.push(t);
    }
    while (!st.empty())
    {

        cout << st.top() < " ";
        st.pop();
    }
    return 0;
}

标签:end,int,样例,stk,++,详解,st,用法
From: https://www.cnblogs.com/sweepy/p/16747697.html

相关文章

  • Java泛型中<T> T 与 T的区别和用法
    有的方法返回值是<T>T,有的是T,区别在哪里?下面是一个泛型方法,<T>声明此方法有一个泛型T,也可以理解声明一个泛型方法.    下面三个T,第一个T表示是泛型,第二个......
  • 迷宫问题详解
    简介实验项目2:栈结构及其应用实验题目:迷宫问题求解实验内容:一个迷宫可以看成是由m×n个房间组成的矩形,迷宫内部的每个房间有4个方向,每个方向或者有障碍(如墙)而......
  • Python爬虫--Requests 库用法大全
    昨晚分享了Python爬虫的基本知识,本文分享一下爬虫里面请求相关的内容:Requests用法。往期知识回顾:​​Python爬虫基本原理​​​​12.奇怪知识(1)--Matlab爬虫获取王者荣耀......
  • C# Dictionary(数据字典)的基本用法
    通常情况下,我们可以通过int类型的索引来从数组或者List集合中查询所需的数据但是如果情况稍微复杂一点:索引是非int类型的数据(比如string或其他类型),这时候就需要使......
  • yum安装nginx的默认目录详解
    nginx是一种web应用服务,yum-yinstallnginx我们通过yum安装往往会找不到默认的配置文件,文件目录等等,我们来说一下  我们先通过yuminstallnginx安装好这个服务,这......
  • python的time库详解
    time库的使用:Python中内置了一些与时间处理相关的库,如time、datatime和calendar库。其中time库是Python中处理时间的标准库,是最基础的时间处理库。time库的功能如下:(1)计......
  • Python基础(九) | time random collections itertools标准库详解
    ⭐本专栏旨在对Python的基础语法进行详解,精炼地总结语法中的重点,详解难点,面向零基础及入门的学习者,通过专栏的学习可以熟练掌握python编程,同时为后续的数据分析,机器学习及深......
  • Python爬虫详解
    1、任务介绍需求分析爬取豆瓣电影Top250的基本信息,包括电影的名称,豆瓣评分,评价数,电影概况,电影链接等。https://movie.douban.com/top2502、基本流程2.1、准备工作通......
  • 【code基础】HashMap用法
    1.hashMap赋值的简便方法for(inti=0;i<s.length();i++){charc=s.charAt(i);if(maps.containsKey(c))maps.put(c,maps.g......
  • Python Markdown解析利器----mistune详细用法记录
    @目录小试牛刀开始使用mistunemistune简单使用mistune高级用法(自定义mistune)mistune中插件插件使用方法(以删除线(strikethrough)为例)插件包名内置插件删除线(striket......