Cracking The Coding Interview 3.6
2014-04-11 09:57
288 查看
// Write a program to sort a stack in ascending order. You should not make any assumptions about how the stack is implemented. The following are the only functions that should be used to write this program: push | pop | peek | isEmpty. // // 使用一个临时stack #include <iostream> #include <stack> using namespace std; class myStack { public: stack<int>s; public: myStack(){} void sort() { stack<int>buffer; while(!s.empty()) { if (buffer.empty()) { buffer.push(s.top()); s.pop(); } else { int t = s.top(); s.pop(); while(!buffer.empty() && t > buffer.top())/***注意&&左右判别式的先后***/ { s.push(buffer.top()); buffer.pop(); } buffer.push(t); } } while(!buffer.empty()) { s.push(buffer.top()); buffer.pop(); } } }; int main() { myStack st; for (int i =0; i<10; i++) { st.s.push(i*((i%2==0)?1:-1)); } st.sort(); cout<<endl; return 0; }
相关文章推荐
- Cracking the coding interview--Q3.6
- Cracking the coding interview--Q5.2
- Cracking the coding interview--Q3.3
- 【Cracking the coding interview】Q1.8(旋转字符串)
- cracking the coding interview ch1.1
- Cracking The Coding Interview 9.1
- 《Cracking the Coding Interview》——第10章:可扩展性和存储空间限制——题目6
- cracking the coding interview 中文版
- 《Cracking the Coding Interview》——第14章:Java——题目3
- 《Cracking the Coding Interview》——第16章:线程与锁——题目5
- 《Cracking the Coding Interview》——第17章:普通题——题目1
- 《Cracking the Coding Interview》——第17章:普通题——题目11
- 《Cracking the Coding Interview》——第18章:难题——题目12
- Cracking the coding interview--Q1.1
- Cracking the coding interview--Q9.5
- Cracking the coding interview--Q2.1
- Cracking the coding interview--问题与解答
- Cracking the coding interview--Q4.8
- Cracking the coding interview--Q5.3
- Cracking the coding interview--Q1.8