Determines whether a sequence is a pop-up sequence of stacks

Source: Internet
Author: User

given two sequences, determine whether the next sequence is the first sequence in the stack's stacking order

People who have studied data structures must have encountered many of these problems.

    比如给定一个序列  如 1 2 3 4 5的入栈序列    问 4 5 3 2 1是不是前者的一个出栈序列
    • Ideas

First see the Stack sequence 4 5 3 2 1 The first element is 4 that is, you must find the stack at the first 4 and then out of the stack to continue looking for 5, you can use a stack to store the current stack element

For example, in the 4 stack is the stack should have 1 2 3 because the stack order is 1 2 3 4 123 in front of 4, so also continue in the stack below a table to represent

Steps

Operation

Elements in the stack

Pop-up number the first one is 4, first find 4

1

1 into the stack

1

2

2 into the stack

1 2

3

3 into the stack

1 2 3

4

4 into the stack

1 2 3 4

At this point the top element of the stack equals the first one in the stack order, and the next step is to stack the element

5

4 out of Stack

1 2 3

4 The stacking order moves backwards, pointing to 5

6

5 into the stack

1 2 3 5

At this point the top element of the stack equals the first one in the stack order, and the next step is to stack the element

7

5 out of Stack

1 2 3

4 5 out of the stack move backwards point 3

8

3 out of Stack

1 2

4 5 3 because the top element of the stack equals the stack element

9

2 out of Stack

1

4 5 3 2 because the top element of the stack equals the stack element

10

1 out of Stack

4 5 3 2 1 out of the box if the account is empty and no other in the stack sequence is represented as true



代码如下所示:
#include <iostream>#include <stack>using namespace STD;BOOLJudgestacka_b (int*pushnumber,int*popnumber,intLength) {BOOLresult=false;if(pushnumber!=NULL&&popnumber!=NULL&&length>0)        {int*p=pushnumber;int*q=popnumber; Stack<int>S while(q-popnumber<length) { while(S.empty () | | *q!=s.top ()) {if(p-pushnumber==length)//indicates fullPart of the element into the stack break;                    S.push (*P);                p++;                } if (S.top ()!=*q)//Indicates whether all elements in the stack have found an equal out stack element without ending break;                S.pop ();            q++;        } if (S.empty () &&q-popnumber==length) result=true; } return result;    void Main () {int a[5]={1,2,3,4,5};    int b[5]={4,5,2,3,1};    if (Judgestacka_b (a,b,5)) printf ("yes\n"); else printf ("no\n");}

Determines whether a sequence is a pop-up sequence of stacks

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.