1,518   PHP

class queue{
	private $arr=[];
	// 长度
	public function length(){
		return count($this->arr);
	}
	// 左边入队
	public function lpush($val){
		return array_unshift($this->arr,$val);
	}
        // 右边入队
	public function rpush($val){
		return array_push($this->arr,$val);
	}
        // 左边出队
	public function lpop(){
		return array_shift($this->arr);
	}
        // 右边出队
	public function rpop(){
		return array_pop($this->arr);
	}

        // 显示
	public function show(){
		var_dump($this->arr);
	}
}

$queue = new queue();
$queue->lpush(1);
$queue->lpush(2);
$queue->rpush(3);
$queue->rpush(4);

$queue->show();
var_dump($queue->lpop());
$queue->show();
var_dump($queue->rpop());
$queue->show();

var_dump($queue->length());



Leave a Reply

Your email address will not be published. Required fields are marked *