php怎么样实现链表?

发布时间:2025-02-26 点击:38
php实现链表的方法:首先定义一个节点类,代码为【function __construct($val=null)】;然后实现链表的实现类,代码为【function addatindex($index, $val)】。
php实现链表的方法:
首先定义一个节点类
class node{ public $val; public $next; function __construct($val=null){ $this->val = $val; $this->next = null; }}链表的实现类
class mylinkedlist { public $dummyhead; //定义一个虚拟的头结点 public $size; function __construct() { $this->dummyhead = new node(); $this->size = 0; } function get($index) { if($index < 0 || $index >= $this->size) return -1; $cur = $this->dummyhead; for($i = 0; $i < $index; $i ){ $cur = $cur->next; } return $cur->next->val; } function addathead($val) { $this->addatindex(0,$val); } function addattail($val) { $this->addatindex($this->size,$val); } function addatindex($index, $val) { if($index < 0 || $index > $this->size) return; $cur = $this->dummyhead; for($i = 0; $i < $index; $i ){ $cur = $cur->next; } $node = new node($val); $node->next = $cur->next; $cur->next = $node; $this->size ; } function deleteatindex($index) { if($index < 0 || $index >= $this->size) return; $cur = $this->dummyhead; for($i = 0; $i < $index; $i ){ $cur = $cur->next; } $cur->next = $cur->next->next; $this->size--; }}相关学习推荐:php编程从入门到精通

国内便宜点的云服务器
云服务器与独立服务器有什么区别
乐山弹性云服务器多少钱
鸿蒙系统首批升级机型有哪些 鸿蒙系统首批升级系统有p40mate30吗
服务器收到攻击-其他问题
阿里云网站服务器物理位置
网站建设怎么申请域名?有什么方法?
恶意抢注域名如何定义 恶意抢注域名会怎样