工作流模式的链表模型类
工作流的线性结构正好和数据结构的链表类似,于是就可以用链表来实现这个业务。
<?php function string_to_array($string,$delimiter=",") { if(is_string($string)) { $string=array_filter(explode($delimiter,$string)); } return $string; } function array_to_string($array=array(),$delimiter=",") { if(is_array($array)) { $array=implode($delimiter,$array); } return $array; } class MyQueue { private $total_array_list=array();//总的数组链表 private $progress_array_list=array();//已经完成的任务链表 private $current_index=0;//当前链表指针位置 private $prev_point=0;//指针上一位置 private $next_point=0;//指针下一位置 public function __construct($leader_list="",$checked_leader_list="") { $this->total_array_list=string_to_array($leader_list); $this->progress_array_list=string_to_array($checked_leader_list); $progress_length=count($this->progress_array_list);//当前完成进度 $this->current_index=$progress_length;//指针位置 $this->prev_point=isset($this->total_array_list[$this->current_index-1])?$this->total_array_list[$this->current_index-1]:null; $this->next_point=isset($this->total_array_list[$this->current_index])?$this->total_array_list[$this->current_index]:null; } /* * 队列链表指针向前移动 * **/ public function forward() { //链表向前移动 if(is_null($this->next_point)) { //到了最后一个任务了 不可继续向前 } else { $this->progress_array_list[]=$this->total_array_list[$this->current_index]; $this->current_index++; $this->prev_point=isset($this->total_array_list[$this->current_index-1])?$this->total_array_list[$this->current_index-1]:null; $this->next_point=isset($this->total_array_list[$this->current_index])?$this->total_array_list[$this->current_index]:null; } return $this; } /* * 队列链表指针向后移动 * **/ public function back_off() { //链表向后移动 if(is_null($this->prev_point)) { //到了第一个任务了 不可继续向后 } else { array_pop($this->progress_array_list); $this->current_index--; $this->prev_point=isset($this->total_array_list[$this->current_index-1])?$this->total_array_list[$this->current_index-1]:null; $this->next_point=isset($this->total_array_list[$this->current_index])?$this->total_array_list[$this->current_index]:null; } return $this; } /* * 拿到链表的信息 * **/ public function get_param() { $leader_list=array_to_string($this->total_array_list); $checked_leader_list=array_to_string($this->progress_array_list); $prev_leader=$this->prev_point; $next_leader=$this->next_point; return compact( "leader_list",//审核人列表 "checked_leader_list",//已经通过审核人的列表 "prev_leader",//上一审核人 "next_leader",//下一审核人 ); } }
我定义了两个方法来对字符串和数组进行转换和切割,封装代替了 implode explode
然后定义MyQueue 来作为链表操作的类。具体使用的方法如下:
<?php $obj=new MyQueue("1,2,3,4,5","1,2,3");//生成一个队列链表 $result=$obj->forward()->get_param();//指针向前移动 // $result=$obj->back_off()->get_param();//指针向后移动 var_dump($result); ?>
使用起来是很方便的。小小的骄傲一下,嘿嘿。
分类:
杀鸡要用宰牛刀
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战