• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            A Za, A Za, Fighting...

            堅信:勤能補拙

            設計包含min函數的棧

            題目出處: http://zhedahht.blog.163.com/blog/static/25411174200712895228171/

            題目:定義棧的數據結構,要求添加一個min函數,能夠得到棧的最小元素。要求函數min、push以及pop的時間復雜度都是O(1)
            #include "StackWithMin.h"
            #include
            <cstdio>
            #include
            <cstdlib>
            #include
            <cstring>

            const int StackWithMin::MAX_SIZE ;

            StackWithMin::StackWithMin() :
                top_index(
            -1), min_index(-1)
            {
                printf(
            "StackWithMin Constructor\n");
            }

            StackWithMin::
            ~StackWithMin()
            {
                printf(
            "StackWithMin Destructor\n");
            }

            int
            StackWithMin::top() 
            const
            {
                
            if(top_index == -1) {
                    printf(
            "top() failed: Stack Empty\n");
                    
            return -1;
                }

                
            return stack[top_index]; 
            }

            int
            StackWithMin::get_min() 
            const
            {
                
            if(min_index == -1) {
                    printf(
            "get_min() failed: Stack Empty\n");
                    
            return -1;
                }

                
            return stack[min_index];
            }

            void
            StackWithMin::push(
            int value)
            {
                
            if(top_index == -1) { /* stack empty */
                    stack[
            ++top_index] = value;
                    min_index 
            = top_index;
                    
            return;
                }
                stack[
            ++top_index] = value;
                
            if(value < stack[min_index]) {
                    index[top_index] 
            = min_index;
                    min_index 
            = top_index;
                }
            }

            int
            StackWithMin::pop()
            {
                
            if(top_index == -1) {
                    printf(
            "pop() failed: Stack Empty\n");
                    
            return -1;
                }
                
            int ret = stack[top_index];
                
            if(min_index == top_index)
                    min_index 
            = index[top_index];
                
            --top_index;
                
            return ret;
            }



            class StackWithMin
            {
                public:
                    StackWithMin();
                    ~StackWithMin();
                    int get_min() const;
                    int top() const;
                    void push(int value);
                    int pop();      
                private:
                    static const int MAX_SIZE = 101;
                    int top_index, min_index;
                    int stack[MAX_SIZE];
                    int index[MAX_SIZE];
            };









            posted on 2011-05-25 15:49 simplyzhao 閱讀(260) 評論(0)  編輯 收藏 引用 所屬分類: M_面試題集錦

            導航

            <2011年5月>
            24252627282930
            1234567
            891011121314
            15161718192021
            22232425262728
            2930311234

            統計

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久se精品一区二区影院| 亚洲国产欧洲综合997久久| 国产成年无码久久久久毛片| 久久99国产精品二区不卡| 久久婷婷成人综合色综合| 久久精品免费观看| 免费精品国产日韩热久久| 亚洲国产欧洲综合997久久| 国产精品欧美亚洲韩国日本久久 | 亚洲精品美女久久久久99| 国产成人久久激情91| 久久91这里精品国产2020| 日韩AV无码久久一区二区| 色综合久久最新中文字幕| 久久精品aⅴ无码中文字字幕不卡| 91精品国产综合久久婷婷| 合区精品久久久中文字幕一区 | 性高湖久久久久久久久AAAAA| 香蕉久久av一区二区三区| 久久93精品国产91久久综合| 久久精品国产精品亚洲毛片| 超级碰碰碰碰97久久久久| 91久久精品视频| WWW婷婷AV久久久影片| 囯产极品美女高潮无套久久久| 久久久人妻精品无码一区| 2021久久精品国产99国产精品 | 天天久久狠狠色综合| 久久亚洲日韩精品一区二区三区| 久久夜色撩人精品国产小说| 女人香蕉久久**毛片精品| 久久久久99精品成人片欧美| 一本久久a久久精品亚洲| 2020国产成人久久精品| 久久久久久久免费视频| 久久影院久久香蕉国产线看观看| 久久综合综合久久狠狠狠97色88 | 一日本道伊人久久综合影| 一级a性色生活片久久无| 无码8090精品久久一区| 国产香蕉久久精品综合网|