PHP堆栈实现 [英] PHP Stack Implementation

查看:114
本文介绍了PHP堆栈实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  class Stack 
{
protected $ stack;
protected $ limit;

public function __construct($ limit = 10){
//初始化栈
$ this-> stack = array();
// stack只能包含这么多项目
$ this-> limit = $ limit;
}

public function push($ item){
//堆栈溢出陷阱
if(count($ this-> stack)< $ this - > limit){
//将数组前面的项添加到数组的开头
array_unshift($ this-> stack,$ item);
} else {
抛出新的RunTimeException('Stack is full!');
}
}

public function pop(){
if($ this-> isEmpty()){
// trap for stack underflow
抛出新的RunTimeException('Stack is empty!');
} else {
//从数组开头的pop项
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)); 

如何实现?






请注意,所有其他功能(例如弹出和推送)都可以正常工作。

解决方案

这是正确的堆栈类的实现。要正确地将数组初始化为堆栈的值,您必须按照以下方式反转该数组的值:

  class Stack 
{
protected $ stack;
protected $ limit;

public function __construct($ values = array(),$ limit = 10){
//初始化栈
$ this-> stack = array_reverse($ values) ;
// stack只能包含这么多项目
$ this-> limit = $ limit;
}

public function push($ item){
//堆栈溢出陷阱
if(count($ this-> stack)< $ this - > limit){
//将数组前面的项添加到数组的开头
array_unshift($ this-> stack,$ item);
} else {
抛出新的RunTimeException('Stack is full!');
}
}

public function pop(){
if($ this-> isEmpty()){
// trap for stack underflow
抛出新的RunTimeException('Stack is empty!');
} else {
//从数组开头的pop项
return array_shift($ this-> stack);
}
}

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

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

快乐编码!


I want to construct a stack implemented in PHP. Initially I have this code:

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

And initialize the class normally using this:

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

This is correct and running. However, I want to initialize my stack with an initial value like this:

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

How can I implement this?


Note that all other functions (e.g pop and push) are functional.

解决方案

Here's the implementation of the correct stack class. To correctly initialize array to the value of a stack, you have to reverse the values of that array like this:

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

Happy coding!

这篇关于PHP堆栈实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆