honeyhhhh / honeyhhhh.github.io

0 stars 0 forks source link

栈的应用 | Zion #29

Open honeyhhhh opened 5 years ago

honeyhhhh commented 5 years ago

https://zionlove.site/stackuse/

回溯:一条路走到黑,碰壁了回来继续走到黑回溯算法搭配递归算法,退回到上一状态的过程叫做回溯,枚举下一个状态的过程叫做递归;用栈结构后进先出记录网页的浏览历史保存文本编辑器中的undo序列深度优先搜索表达式求值子程序/函数调用的管理消除递归 栈1234567891011121314151617181920212223242526272829303132333435363738394041424344

honeyhhhh commented 4 years ago

判断回文数

#include <iostream>
#include <string>
using namespace std;

class arrStack
{
    int mSize;
    int atop;//小于mSize,栈指针
    char *st;
public:
    arrStack():mSize(100),atop(-1),st(new char[mSize]){}
    ~arrStack(){delete [] st;}
    bool push(const char item)
    {
        if (atop == mSize - 1) {
            cout << "Full" << endl;
            return false;
        } else {
            st[++atop] = item;
            return true;
        }
    }
    bool pop(char & item)
    {
        if (atop == -1) {
            cout << "Empty" << endl;
            return false;
        } else {
            item = st[atop--];
            return true;
        }
    }
    void isHW(string &str)
    {
        if (str.length() == 0) {
            return ;
        }
        int i = 0;
        for (; i < str.length()/2; i++)
        {
            push(str.c_str()[i]);
        }
        if (str.length() % 2 != 0) {
            i++;
        }
        char c;
        while (atop != -1)
        {
            pop(c);
            if (c != str.c_str()[i]) {
                cout << "No" << endl;
                return ;
            }
            else
                i++;
        }
        if (atop == -1) {
            cout << "Yes" << endl;
        }

    }
};
int main()
{
    string str = "a";
    arrStack s;
    s.isHW(str);
    return 0;
}