• <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 閱讀(270) 評論(0)  編輯 收藏 引用 所屬分類: M_面試題集錦

            導(dǎo)航

            <2025年9月>
            31123456
            78910111213
            14151617181920
            21222324252627
            2829301234
            567891011

            統(tǒng)計

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            日本精品久久久久影院日本 | 久久久WWW免费人成精品| 久久婷婷成人综合色综合| 人妻久久久一区二区三区| 精品国产一区二区三区久久| 99久久做夜夜爱天天做精品| 99999久久久久久亚洲| 国内精品伊人久久久影院| 国产亚洲精久久久久久无码| 精品久久人人爽天天玩人人妻| 久久99国产精品久久99| 国产精品一区二区久久精品涩爱| 久久久久久久99精品免费观看| 亚洲伊人久久精品影院| 久久婷婷五月综合97色直播| 蜜桃麻豆www久久| 久久精品国产99久久无毒不卡| 嫩草伊人久久精品少妇AV| 一级女性全黄久久生活片免费 | 国产精品99久久久久久人| 日韩电影久久久被窝网| 国产精品久久久久久久午夜片| 99re这里只有精品热久久| 久久久亚洲欧洲日产国码是AV | 国产精品激情综合久久| 香蕉久久一区二区不卡无毒影院| 久久精品免费一区二区三区| 狠狠综合久久AV一区二区三区| 久久综合成人网| 久久久久青草线蕉综合超碰| 亚洲精品无码久久久| 无码8090精品久久一区| 久久久久久久综合综合狠狠| 久久久久久久国产免费看| 久久久久久亚洲精品不卡| 久久亚洲中文字幕精品一区| 亚洲国产高清精品线久久| 亚洲国产成人久久综合区| 亚洲精品乱码久久久久久蜜桃 | 亚洲AV无码久久精品狠狠爱浪潮| 国产午夜福利精品久久2021|