• <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)論排行榜

            久久只有这精品99| 久久国产亚洲精品无码| 久久精品国产亚洲AV无码麻豆 | 国产亚洲精久久久久久无码| 久久综合久久综合亚洲| 伊人色综合久久天天网| 欧美一级久久久久久久大| 久久久精品国产亚洲成人满18免费网站| 好久久免费视频高清| 久久国产热精品波多野结衣AV| 久久久久99精品成人片直播| 99久久国产综合精品麻豆| 国产婷婷成人久久Av免费高清| 国产成人久久精品区一区二区| 99久久国产热无码精品免费| 97久久综合精品久久久综合| 曰曰摸天天摸人人看久久久| 久久久久久亚洲精品不卡| 亚洲国产成人精品91久久久 | 国产成人无码精品久久久性色 | 久久综合丝袜日本网| 精品久久久久一区二区三区| 久久夜色精品国产www| 久久精品国产亚洲AV忘忧草18| 亚洲精品乱码久久久久久 | 久久国产欧美日韩精品| 久久人爽人人爽人人片AV| 久久精品一区二区国产| 精品一久久香蕉国产线看播放| 亚洲国产视频久久| 久久99亚洲网美利坚合众国| 国产999精品久久久久久| 亚洲天堂久久久| 久久综合中文字幕| 久久夜色精品国产噜噜亚洲a| AV色综合久久天堂AV色综合在| 久久久久久久国产免费看| 国产成人精品免费久久久久| 日韩久久久久中文字幕人妻| 精品久久久久久久| 精品久久久无码21p发布|