zoukankan      html  css  js  c++  java
  • 编程1:设计一个有getMin功能的栈

    <?php
    header("content-type:text/html;charset=utf-8");
    /*
     *设计一个有getMin功能的栈 P1
     */
    
    class MyStack{
        private $stackData;
        private $stackMin;
    
        public function __construct()
        {
            $this->stackData = new SplStack();
            $this->stackMin = new SplStack();
        }
    
        public function push($newData){
            if($this->stackMin->isEmpty()){
                $this->stackMin->push($newData);
            }
            elseif ($newData <= $this->stackMin->top()){
                $this->stackMin->push($newData);
            }
            $this->stackData->push($newData);
        }
    
        public function pop(){
            if($this->stackData->isEmpty()){
                echo "栈已空";
                return false;
            }
            if($this->stackData->top() == $this->stackMin->top()){
                $this->stackMin->pop();
            }
            return $this->stackData->pop();
        }
    
        public function getMin(){
            if($this->stackMin->isEmpty()){
                echo "栈已空";
                return false;
            }
            return $this->stackMin->top();
        }
    }
    
    echo "向栈中一次压入7,3,1,4,0:";
    echo "</br>";
    $myStack = new MyStack();
    $myStack->push(7);
    $myStack->push(3);
    $myStack->push(1);
    $myStack->push(4);
    $myStack->push(0);
    print_r($myStack);
    echo "</br>";
    echo "</br>";
    
    echo "弹出栈顶元素0,此时的最小值栈也应该有变化:";
    echo "</br>";
    $myStack->pop();
    print_r($myStack);
    echo "</br>";
    echo "</br>";
    
    echo "弹出栈顶元素4,此时的最小值栈也应该无变化:";
    echo "</br>";
    $myStack->pop();
    print_r($myStack);
    echo "</br>";
    echo "</br>";
    
    echo "获取此时栈中最小值:";
    echo "</br>";
    $min = $myStack->getMin();
    echo $min;

    输出结果:

  • 相关阅读:
    visual studio 2010的安装
    安装操作系统的过程图解
    教育法学第八章单元测试MOOC
    教育法学第七章单元测试MOOC
    教育法学第六章单元测试MOOC
    教育法学第四章单元测试MOOC
    教育法学第三章单元测试MOOC
    教育法学第一章单元测试MOOC
    教育法学-第二章单元测验mooc
    单元测验4:人格知识大比武2mooc
  • 原文地址:https://www.cnblogs.com/xlzfdddd/p/10020415.html
Copyright © 2011-2022 走看看