[算法] 社区堆栈
对于操作上的堆栈,你可以看到 这里
由于协议栈是链接,形成前后的列表,所以我们不能直接公开 2 堆. 假设需要社区堆栈堆栈S2 S1, 我们用 1 堆栈STEMP存储S2的中间值,然后把只值将成为S1 STEMP.
如果社区 2 叠唱 1 新的堆栈是转移所有 2 堆栈堆栈切换到必要的公共堆栈前调解. 栈以及许多类似行动.

#include <iostream>
#include <stack>
using namespace std;
int main(){
stack <int> S1;
stack <int> S2;
stack <int> Stemp;
// create 2 Stack
for (int i = 0; i < 5; i++)
S1.push(i); // S1 : 0 1 2 3 4
for (int i = 5; i < 10; i++)
S2.push(i); // S2 : 5 6 7 8 9
// move S2 to Stemp;
while (!S2.empty()){
int x = S2.top();
S2.pop();
Stemp.push(x); // Stemp : 9 8 7 6 5
}
// move Stemp to S1
while(!Stemp.empty()){
int x = Stemp.top();
Stemp.pop();
S1.push(x); // S1 : 0 1 2 3 4 5 6 7 8 9
}
// show S1
while (!S1.empty()){
int x = S1.top();
S1.pop();
cout << x << " ";
}
return 0;
}



最新评论