Stack push and pop Sequences

Source: Internet
Author: User

Question: enter two integer sequences. One sequence represents the push sequence of the stack and determines whether the other sequence may be in the pop sequence. For the sake of simplicity, we assume that any two integers in the push sequence are not equal.
For example, if the input push sequence is 1, 2, 3, 4, 5, 3, 2, and 1, it may be a pop series. It can have the following push and pop sequences: push 1, push 2, push 3, push 4, pop, push 5, pop, the pop sequence is 4, 5, 3, 2, and 1. However, sequences 4, 3, 5, 1, and 2 cannot be pop sequences of push sequences 1, 2, 3, 4, and 5.

 

 

[Cpp]
# Include <iostream>
Using namespace std;
Const int MAXSIZE = 10;
Template <class T>
Class stack
{
Public:
Stack ();
~ Stack ();
Bool push (T value );
Bool pop ();
Bool empty ();
T top ();
Private:
T s [MAXSIZE]; // elements corresponding to the stack
Int head; // points to the top element of the stack.
};
 
Template <class T>
Bool stack <T >:: empty ()
{
Return head =-1? 1:0;
}
 
Template <class T>
Stack <T>: stack ()
{
Head =-1;
}
 
Template <class T>
Stack <T> ::~ Stack ()
{
}
 
Template <class T>
Bool stack <T>: push (T value)
{
If (MAXSIZE-1) = head ){
Cout <"stack full" <endl;
Return false;
}
S [++ head] = value;
Return true;
}
 
Template <class T>
Bool stack <T>: pop ()
{
If (-1 = head ){
Cout <"Stack empty" <endl;
Return false;
}
-- Head;
Return true;
}
 
Template <class T>
T stack <T>: top ()
{
If (-1 = head)
Throw 1;
Else
Return s [head];
}
 
Bool Is_Pop (int * a, int * B, int len_a, int len_ B)
{
If (len_a! = Len_ B)
Return false;
Stack <int> s;
Int I = 1, j = 1;
While (j! = Len_a ){
// If the number of the input array B is incorrect
If (B [j] <1 | B [j]> = len_a)
Return false;
// If array a is all written into the stack, compare it with array B [j] in sequence.
If (I = len_a ){
While (! S. empty ()){
If (s. top ()! = B [j ++])
Return false;
S. pop ();
}
Return true;
}
If (a [I] <B [j]) {
S. push (a [I ++]);
}
Else {
S. push (a [I]);
++ I;
++ J;
S. pop ();
}
}
Return true;
}
 
Void main ()
{
Stack <int> s;
Int Push [] = {,}; // start with Push [1]
Int Pop [] = {,}; // starting from Pop [1]
Cout <Is_Pop (Push, Pop, sizeof (Push)/sizeof (int), sizeof (Pop)/sizeof (int ));
System ("pause ");
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.