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

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            93精91精品国产综合久久香蕉| 91精品观看91久久久久久| 伊人久久大香线蕉无码麻豆| 2021国产精品午夜久久| 99久久婷婷国产综合亚洲| 久久久久国产精品麻豆AR影院| 久久久精品人妻一区二区三区蜜桃| 久久青草国产手机看片福利盒子| 亚洲精品99久久久久中文字幕 | 国产精品久久久久影院嫩草 | 久久香蕉国产线看观看精品yw| 亚洲综合精品香蕉久久网97| 亚洲精品白浆高清久久久久久| 久久伊人亚洲AV无码网站| 国产91色综合久久免费| 亚洲国产精品18久久久久久| 亚洲国产精品无码久久久久久曰| 日韩亚洲欧美久久久www综合网| 老色鬼久久亚洲AV综合| 日韩人妻无码一区二区三区久久99| 99久久精品九九亚洲精品| 精品无码久久久久国产| 亚洲国产精品无码久久久蜜芽| 国内精品久久久久影院亚洲| 青青草国产97免久久费观看| 久久久久人妻一区精品| 久久久久久久久久免免费精品| 精品一区二区久久久久久久网站| 久久99久久99精品免视看动漫| 久久精品亚洲一区二区三区浴池 | 2021国内精品久久久久久影院| 麻豆久久| 亚洲AV日韩精品久久久久久| 久久精品日日躁夜夜躁欧美| 久久综合给合久久狠狠狠97色 | 欧美精品九九99久久在观看| 欧美与黑人午夜性猛交久久久| 久久影视国产亚洲| 久久99热这里只频精品6| av色综合久久天堂av色综合在| 亚洲精品无码久久久久sm|