Stack push-in, pop-up sequence

Source: Internet
Author: User

Title: Enter a sequence of two integers, the first sequence represents the stacking order of the stack, and determine if the second sequence is the pop-up order for the stack. Assume that all the numbers that are pressed into the stack are not equal.  For example, sequence 1, 2, 3, 4, 5 is a stack of the stacking sequence, sequence 4,5,3,2,1 is the stack sequence corresponding to a pop-up sequence, but 4,3,5,1,2 is not likely to be the stack sequence of pop-up sequence. How to solve the problem: if the next pop-up number is just the top number of the stack, then pop directly. If the next pop-up number is not on the top of the stack, we will press the number that is not yet in the stack in the stack sequence into the auxiliary stack until the next number that needs to eject is pressed into the top of the stack. If all the numbers are pressed into the stack and the next pop-up number is not found, then the sequence cannot be a pop-up sequence.
1 BOOLIspoporder (Const int* Ppush,Const int* Ppop,intnlength)2 {3  BOOLbpossible=false;4  if(ppush!=null&&ppop!=null&&nlength>0)5  {6   Const int* pnextpush=Ppush;7   Const int* pnextpop=Ppop;8std::stack<int>Stackdata;9    Ten   while(pnextpop-ppop<nlength) One  { A     while(Stackdata.empty () | | Stackdata.top ()!=*Pnextpop) -    { -        if(pnextpush-ppush==nlength) the         Break; -Stackdata.push (*Pnextpush); -pnextpush++; -    } +    if(Stackdata.top ()!=*Pnextpop) -     Break; +    A Stackdata.pop (); atpnextpop++; - } -  if(Stackdata.empty () &&pnextpop-ppop==nlength) -bpossible=true; - } - returnbpossible; in}

Stack push-in, pop-up sequence

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.