栈(三)

#include 
#include 
using namespace std;

bool Is_Legal(stack&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 s;
	const char *p1 = "12345";
	const char *p2 = "24531";
	if (Is_Legal(s, p1, p2))
	{
		cout << "出栈顺序合法" << endl;
	}
	else
	{
		cout << "出栈顺序不合法" << endl;
	}
	return 0;
}

你可能感兴趣的:(c++,数据结构)