面试题22—栈的压入、弹出序列
来源:互联网 发布:秒杀java实现代码 编辑:程序博客网 时间:2024/06/15 14:26
题目:输入两个整数序列,第一个表示栈的压入顺序,青判断第二个序列是否为该栈的弹出序列。
代码示例:
#include<iostream>#include<assert.h>using namespace std;#define MaxSize 100class MyClass{int data[MaxSize];int length;public:MyClass(){length = -1;}~MyClass(){}bool empty(void);bool push(int n);bool pop(void);int gettop(void);bool IsPopSerial(int pushArr[],int popArr[], int n);};bool MyClass::empty(void){return (length == -1);}bool MyClass::push(int n){length++;if (length < MaxSize){data[length] = n;return true;}elsereturn false;}bool MyClass::pop(void){if (length >= 0){length--;return true;}else{return false;}}int MyClass::gettop(void){assert(length >= 0);return data[length];}bool MyClass::IsPopSerial(int pushArr[], int popArr[], int n){if (pushArr == NULL || popArr==NULL||n <= 0)return false;for (int j = 0,i=0;j<=n&&i<=n;){if (empty() || gettop()!=popArr[j]){push(pushArr[i]);i++;}int temp = gettop();if (temp == popArr[j]){pop();j++;}}if (empty())return true;elsereturn false;}int main(){MyClass mystack;const int n = 5;int pushArr[n] = { 5,3,4,2,7};int popArr[n] = { 4,5,3,2,7};bool flag = mystack.IsPopSerial(pushArr,popArr, n);if (flag){for (int i = 0; i < n; i++)cout << popArr[i] << " ";cout << "是";for (int i = 0; i < n; i++)cout << pushArr[i] << " ";cout<<"的一个出栈序列!" << endl;}else{for (int i = 0; i < n; i++)cout << popArr[i] << " ";cout << "不是";for (int i = 0; i < n; i++)cout << pushArr[i] << " ";cout << "的一个出栈序列!" << endl;}}
阅读全文
0 0
- 面试题22—栈的压入、弹出序列
- 栈的压入、弹出序列(面试题 22)
- 面试题22:栈的压入弹出序列
- 面试题22栈的压入、弹出序列
- 面试题22 栈的压入弹出序列
- 面试题22:栈的压入弹出序列
- 面试题22:栈的压入、弹出序列
- 面试题22:栈的压入、弹出序列
- 面试题22 栈的压入、弹出序列
- 面试题22:栈的压入、弹出序列
- 面试题22:栈的压入、弹出序列
- 面试题22栈的压入弹出序列
- 面试题22:栈的压入、弹出序列
- 面试题 22: 栈的压入,弹出序列
- 面试题22:栈的压入、弹出序列
- 面试题22:栈的压入、弹出序列
- 剑指offer代码解析——面试题22栈的压入、弹出序列
- 剑指Offer:面试题22——栈的压入,弹出序列(java实现)
- Spring4笔记--IOC
- 跳转Activity时,加入动画效果
- js数组方法
- 未来的世界一定是尔虞我诈的世界吗
- CircleImageView 解析与定制
- 面试题22—栈的压入、弹出序列
- jQuery加载html文档的几种方法
- 报文格式
- ERROR: SWT folder '' does not exist
- Node安装和使用
- syscon 的使用
- 理解回调机制-java
- 面试题23—从上往下打印二叉树
- 装机版制作启动u盘图文教程