#include <iostream>
#include <stack>
using namespace std;
bool Is_Legal(stack<char>&s, const char* &pushstr, const char* &popstr)
{
if (strlen(pushstr) != strlen(popstr))
{
return false;
}
while (*pushstr != '\0')
{
if (*pushstr != *popstr)
{
s.push(*pushstr);
}
else
{
popstr++;
while (!s.empty() && s.top() == *popstr)
{
s.pop();
popstr++;
}
}
pushstr++;
}
if (s.empty() && *popstr == '\0')
{
return true;
}
else
{
return false;
}
}
int main()
{
stack<char> s;
const char *p1 = "12345";
const char *p2 = "24531";
if (Is_Legal(s, p1, p2))
{
cout << "出栈顺序合法" << endl;
}
else
{
cout << "出栈顺序不合法" << endl;
}
return 0;
}
栈(三)
最新推荐文章于 2022-04-22 23:43:58 发布