PHP栈实现

9

我希望构建一个用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)都是函数式的。


4
жЏђдѕ›дїЎжЃЇпјЊPHPжњ‰array_pushпј€http://php.net/manual/en/function.array-push.phpпј‰е’Њ`array_pop`пј€http://us3.php.net/array_popпј‰е‡Ѕж•°е®ћзЋ°гЂ‚ - adeelx
注:$stack和$limit可以是private。请仅返回翻译后的文本。 - Raptor
是的,但如果你有那些,看起来更整洁,对吧? - Mark
显然,PHP中已经有一个更快、更完整的实现,详见 https://www.php.net/manual/en/class.ds-stack.php。 - David Spector
5个回答

4
以下是正确的堆栈类实现。要将数组正确初始化为堆栈的值,您必须像这样反转该数组的值:
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);
    }
}

编程愉快!


4

将您的构造函数更改如下:

<?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/zh/function.array-push.php) 和 array_pop (http://php.net/manual/zh/function.array-pop.php) 实现。


附注: 强烈建议在构造函数中验证输入参数(至少检查其类型)。 - Raptor

3

使用 PHP(面向过程方法)实现栈(Stack)

$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();

1
简单地说,更改您的构造函数:

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

准备好了,先生!我会给您一个受欢迎的答案!=) - Mark

0

将构造函数更改为此。使用此方法,您可以提供零个值、单个值或多个值的数组。如果值的数量大于限制,则会抛出错误。

    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);
    }

希望这能有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接