php实现链表的方法:首先定义一个节点类,代码为【function __construct($val=null)】;然后实现链表的实现类,代码为【function_construct $this->dummyhead = new nod】。
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编程从入门到精通
套路满满 电商售后暗藏陷阱学生买云服务器阿里云服务器租多少钱一年云服务器镜像选哪个css层叠样式表是什么网站访问不上-虚拟主机/数据库问题网站备案有哪些知识是需要了解的钉钉电脑版直播如何共享屏幕 电脑钉钉共享屏幕怎么弄