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

            堅(jiān)信:勤能補(bǔ)拙

            設(shè)計(jì)包含min函數(shù)的棧

            題目出處: http://zhedahht.blog.163.com/blog/static/25411174200712895228171/

            題目:定義棧的數(shù)據(jù)結(jié)構(gòu),要求添加一個(gè)min函數(shù),能夠得到棧的最小元素。要求函數(shù)minpush以及pop的時(shí)間復(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 閱讀(260) 評(píng)論(0)  編輯 收藏 引用 所屬分類: M_面試題集錦

            導(dǎo)航

            <2010年10月>
            262728293012
            3456789
            10111213141516
            17181920212223
            24252627282930
            31123456

            統(tǒng)計(jì)

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            www.久久热| 久久久久人妻一区精品性色av| 成人国内精品久久久久一区| 精品乱码久久久久久久| 久久久久久久尹人综合网亚洲| 亚洲成av人片不卡无码久久| 国产精品久久久久久久| 精品多毛少妇人妻AV免费久久| 狠狠色丁香婷婷久久综合| 日韩乱码人妻无码中文字幕久久| 久久93精品国产91久久综合| 国产精品99久久久久久宅男小说| 亚洲综合精品香蕉久久网| 久久久久国色AV免费观看| 国产精品天天影视久久综合网| 久久国产成人亚洲精品影院| 久久天天躁狠狠躁夜夜躁2O2O| 久久精品一区二区影院| 粉嫩小泬无遮挡久久久久久| 亚洲精品NV久久久久久久久久| 久久久久久毛片免费看| jizzjizz国产精品久久| 日本强好片久久久久久AAA| 热久久国产欧美一区二区精品| 久久精品国产99国产精偷| 精品一区二区久久| 久久夜色精品国产欧美乱| 亚洲色大成网站WWW久久九九| 久久无码国产| 97精品国产97久久久久久免费| 91精品日韩人妻无码久久不卡| 亚洲午夜精品久久久久久人妖| 天堂久久天堂AV色综合| 久久精品一本到99热免费| 久久久久亚洲国产| 色欲久久久天天天综合网精品| 一本久久a久久精品综合香蕉| 欧美日韩精品久久久久 | 久久精品夜夜夜夜夜久久| 蜜桃麻豆www久久国产精品| 国产午夜久久影院|