zoukankan      html  css  js  c++  java
  • 编程之美:队列中的最大最小值

    #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
    ", q.maxItem());
        
    
        q.inque(2);
        printf("%d
    ", q.maxItem());
        q.deque();
    
        q.inque(1);
        printf("%d
    ", q.maxItem());
        
        stack s;
        s.push(3);
        printf("%d
    ", s.maxItem());
        
        
        s.push(2);
        printf("%d
    ", s.maxItem());
        s.pop();
        
        s.push(1);
        printf("%d
    ", 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
    ", q.maxItem());
        
    
        q.inque(2);
        printf("%d
    ", q.maxItem());
        q.deque();
    
        q.inque(1);
        printf("%d
    ", q.maxItem());
        
        stack s;
        s.push(3);
        printf("%d
    ", s.maxItem());
        
        
        s.push(2);
        printf("%d
    ", s.maxItem());
        s.pop();
        
        s.push(1);
        printf("%d
    ", s.maxItem());
        
    
    
        return 0;
    }

    代码与思路见原书236页

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

    1.求取栈的最大值

    2.用栈模拟队列

  • 相关阅读:
    c# 判断网络是否连接
    有关TSQL的10个好习惯
    相同文件只能一个进程读取
    我的单元测试方案
    又用了一把VBA
    深入理解字符串和字节数组转换
    如何清除应用程序承载 WebBrowser 控件时缓存
    VB也绿色
    ASP.Net网站开发的单元测试方案
    Nunit使用心得
  • 原文地址:https://www.cnblogs.com/mintmy/p/4199284.html
Copyright © 2011-2022 走看看