Problem: element out stack, the legality of the order into the stack. such as the sequence into the stack (1,2,3,4,5), the stack sequence is (4,5,3,1,2)
Define a stack sp, the stack sequence is str1, the stack sequence is str2, the length is size1 and size2 respectively. If two sequences are empty or unequal in length, it is illegal to judge two sequences of equal length and not empty.
The first element in the STR1 is first put into the stack, and then the STR1 is moved back through the loop.
1, if the current stack is empty and the stack sequence is not empty, then into the stack sequence str1 back, sp into the stack.
2, if the top of the stack is not equal to the stack sequence and into the stack sequence is not empty, then into the stack sequence str1 back, sp into the stack.
3, if the stack top element is equal to the stack sequence, SP out stack, the stack sequence str2.
After a loop comparison, if the stack is empty, the stack sequence is legitimate.
The following is an example of a sequence of stacks (1,2,3,4,5) and a stack sequence (4,5,3,1,2).
Using the template to achieve the object Outstack, you can judge the different types of elements out of the stack, the order of the legitimacy of the stack, specifically implemented as follows:
Template<class t> class Outstack {public:outstack ();
Outstack (const t* str1, const t* str2, size_t size1, size_t size2);
~outstack ();
BOOL Outstack::legality ();
private:t* _str1;
t* _str2;
size_t _size1;
size_t _size2;
}; Template<class t> outstack<t>::outstack (): _STR1 (null), _STR2 (null), _SIZE1 (0), _size2 (0) {} template<c Lass t> outstack<t>::outstack (const t* str1, const t* str2, size_t size1, size_t size2): _str1 (New T[size1), _ STR2 (new T[size2]), _size1 (size1), _size2 (size2) {memcpy (_str1, str1, sizeof (T) *_size1);//memcpy copied by number of bytes (_
STR2, STR2, (sizeof (T) *_size2));
} template<class t> Outstack<t>::~outstack () {if (_str1 | | _str2) {delete[] _str1;
Delete[] _STR2; } template<class t> bool Outstack<t>::legality () {<span style= "White-space:pre" > </span>T* STR1 = _str1;//into stack sequence <span style= "White-space:pre" > </span>t* str2 = _str2;//out stack sequence <span style= "White-space" :p Re ">
</span>stack<T> sp; <span style= "White-space:pre" > </span>if (str1 && str2 && _size1!= _size2) <span style= "W
Hite-space:pre "> </span>return 0; <span style= "White-space:pre" > </span>while (*str1!= ' ")//the stack sequence is compared <span style=" White-space:pre " > </span>{<span style= "White-space:pre" > </span>if (*str1!= NULL && sp.empty ()) <span s
tyle= "White-space:pre" > </span>{<span style= "White-space:pre" > </span>sp.push (*STR1);
<span style= "White-space:pre" > </span>str1++; <span style= "White-space:pre" > </span>} <span style= "White-space:pre" > </span>if (*str1!= NULL && sp.top ()!= *str2)//stack top element compared to out stack element <span style= "White-space:pre" > </span>{<span "style="
White-space:pre "> </span>sp.push (*STR1);
<span style= "White-space:pre" > </span>str1++; <span style= "White-space:pre" >;/span>} <span style= "White-space:pre" > </span>if (sp.top () = *str2) <span style= "White-space:pre" & Gt </span>{<span style= "White-space:pre" > </span>sp.pop () <span style= "White-space:pre" > <
/span>str2++; <span style= "White-space:pre" > </span> <span style= "White-space:pre" > </span>} <span style= "White-space:pre" > </span>if (Sp.empty ())//If the stack is empty, the stack sequence is legitimate <span style= "White-space:pre" > < /span>{<span style= "White-space:pre" > </span>return 1; <span style= "White-space:pre" > </span
} <span style= "White-space:pre" > </span>return 0; }
Test cases are as follows:
void Test1 ()
{
int arr1[] = {1, 2, 3, 4, 5};//into stack sequence
int str1[] = {4, 5, 3, 1, 2};//out stack sequence
//int str1[] = {3, 2, 1, 5, 4};//stack sequence
outstack<int> S1 (arr1, STR1, 5, 5);
cout << S1. Legality () << Endl;
char* arr2 = "abcdef";//into stack sequence
//char* str2 = "BAFDCE";//out Stack sequence
char* str2 = "BAEFDC";//Stack sequence
Outstack<char > s2 (arr2, STR2, 6, 6);
cout << S2. Legality () << Endl;
Outstack<int> S3;
}