数据结构篇-栈

本篇开始学习栈,首先栈这种数据结构是一种后进先出的结构,栈限定为只能在一段进行删除或插入操作。

比如我们要插入2 3 1三个数,按照2 3 1 的顺序插入叫做入栈,出栈的顺序就是1 3 2。

现在尝试写一个判断回文串的程序。

/*
*这里讲述用栈的思想来判断回文串,
*主要思路是取字符串的前半截入栈,
*然后确定要匹配的字符串后半截下标,逆序将栈与字符串匹配
**/
#include<bits/stdc++.h>
using namespace std;
int main () {
    char a[101], s[101];
    int len, mid, next, top;
    //读入一行字符
    gets(a);
    len = strlen(a);//求字符长度
    mid = len / 2 - 1;//求字符中点
    //栈的初始化
    top = 0;
    //将mid前的字符依次入栈
    for (int i = 0; i <= mid; i++) {
        s[++top] = a[i];
    }
    //判断mid前的字符长度是奇数还是偶数,并找出需要进行字符匹配的起始下标。
        if (len % 2 == 0) {
            next = mid + 1;
        }
        else next = mid + 2;
        //开始匹配
        for(int i = next; i <= len - 1; i++) {
            if (a[i] != s[top]) {
                break;
            }
            top--;
        }
        //如果top的值为0,则说明栈内所有的字符都被一一匹配了
        if(top == 0) {
            cout << "YES" << endl;
        }
        else cout << "NO" << endl;
    return 0;
}

 

现在,再了解一个东西

逆波兰表示法

参照《挑战程序设计竞赛2》的例子:

(1 + 2) * (5 + 4)用逆波兰表示法就是1 2 + 5 4 + *

它的优势就是不需要括号,现在写一个程序来模拟逆波兰表示法

这两个逆波兰表示法的代码必须ctrl+z终止才能出结果

这两个逆波兰表示法的代码必须ctrl+z终止才能出结果

这两个逆波兰表示法的代码必须ctrl+z终止才能出结果

 

#include<string.h>
#include<stdio.h>
#include<iostream>
using namespace std;
int top, S[1000];
void push (int x) {
    //top + 1之后将元素插入top所指的位置
    S[++top] = x;
}
int pop () {
    top--;
    //返回top所指的位置
    return S[top + 1];
}
int main () {
    int a, b;
    top = 0;
    char s[100];
    while(scanf("%s", s) != EOF) {
        if( s[0] == '+' ) {
            a = pop();
            b = pop();
            push(a + b);
        }
        else if(s[0] == '-') {
            b = pop();
            a = pop();
            push(a - b);
        }
        else if(s[0] == '*') {
            a = pop();
            b = pop();
            push (a * b);
        }
        else push(atoi(s));//atoi()是标准库的函数,用来将字符串形式的数字转换成整型的数值
    }
    cout << pop() << endl;
    return 0;
}

 

接下来就是标准库了

#include<iostream>
#include<stack>
using namespace std;
int main() {
    stack <int> s;
    s.push(3);//3
    s.push(2);//3 2
    s.push(4);//3 2 4
    s.pop();//3 2
    int size = s.size();
    for(int i = 0; i < size; i++) {
        cout << s.top() << " ";
        s.pop();
    }

    return 0;
}

 

函数名功能复杂度
size()返回栈的元素数O(1)
top()返回栈顶的元素O(1)
pop()从栈中取出并删除元素O(1)
push(x)向栈中添加元素xO(1)
empty()在栈为空时返回trueO(1)

用标准库实现

#include<iostream>
#include<stack>
#include<cstdlib>
using namespace std;
int main() {
    stack<int> s;
    int a, b;
    string c;
    while(cin >> c) {
        if(c[0] == '+') {
            a = s.top();
            s.pop();
            b = s.top();
            s.pop();
            s.push(a + b);
        }
        else if(c[0] == '-') {
            a = s.top();
            s.pop();
            b = s.top();
            s.pop();
            s.push(a - b);
        }
        else if(c[0] == '*') {
            a = s.top();
            s.pop();
            b = s.top();
            s.pop();
            s.push(a * b);
        }
        else {
            s.push(atoi(c.c_str()));
        }
    }
    cout << s.top() << endl;
    return 0;
}


 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值