剑指offer第二十二题:栈的压入、弹出序列
1 //============================================================================ 2 // Name : JZ-C-22.cpp 3 // Author : Laughing_Lz 4 // Version : 5 // Copyright : All Right Reserved 6 // Description : 栈的压入、弹出序列 7 //============================================================================ 8 9 #include <iostream> 10 #include <stdio.h> 11 #include <stack> 12 using namespace std; 13 /** 14 *给定两个整数序列,第一个序列表示栈的压入顺序,判断第二个序列是否会是该栈的弹出顺序 15 */ 16 bool IsPopOrder(const int* pPush, const int* pPop, int nLength) { 17 std::stack<int> StackData; //辅助栈 18 if (pPush != NULL && pPop != NULL && nLength != 0) { 19 int pLength = nLength-1;//pLength指的是压入栈剩余未进辅助栈的数目 20 StackData.push(*pPush); 21 while (!StackData.empty()) { //每次将辅助栈的栈顶元素和弹出栈序列第一个数比较,直至全部比较完毕 22 if (StackData.top() != *pPop && pLength > 0) { 23 pPush++; 24 StackData.push(*pPush); 25 pLength--; 26 } else if (StackData.top() != *pPop && pLength <= 0) { 27 return false; //弹出栈序列有错误,直接退出循环 28 } else { 29 StackData.pop(); 30 pPop++; 31 if(StackData.empty() && pLength > 0){//出栈后若辅助栈为空,继续压入‘压入栈序列’后续元素 32 StackData.push(*pPush); 33 } 34 } 35 } 36 } else { 37 return false; 38 } 39 return true; 40 } 41 42 // ====================测试代码==================== 43 void Test(char* testName, const int* pPush, const int* pPop, int nLength, 44 bool expected) { 45 if (testName != NULL) 46 printf("%s begins: ", testName); 47 48 if (IsPopOrder(pPush, pPop, nLength) == expected) 49 printf("Passed.\n"); 50 else 51 printf("failed.\n"); 52 } 53 54 void Test1() { 55 const int nLength = 5; 56 int push[nLength] = { 1, 2, 3, 4, 5 }; 57 int pop[nLength] = { 4, 5, 3, 2, 1 }; 58 59 Test("Test1", push, pop, nLength, true); 60 } 61 62 void Test2() { 63 const int nLength = 5; 64 int push[nLength] = { 1, 2, 3, 4, 5 }; 65 int pop[nLength] = { 3, 5, 4, 2, 1 }; 66 67 Test("Test2", push, pop, nLength, true); 68 } 69 70 void Test3() { 71 const int nLength = 5; 72 int push[nLength] = { 1, 2, 3, 4, 5 }; 73 int pop[nLength] = { 4, 3, 5, 1, 2 }; 74 75 Test("Test3", push, pop, nLength, false); 76 } 77 78 void Test4() { 79 const int nLength = 5; 80 int push[nLength] = { 1, 2, 3, 4, 5 }; 81 int pop[nLength] = { 3, 5, 4, 1, 2 }; 82 83 Test("Test4", push, pop, nLength, false); 84 } 85 86 // push和pop序列只有一个数字 87 void Test5() { 88 const int nLength = 1; 89 int push[nLength] = { 1 }; 90 int pop[nLength] = { 2 }; 91 92 Test("Test5", push, pop, nLength, false); 93 } 94 95 void Test6() { 96 const int nLength = 1; 97 int push[nLength] = { 1 }; 98 int pop[nLength] = { 1 }; 99 100 Test("Test6", push, pop, nLength, true); 101 } 102 103 void Test7() { 104 Test("Test7", NULL, NULL, 0, false); //这里若两栈均为空,判断为false 105 } 106 107 void Test8() { 108 const int nLength = 5; 109 int push[nLength] = { 1, 2, 3, 4, 5 }; 110 int pop[nLength] = { 1, 5, 4, 3, 2 }; 111 112 Test("Test8", push, pop, nLength, true); 113 } 114 int main(int argc, char** argv) { 115 Test1(); 116 Test2(); 117 Test3(); 118 Test4(); 119 Test5(); 120 Test6(); 121 Test7(); 122 Test8(); 123 124 return 0; 125 }
时间: 2024-10-10 02:49:03