栈的push和pop 序列

来源:互联网 发布:户外刀具能在淘宝买吗 编辑:程序博客网 时间:2024/05/22 12:23
//思路:
//模拟栈, 让栈顶元素和pop序列比较,若相等则出栈, 若不等则入栈
//结果:若栈空,则是,非空则不是。

#include <iostream>#include <vector>#include <stack>using namespace std;#define NSIZ 1200int IsPushPop(int push[], int pop[], int n){int i = 0, j = 0;stack<int > st;st.push(push[i]);while(i++ < n){while(!st.empty() && j < n){if (st.top() == pop[j]){++j;st.pop();}else{break;}}if (i < n){st.push(push[i]);}}return st.size() == 0? 1: 0;}int main(){int push[] = {1,2 , 3, 4, 5};int pop[] = {4, 3, 5, 1, 2};int n = sizeof(push)/sizeof(push[0]);printf("%s\n", IsPushPop(push, pop, n) == 0?"NO":"YES");return 0;}


原创粉丝点击