• <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函數,能夠得到棧的最小元素。要求函數minpush以及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 閱讀(256) 評論(0)  編輯 收藏 引用 所屬分類: M_面試題集錦

            導航

            <2011年9月>
            28293031123
            45678910
            11121314151617
            18192021222324
            2526272829301
            2345678

            統計

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            九九久久99综合一区二区| 国产欧美久久久精品影院| 国产激情久久久久久熟女老人| 国产精品内射久久久久欢欢| 日本免费久久久久久久网站| 日产精品久久久久久久性色| 亚洲国产精品无码久久一线| 色综合久久久久综合体桃花网| 午夜久久久久久禁播电影| 亚洲中文久久精品无码ww16| 久久只这里是精品66| 久久国产精品一国产精品金尊| 久久精品国产精品亚洲人人| 亚洲AV日韩精品久久久久| 亚洲国产二区三区久久| 久久99久国产麻精品66| 国产 亚洲 欧美 另类 久久| 亚洲AV日韩精品久久久久久| 久久精品国产亚洲av麻豆蜜芽 | 久久久久亚洲精品无码网址| 奇米影视7777久久精品| 欧美久久久久久| 伊人久久大香线蕉成人| 久久精品一区二区三区中文字幕| 精品综合久久久久久888蜜芽| 亚洲国产精品无码久久一区二区| 久久夜色精品国产噜噜亚洲a| 久久综合九色综合久99| 亚洲精品国精品久久99热| 久久久无码精品亚洲日韩蜜臀浪潮| 久久久午夜精品福利内容| 久久人人爽人人爽人人片AV麻烦| 伊人久久五月天| 国产精品久久久久久搜索| 日韩精品久久久久久| 亚洲日本久久久午夜精品| 亚洲精品乱码久久久久久中文字幕| 国产亚洲色婷婷久久99精品| 精品久久国产一区二区三区香蕉 | 一级做a爱片久久毛片| 无码8090精品久久一区|