Friday, February 1, 2013
cciv 3.6
#include<stack>
#include<vector>
#include<algorithm>
template<class T>
#define ALGO_2
inline void sortstack(std::stack<T>& stack)
{
//algorithm 1
#ifdef ALGO_1
std::vector<T> v;
while(!stack.empty())
{
T val=stack.top();
stack.pop();
v.push_back(val);
}
std::sort(v.begin(),v.end());
for(int i=0;i<v.size();i++)
stack.push(v[i]);
#endif
//algorithm 2
#ifdef ALGO_2
std::stack<T> s2;
while(!stack.empty())
{
T val=stack.top();
stack.pop();
while(!s2.empty() && val<s2.top())
{
T val2=s2.top();
s2.pop();
stack.push(val2);
}
s2.push(val);
//cout<<s2.top()<<endl;
}
stack=s2;
#endif
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment