FW-stack sort- 12/17/2013 C++ version

#include <iostream>
#include <stack>
using namespace std;
class stacksort
{
    stack<int> * o_stack;
    stack<int> * b_stack;
    stack<int> * r_stack;
    public: stacksort()
    {
        o_stack = new stack<int>();
        b_stack = new stack<int>();
        r_stack = new stack<int>();
    }
    public: void push(int i)
    {
        o_stack->push(i);
    }
    public: int pop()
    {
        int tempi = o_stack->top();
        o_stack->pop();
        return tempi;
    }
    public : void sort_stack()
    {
        while(!o_stack->empty() || !b_stack->empty())
        {// if one of the two is not empty;
            int min;
            
           
            if(!o_stack->empty())
            {// when we pop something we must consider whether the right value can be poped rather than null 
                min = o_stack->top();
                o_stack->pop();
                while(!o_stack->empty())
                {
                    int min_cddt = o_stack->top();
                    o_stack->pop();
                    
                    if(min_cddt<min)
                    {
                        b_stack->push(min);
                        min = min_cddt;
                    }
                    else
                    {
                        b_stack->push(min_cddt);
                    }
                }
                r_stack->push(min);
            }

            if(!b_stack->empty())
            {
                min = b_stack->top();
                b_stack->pop();
                
                while(!b_stack->empty())
                {
                    int min_cddt2 = b_stack->top();
                    b_stack->pop();
                    
                     if(min_cddt2<min)
                    {
                        o_stack->push(min);
                        min = min_cddt2;
                    }
                    else
                    {
                        o_stack->push(min_cddt2);
                    }
                }
                r_stack->push(min);
            }
                        
        }
        
        delete o_stack;
        o_stack = 0;
        delete b_stack;
        b_stack = 0;
    
        o_stack=r_stack;
    }
};
int main()
{
        stacksort ss;  
         int a[7] = {1,23,-2,-43,-243,-33,3332};  
         for(int i = 0;i<7;++i)  
         {  
             ss.push(a[i]);  
         }  
         ss.sort_stack();  
           
         for(int i = 0;i<7;++i)  
         {  
             cout << ss.pop()<<" ->> "; 
         }  
   
   return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值