dongyue6199
2013-11-26 06:50
浏览 39
已采纳

PHP堆栈实现

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.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • dprxj1995 2013-11-26 07:00
    已采纳

    Change your constructor as follows:

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

    Just FYI, PHP has array_push (http://php.net/manual/en/function.array-push.php) and array_pop (http://us3.php.net/array_pop) implementations.

    打赏 评论
  • douqianmin5367 2013-11-26 06:54

    Simple, change your constructor:

    public function __construct($limit = 10, $values = array()) {
        // initialize the stack
        $this->stack = $values;
        // stack can only contain this many items
        $this->limit = $limit;
    }
    
    打赏 评论
  • duandang9434 2013-11-26 06:59

    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!

    打赏 评论
  • doumang20060820 2013-11-26 07:04

    Change the constructor to this. With this, you can supply no value, or a single value, or multiple values in an array. It will throw an error if the values count larger than the limit.

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

    There, hope it helps.

    打赏 评论
  • douhu2890 2019-06-18 16:35

    Stack using php(Procedural approach)

    $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();
    
    打赏 评论

相关推荐 更多相似问题