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

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            91精品国产色综久久| 国产欧美久久一区二区| 成人国内精品久久久久影院VR| 国产精品久久久久AV福利动漫| 丰满少妇高潮惨叫久久久| 99热精品久久只有精品| 亚洲国产成人久久一区WWW| 久久精品人人做人人爽97| 国产免费久久精品99久久| 久久精品国产AV一区二区三区| 狠狠88综合久久久久综合网 | 麻豆亚洲AV永久无码精品久久| 久久99亚洲网美利坚合众国| 久久精品成人欧美大片| 亚洲精品无码久久久久去q| 久久久久一本毛久久久| 久久久久久亚洲精品成人| 亚洲精品无码久久久| 国产精品99久久精品爆乳| 久久久久久久人妻无码中文字幕爆| 精品乱码久久久久久夜夜嗨| 麻豆AV一区二区三区久久 | 国产成人久久精品区一区二区| 久久人妻少妇嫩草AV蜜桃| 久久综合丝袜日本网| 亚洲精品乱码久久久久久中文字幕| 久久久精品日本一区二区三区| av无码久久久久不卡免费网站| 久久久久久曰本AV免费免费| 日韩十八禁一区二区久久| 国产精品亚洲美女久久久| 88久久精品无码一区二区毛片 | 久久久久国产日韩精品网站| 久久精品国产亚洲麻豆| av无码久久久久久不卡网站| 久久婷婷激情综合色综合俺也去| 国内精品久久久久影院老司| 日本WV一本一道久久香蕉| 久久经典免费视频| 77777亚洲午夜久久多人| 久久久久se色偷偷亚洲精品av|