设为首页 加入收藏

TOP

用PHP实现双向队列
2014-11-23 23:26:58 来源: 作者: 【 】 浏览:5
Tags:PHP 实现 双向 队列

//双向队列的实现
class DoubleEndedQueue{
public $elements;
public function __construct(){//析构函数,创建一个数组
$this->elements = array();
}
public function push($element){//array_unshift() 函数在数组开头插入一个或多个元素。
array_unshift($this->elements , $element);
}
public function pop(){
return array_shift($this->elements);//PHP array_shift() 函数删除数组中的第一个元素_
}
public function inject($element){//给数组末尾追加元素,无指定下标,默认为数字
$this->elements[] = $element;
}
public function eject(){
array_pop($this->elements);//PHP array_pop() 函数删除数组中的最后一个元素
}
}


//实例化该类,测试下
$a=new DoubleEndedQueue();


$a->inject(‘aa’);//给数组末尾追加元素,无指定下标,默认为数字
$a->inject(‘dd’);
$a->inject(‘cc’);
$a->inject(‘dd’);
$a->push(’111′);//函数在数组开头插入一个或多个元素。
$a->pop();//PHP array_shift() 函数删除数组中的第一个元素_
$a->eject();//PHP array_pop() 函数删除数组中的最后一个元素
print_r($a->elements);
>


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Java中对文件进行操作的类都有哪.. 下一篇测试报告的主要内容都有哪些?

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: