• <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)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            理论片午午伦夜理片久久| 久久精品国产亚洲AV不卡| 99久久精品国产毛片| 91精品免费久久久久久久久| 久久香蕉综合色一综合色88| 久久久久国产一级毛片高清板| 麻豆精品久久久久久久99蜜桃| 18岁日韩内射颜射午夜久久成人| 狠狠色丁香久久综合婷婷| 成人国内精品久久久久影院| 久久av免费天堂小草播放| 狠狠色丁香久久婷婷综合图片| 2022年国产精品久久久久| 色婷婷久久综合中文久久一本| 国产精品99久久久精品无码| 久久久久亚洲AV成人片| 久久亚洲国产精品五月天婷| 久久99精品国产99久久6男男| 久久久久久久久久久精品尤物| 91视频国产91久久久| 久久久久久久97| 久久精品国产第一区二区| 性欧美丰满熟妇XXXX性久久久| 国内精品欧美久久精品| 久久久噜噜噜久久中文福利| 青青草原综合久久大伊人| 精品国产一区二区三区久久| 人妻精品久久久久中文字幕69| 久久成人小视频| 人人狠狠综合久久亚洲婷婷| 亚洲精品乱码久久久久久久久久久久| 久久天天躁狠狠躁夜夜不卡| 精品亚洲综合久久中文字幕| 久久精品国产亚洲AV无码娇色 | 亚洲午夜无码AV毛片久久| 欧美一区二区三区久久综| 久久这里有精品视频| 欧美精品丝袜久久久中文字幕 | 久久亚洲国产成人精品无码区| 一级做a爰片久久毛片人呢| 一本一本久久a久久综合精品蜜桃|