php 数据结构之链表队列
来源:爱站网时间:2020-02-20编辑:网友分享
队列是先进先出的线性表,它只允许在表的前端删除和在表的后端插入,下面爱站技术频道小编就来为大家分析php 数据结构之链表队列,希望能帮到您。
队列是先进先出的线性表,它只允许在表的前端删除和在表的后端插入,下面爱站技术频道小编就来为大家分析php 数据结构之链表队列,希望能帮到您。
php 链表队列
实例代码:
class Queue{
private $last;
private $first;
private $oldfirst;
private static $n=0;
public function __construct(){
$this->last = null;
$this->first = null;
$this->oldfirst = null;
}
public function push($item){
$this->oldfirst = $this->last;
$this->last = new Node();
$this->last->item = $item;
$this->last->next = null;
if(empty($this->first)){
$this->first = $this->last;
}else{
$this->oldfirst->next = $this->last;
}
self::$n++;
}
public function pop(){
if(self::$n<0){
return null;
}
$item = $this->first->item;
$this->first = $this->first->next;
self::$n--;
return $item;
}
}
class Node{
public $item;
public $next;
}
$Queue = new Queue();
$Queue->push("a");
$Queue->push("b");
$Queue->push("c");
echo $Queue->pop().PHP_EOL;
echo $Queue->pop().PHP_EOL;
echo $Queue->pop().PHP_EOL;
echo $Queue->pop().PHP_EOL;
php 数据结构之链表队列,大家都清楚吗?当然,学习的方法多种多样,大家想要继续了解的话,可以关注js.aizhan.com吧!
下一篇:PHP实现的贪婪算法实例
