Stack push-in, pop-up sequence

Source: Internet
Author: User

Title: Determine whether a number sequence is a stack of these numbers into the stack ( Prerequisites:The number in the stack is not duplicated)

Idea 1: If the next pop-up number is just the top number of the Stack, it pops up directly. If the next pop-up number is not at the top of the stack, we will press the stack sequence without the stack number into the auxiliary stack, knowing that the next number to eject will be pushed to the top of the stack. If all the numbers are pressed in and the next pop-up is still not found, then the sequence cannot be a pop-up sequence.

Train of thought 2: Open up a auxiliary stack, simulation into the stack to play the process (assuming PA for the stack sequence, PB for the fight sequence), the elements in the PA press into the auxiliary stack, the new pressed element and the pop-up sequence of the same stack, the auxiliary stack pops up, while the PB moves upward, not the same PB elements continue into the

1#include"stdafx.h"2#include <stack>3 4 //Method 15 BOOLIspoporder (Const int* Ppush,Const int* Ppop,intnlength)6 {7     BOOLBpossible =false;8     9     if(Ppush! = NULL && Ppop! = null && nlength >0)Ten     { One         Const int* Pnextpush =Ppush; A         Const int* Pnextpop =Ppop; -          -std::stack<int>Stackdata; the          -          while(Pnextpop-ppop <nlength) -         { -             //when the stack top element of the secondary stack is not the element to eject +             //press some numbers into the stack first -              while(Stackdata.empty () | | stackdata.top ()! = *Pnextpop) +             { A                 //If all the numbers are pressed into the secondary stack, exit the loop at                 if(Pnextpush-ppush = =nlength) -                      Break; -                  -Stackdata.push (*Pnextpush); -                  -Pnextpush + +; in             } -              to             if(Stackdata.top ()! = *Pnextpop) +                  Break; -          the Stackdata.pop (); *Pnextpop + +; $         }Panax Notoginseng          -         if(Stackdata.empty () && Pnextpop-ppop = =nlength) theBpossible =true; +     } A      the     returnbpossible; +      - } $  $ //Method 2 - BOOLIsPopOrder1 (Const int* Ppush,Const int* Ppop,intLengtha,intLENGTHB) - { the     if(Lengtha! = LENGTHB | | lengtha = =0) -         return false;Wuyi     BOOLFlag =false; the  -     intPA =0; Wu     intPB =0; -     int*newppush =New int[Lengtha]; About     inttop =-1; $      for(PA =0; PA < Lengtha; ++PA) -     { -++top; -Newppush[top] =PPUSH[PA]; A          while(Newppush[top] = =PPOP[PB]) +         { the--top; -++PB; $         } the     } the     if(Top = =-1) theFlag =true; the     Delete[]newppush; -     returnFlag; in } the  the intMain () About { the     Const intNlength =5 ; the     intPush[nlength] = {1,2,3,4,5}; the     intPop[nlength] = {4,5,3,2,1}; +      -     BOOLFlag1 =Ispoporder (push, pop, nlength); theprintf"Solution 1 is%d\n", Flag1);Bayi      the     BOOLFlag2 =IsPopOrder1 (push, pop, nlength, nlength); theprintf"Solution 2 is%d", Flag2); -     return 0; -}

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.