ITeye博客

PHP实现的栈

keshion · 2012-11-16 13:44 · PHP · 编程语言 · Markdown

<?php

class Stack{
    private $_data = array();
    private $_end = null;

    public function push($data){
        if($this->_end === null){
            $this->_end = 0;
        }else{
            $this->_end++;
        }
        $this->_data[$this->_end] = $data;
    }

    public function pop(){
        if(empty($this->_data)){return false;}
        
        $ret = $this->_data[$this->_end];

        //array_pop($this->_data);
        array_splice($this->_data, $this->_end);

        $this->_end--;
        
        return $ret;
    }

    public function get_data(){
        return $this->_data;
    }

}

$stack = new Stack();
$stack->push('a');
$stack->push('b');
$stack->push('c');
$stack->pop();
print_r($stack->get_data());

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。