首页 > 其他 > 详细

编程之美:队列中的最大最小值

时间:2015-01-03 11:50:30      阅读:256      评论:0      收藏:0      [点我收藏+]
#include "iostream"
#include "memory.h"
#include "stdio.h"
#include "limits.h"
typedef int Type;
const int MAXN=15;
const int MIN=INT_MIN;
class stack
{
public:
    stack()
    {
        stacktop=-1;
        maxItemIndex=-1;
    }
    void push(Type x)
    {
        ++stacktop;
        if (stacktop>=MAXN)
            ;
        else
        {
            stackItem[stacktop]=x;
            if (x>maxItem())
            {
                link2NextMaxItem[stacktop]=maxItemIndex;
                maxItemIndex=stacktop;
            }
            else link2NextMaxItem[stacktop]=-1;
        }
    }
    Type pop()
    {
        if (stacktop<0)
            ;
        else
        {
            Type x=stackItem[stacktop];
            if (maxItemIndex==stacktop)
            {
                maxItemIndex=link2NextMaxItem[stacktop];
            }
            --stacktop;
            return x;
        }
    }
    Type maxItem()
    {
        if (maxItemIndex>=0)
        {
            return stackItem[maxItemIndex];
        }
        else return MIN;
    }
    Type empty()
    {
        return stacktop==-1;
    }
private:
    Type stackItem[MAXN];
    Type link2NextMaxItem[MAXN];
    Type maxItemIndex;
    Type stacktop;

};
class Queue
{
public:
    inline Type max(Type x,Type y)
    {
        return x>y?x:y;
    }
    Type deque()
    {
        if (stackOut.empty())
        {
            while (!stackIn.empty())
                stackOut.push(stackIn.pop());
        }
        return stackOut.pop();
    }
    void inque(Type x)
    {
        stackIn.push(x);
    }
    Type maxItem()
    {
        return max(stackIn.maxItem(),stackOut.maxItem());
    }
private:
    stack stackIn,stackOut;
};
int main(int argc, char const *argv[])
{
    Queue q;
    q.inque(3);
    printf("%d\n", q.maxItem());
    

    q.inque(2);
    printf("%d\n", q.maxItem());
    q.deque();

    q.inque(1);
    printf("%d\n", q.maxItem());
    
    stack s;
    s.push(3);
    printf("%d\n", s.maxItem());
    
    
    s.push(2);
    printf("%d\n", s.maxItem());
    s.pop();
    
    s.push(1);
    printf("%d\n", s.maxItem());
    


    return 0;
}
push(Type x)
    {
        ++stacktop;
        if (stacktop>=MAXN)
            ;
        else
        {
            stackItem[stacktop]=x;
            if (x>maxItem())
            {
                link2NextMaxItem[stacktop]=maxItemIndex;
                maxItemIndex=stacktop;
            }
            else link2NextMaxItem[stacktop]=-1;
        }
    }
    Type pop()
    {
        if (stacktop<0)
            ;
        else
        {
            Type x=stackItem[stacktop];
            if (maxItemIndex==stacktop)
            {
                maxItemIndex=link2NextMaxItem[stacktop];
            }
            --stacktop;
            return x;
        }
    }
    Type maxItem()
    {
        if (maxItemIndex>=0)
        {
            return stackItem[maxItemIndex];
        }
        else return MIN;
    }
    Type empty()
    {
        return stacktop==-1;
    }
private:
    Type stackItem[MAXN];
    Type link2NextMaxItem[MAXN];
    Type maxItemIndex;
    Type stacktop;

};
class Queue
{
public:
    inline Type max(Type x,Type y)
    {
        return x>y?x:y;
    }
    Type deque()
    {
        if (stackOut.empty())
        {
            while (!stackIn.empty())
                stackOut.push(stackIn.pop());
        }
        return stackOut.pop();
    }
    void inque(Type x)
    {
        stackIn.push(x);
    }
    Type maxItem()
    {
        return max(stackIn.maxItem(),stackOut.maxItem());
    }
private:
    stack stackIn,stackOut;
};
int main(int argc, char const *argv[])
{
    Queue q;
    q.inque(3);
    printf("%d\n", q.maxItem());
    

    q.inque(2);
    printf("%d\n", q.maxItem());
    q.deque();

    q.inque(1);
    printf("%d\n", q.maxItem());
    
    stack s;
    s.push(3);
    printf("%d\n", s.maxItem());
    
    
    s.push(2);
    printf("%d\n", s.maxItem());
    s.pop();
    
    s.push(1);
    printf("%d\n", s.maxItem());
    


    return 0;
}

代码与思路见原书236页

简单的来说:求队列中的最大值过程由两部分组成

1.求取栈的最大值

2.用栈模拟队列

编程之美:队列中的最大最小值

原文:http://www.cnblogs.com/mintmy/p/4199284.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!