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

            堅信:勤能補拙

            設(shè)計包含min函數(shù)的棧

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

            題目:定義棧的數(shù)據(jù)結(jié)構(gòu),要求添加一個min函數(shù),能夠得到棧的最小元素。要求函數(shù)minpush以及pop的時間復(fù)雜度都是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 閱讀(261) 評論(0)  編輯 收藏 引用 所屬分類: M_面試題集錦

            導(dǎo)航

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

            統(tǒng)計

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久精品国产2020| 国产91久久综合| 99国产精品久久久久久久成人热| 99精品久久久久中文字幕| 久久国产成人| 久久99精品久久只有精品 | 亚洲国产成人久久综合一区77 | 人人狠狠综合88综合久久| 国产精品久久久久免费a∨| 久久久国产精品福利免费 | 久久se精品一区精品二区国产| 无码国内精品久久综合88 | 久久久精品国产| 91秦先生久久久久久久| 精品久久久久香蕉网| 青青草原综合久久大伊人| 国产精品九九久久免费视频 | 久久久久久久99精品免费观看| 四虎国产精品成人免费久久| 色偷偷888欧美精品久久久| 亚洲国产精品久久久天堂| 亚洲国产成人久久综合区| 久久国产精品免费一区| 99久久国产亚洲高清观看2024| 久久精品天天中文字幕人妻| 久久免费看黄a级毛片| 伊人精品久久久久7777| 久久久久久久亚洲精品| 久久成人永久免费播放| 久久综合久久综合久久综合| 免费国产99久久久香蕉| 国产精品丝袜久久久久久不卡| 久久久九九有精品国产| 99久久国产免费福利| 精品欧美一区二区三区久久久| 99久久免费国产精品| 久久乐国产精品亚洲综合| 亚洲国产成人久久精品99| 欧美日韩精品久久久久| 久久99九九国产免费看小说| 亚洲午夜无码久久久久|