首页 > 开发 > PHP > 正文

php 数据结构之链表队列

2024-05-04 22:04:25
字体:
来源:转载
供稿:网友

队列是先进先出的线性表,它只允许在表的前端删除和在表的后端插入,下面错新技术频道小编就来为大家分析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.VeVb.com吧!

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表