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

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            少妇久久久久久久久久| 国内精品久久久久久99| 久久se这里只有精品| 国产精品免费看久久久香蕉| 欧美国产成人久久精品| 亚洲午夜久久久影院伊人| 青青草国产精品久久| 伊人色综合九久久天天蜜桃| 无码人妻久久一区二区三区| 91久久精品国产成人久久| 亚洲国产精品无码久久| 久久精品国产福利国产琪琪| 亚洲综合伊人久久大杳蕉| 久久国产精品免费一区二区三区| 久久久久99这里有精品10| 中文字幕亚洲综合久久2| 亚洲国产精品无码久久久蜜芽 | 久久综合九色综合精品| 久久99热这里只频精品6| 99热成人精品免费久久| 久久这里只有精品18| 欧美亚洲国产精品久久久久| 久久亚洲国产中v天仙www | 国产精品嫩草影院久久| 日本强好片久久久久久AAA | 亚洲国产精品成人久久蜜臀 | 久久亚洲高清综合| 国产精品久久网| 久久A级毛片免费观看| 99久久99久久精品国产片果冻| 九九久久精品国产| 爱做久久久久久| 国产精品一区二区久久精品无码 | 国产精品久久久久久久| 99久久人妻无码精品系列蜜桃| 久久亚洲日韩精品一区二区三区| 伊人久久精品无码av一区| 国产成人综合久久精品红| 中文字幕久久精品无码| 日本强好片久久久久久AAA| 国产午夜福利精品久久2021|