• <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ù)min、push以及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 閱讀(256) 評(píng)論(0)  編輯 收藏 引用 所屬分類: M_面試題集錦

            導(dǎo)航

            <2010年7月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            統(tǒng)計(jì)

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            奇米影视7777久久精品| 无遮挡粉嫩小泬久久久久久久| 亚洲伊人久久大香线蕉苏妲己| 国产精品成人99久久久久 | 久久久久综合中文字幕| 久久午夜福利电影| 亚洲中文字幕无码一久久区 | 欧美激情精品久久久久久久九九九| 午夜视频久久久久一区 | 国产亚洲精品自在久久| 久久久久无码精品国产app| 无码人妻久久一区二区三区免费丨 | 久久久久亚洲av无码专区导航 | 久久婷婷激情综合色综合俺也去| 久久国产成人| 精品久久久久久亚洲| 亚洲精品乱码久久久久66| 久久久久久亚洲精品不卡| 国内精品久久久久伊人av| 久久久久久久综合狠狠综合| 国产精品欧美久久久久天天影视 | 亚洲国产精品成人久久| 三级韩国一区久久二区综合 | 成人午夜精品无码区久久| 久久亚洲高清综合| 国产精品xxxx国产喷水亚洲国产精品无码久久一区 | 一本色道久久综合| 久久亚洲高清观看| 久久发布国产伦子伦精品 | 久久亚洲欧美日本精品| 久久久久亚洲AV无码专区体验| 狠狠综合久久综合88亚洲 | 久久超碰97人人做人人爱| 亚洲精品国精品久久99热一 | 久久天天躁狠狠躁夜夜不卡| 狠狠久久综合伊人不卡| 久久精品二区| 久久伊人色| 精品一二三区久久aaa片| 国内高清久久久久久| 麻豆AV一区二区三区久久|