#include <stack>
创建一个堆栈对象s(注意stack是模板类):stack <char> s; //堆栈的数据类型是字符型
把一个字符ct压入堆栈: s.push(ct);
把栈顶元素弹出:s.pop();
获取栈顶元素,放入变量c2: c2 = s.top();
判断堆栈是否空: s.empty(),如果为空则函数返回true,如果不空则返回false
逆序输出
#include<iostream>
#include<stack>
using namespace std;
stack <char> s;
int main()
{
int t;
cin >> t;
while (t--)
{
string str;
cin >> str;
int len;
len = str.length();
for (int i = 0; i < len; i++)
{
s.push(str[i]);
}
while (!s.empty())
{
cout << s.top();
s.pop();
}
cout << endl;
}
}
行编辑
#include<iostream>
#include<stack>
#include<cstring>
using namespace std;
int main()
{
int t;
cin >> t;
while(t--)
{
stack<char> s1, s2;
string str;
cin >> str;
int len = str.length();
char x;
for(int i = 0; i < len; i++)
{
x = str[i];
if(x == '#' )
{
if(!s1.empty()) s1.pop();//此if条件不能和上一条if合并是因为下面一行省略了:else do nothing
}
else s1.push(x); //仅当x!= '#'时。所以如果上面两个if放在一起的话,如果是a####就会push(#)
}
if(s1.empty()) cout << "NULL" << endl;
else
{
while(!s1.empty())
{
x = s1.top();
s1.pop();
s2.push(x);
}
while(!s2.empty())
{
x = s2.top();
cout << x;
s2.pop();
}
cout << endl;
}
}
}
括号匹配
#include<iostream>
#include<stack>
#include<map>
#include<cmath>
#include<string.h>
#include<algorithm>
using namespace std;
stack<char> p;
int main()
{
int t;
cin >> t;
while (t--)
{
string str;
cin >> str;
bool starnull = true;
bool flag = false;
for (int i = 0; i < str.length(); i++)
{
if (str[i] == '(' || str[i] == '{' || str[i] == '[' || str[i] == ')' || str[i] == '}' || str[i] == ']')
{
flag = true;
break;
}
}
for (int i = 0; i < str.length(); i++)
{
if (str[i] == '(' || str[i] == '{' || str[i] == '[')
{
if (str[i] == '(')
p.push(')');
else if (str[i] == '{')
p.push('}');
else if (str[i] == '[')
p.push(']');
}
else if (str[i] == ')' || str[i] == '}' || str[i] == ']')
{
if (!p.empty())
{
if (p.top() == str[i])
{
starnull = false;
p.pop();
}
else
{
starnull = true;
break;
}
}
else
{
starnull = true;
break;
}
}
}
if (flag == false)
{
cout << "ok" << endl;
}
else if (starnull == true || !p.empty())
{
cout << "error" << endl;
}
else
{
cout << "ok" << endl;
}
while (!p.empty())
{
p.pop();
}
}
return 0;
}
迷宫求解
#include <iostream>
#include <stack>
using namespace std;
struct position {
int x;
int y;
};
class Maze {
int **maze;
int size;
stack<position> path;
public:
Maze(int n);
~Maze();
void Go();
};
Maze::Maze(int n) {
size = n;
maze = new int *[n];
for (int i = 0; i < n; i++)
maze[i] = new int[n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
cin >> maze[i][j];
}
Maze::~Maze() {
for (int i = 0; i < size; i++)
delete[]maze[i];
delete[]maze;
}
void Maze::Go() {
path.push({0, 0});//第一个坐标必是(0,0)
maze[0][0] = 1; // =1 代表走过
int i = 0, j = 0;
while (true) {
if (maze[i][j + 1] == 0 && j + 1 < size) //向右
{
maze[i][j + 1] = 1;
path.push({i, ++j}); //走到 i,j+1位置,入栈
} else if (i + 1 < size && maze[i + 1][j] == 0) //向下
{
maze[i + 1][j] = 1;
path.push({++i, j});
} else if (j - 1 >= 0 && maze[i][j - 1] == 0) //向左
{
maze[i][j - 1] = 1;
path.push({i, --j});
} else if (i - 1 >= 0 && maze[i - 1][j] == 0) //向上
{
maze[i - 1][j] = 1;
path.push({--i, j});
} else //无路可走
{
//回退
i = path.top().x;
j = path.top().y;
//判断回退之后是否还有可以走的路
if(!((j + 1 < size && maze[i][j + 1] == 0) || (i + 1 < size && maze[i + 1][j] == 0) || (j - 1 >= 0 && maze[i][j - 1] == 0) || (i - 1 >= 0 && maze[i - 1][j] == 0)))
path.pop(); //出栈
}
if (path.empty() || (i == size - 1 && j == size - 1)) //如果回退到起点或者到达终点就结束循环
break;
}
//输出路径
if (path.empty())
cout << "no path" << endl;
else {
stack<position> path1;
while (!path.empty()) //将path倒序
{
path1.push(path.top());
path.pop();
}
i = 0;
while (!path1.empty()) {
cout << '[' << path1.top().x << ',' << path1.top().y << ']' << "--";
if ((++i) % 4 == 0) cout << endl;
path1.pop();
}
cout << "END" << endl;
}
}
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
Maze myMaze(n);
myMaze.Go();
}
return 0;
}