【问题标题】:PHP Stack ImplementationPHP 堆栈实现
【发布时间】:2013-12-11 04:44:20
【问题描述】:

我想构建一个用 PHP 实现的堆栈。最初我有这个代码:

class Stack
{
    protected $stack;
    protected $limit;

    public function __construct($limit = 10) {
        // initialize the stack
        $this->stack = array();
        // stack can only contain this many items
        $this->limit = $limit;
    }

    public function push($item) {
        // trap for stack overflow
        if (count($this->stack) < $this->limit) {
            // prepend item to the start of the array
            array_unshift($this->stack, $item);
        } else {
            throw new RunTimeException('Stack is full!');
        }
    }

    public function pop() {
        if ($this->isEmpty()) {
            // trap for stack underflow
          throw new RunTimeException('Stack is empty!');
      } else {
            // pop item from the start of the array
            return array_shift($this->stack);
        }
    }

    public function top() {
        return current($this->stack);
    }

    public function isEmpty() {
        return empty($this->stack);
    }
}

然后用这个正常初始化类:

$stack = new Stack();
$stack->push(1);
$stack->push(2);
$stack->push(3);
$stack->push(4);
$stack->push(5);

这是正确的并且正在运行。但是,我想用这样的初始值初始化我的堆栈:

$stack = new Stack(array(1,2,3,4,5));

我该如何实现?


请注意,所有其他功能(例如 pop 和 push)都是正常的。

【问题讨论】:

标签: php data-structures stack


【解决方案1】:

如下改变你的构造函数:

<?php

class Stack {

    protected $stack;
    protected $limit;

    public function __construct($limit = 10, $initial = array()) {
        // initialize the stack
        $this->stack = $initial;
        // stack can only contain this many items
        $this->limit = $limit;
    }

    public function push($item) {
        // trap for stack overflow
        if (count($this->stack) < $this->limit) {
            // prepend item to the start of the array
            array_unshift($this->stack, $item);
        } else {
            throw new RunTimeException('Stack is full!');
        }
    }

    public function pop() {
        if ($this->isEmpty()) {
            // trap for stack underflow
            throw new RunTimeException('Stack is empty!');
        } else {
            // pop item from the start of the array
            return array_shift($this->stack);
        }
    }

    public function top() {
        return current($this->stack);
    }

    public function isEmpty() {
        return empty($this->stack);
    }

}

/**
 * This'll work as expected.
 */
$stack = new Stack();
$stack->push(1);
$stack->push(2);
$stack->push(3);
$stack->push(4);
$stack->push(5);

/**
 * And this too.
 */
$stack = new Stack(10, array(1, 2, 3, 4, 5));

仅供参考,PHP 有 array_push (http://php.net/manual/en/function.array-push.php) 和 array_pop (http://us3.php.net/array_pop) 实现。

【讨论】:

  • 旁注:强烈建议在构造函数中验证输入参数(至少检查其类型)。
【解决方案2】:

这是正确堆栈类的实现。要将数组正确初始化为堆栈的值,您必须像这样反转该数组的值:

class Stack
{
    protected $stack;
    protected $limit;

    public function __construct($values = array(),$limit = 10) {
        // initialize the stack
        $this->stack = array_reverse($values);
        // stack can only contain this many items
        $this->limit = $limit;
    }

    public function push($item) {
        // trap for stack overflow
        if (count($this->stack) < $this->limit) {
            // prepend item to the start of the array
            array_unshift($this->stack, $item);
        } else {
            throw new RunTimeException('Stack is full!');
        }
    }

    public function pop() {
        if ($this->isEmpty()) {
            // trap for stack underflow
          throw new RunTimeException('Stack is empty!');
      } else {
            // pop item from the start of the array
            return array_shift($this->stack);
        }
    }

    public function top() {
        return current($this->stack);
    }

    public function isEmpty() {
        return empty($this->stack);
    }
}

编码愉快!

【讨论】:

    【解决方案3】:

    使用 php 的堆栈(过程方法)

    $data_array = [];
    $top = -1;
    
    function push($top, $item, $data_array){
        global $top,$data_array;
        $top++;
        $data_array[$top] = $item;
        return $data_array;
    }
    
    
    function pop(){
        global $top,$data_array;
        if($top < 0)
            return -1;
        $top_item = $data_array[$top];
        unset($data_array[$top]);
        $top--;
        return $top_item;
    }
    push($top, 1, $data_array);
    push($top, 2, $data_array);
    push($top, 3, $data_array)
    
    pop();
    

    【讨论】:

      【解决方案4】:

      简单,改变你的构造函数:

      public function __construct($limit = 10, $values = array()) {
          // initialize the stack
          $this->stack = $values;
          // stack can only contain this many items
          $this->limit = $limit;
      }
      

      【讨论】:

      • 准备得到一个受欢迎的答案先生!.=)
      【解决方案5】:

      将构造函数更改为此。有了这个,您可以在数组中不提供任何值、单个值或多个值。如果值计数大于限制,它将引发错误。

          public function __construct($limit = 10, $values = null) {
              // stack can only contain this many items
              $this->limit = $limit;
              // initialize the stack
              $this->stack = array();
              if (is_null($values)) $values = array();
              else if (!is_array($values)) $values = array($values);
              foreach ($values as $value) $this->push($value);
          }
      

      好了,希望对你有帮助。

      【讨论】:

        猜你喜欢
        • 2013-01-25
        • 2010-11-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-07
        • 2019-11-11
        • 2017-07-27
        • 2012-04-07
        相关资源
        最近更新 更多