当前位置:网站首页>php链表创建和遍历
php链表创建和遍历
2022-07-02 10:55:00 【liuliang514218119】
<?php
#php链表创建和遍历
/** * Class node * 链表节点元素 */
class node
{
public $item; #当前节点存储元素
public $next; #指向下一个节点的地址
public function __construct($item, $next = null)
{
$this->item = $item;
$this->next = $next;
}
}
$node1 = new node(1);
$node2 = new node(2);
$node3 = new node(3);
$node1->next = $node2;
$node2->next = $node3;
#var_dump($node1);
# 头插法
function create_link_list_head($arr = [])
{
$head = new node($arr[0]); # 头节点
for ($i = 1; $i < count($arr); $i++) {
$node = new node($arr[$i]);
$node->next = $head; #当前节点的下一个节点指向头节点
$head = $node; #当前节点移为头节点
}
return $head;
}
$lk = create_link_list_head([1, 2, 3, 4, 5]);
print_link_list($lk);
echo "\n";
# 遍历打印链表元素
/** * @param $link_list node */
function print_link_list($link_list)
{
while ($link_list) {
print($link_list->item . ",");
$link_list = $link_list->next;
}
}
# 尾插法
function create_link_list_tail($arr = [])
{
$head = new node($arr[0]); # 头节点
$tail = $head; // 尾节点
for ($i = 1; $i < count($arr); $i++) {
$node = new node($arr[$i]);
$tail->next = $node; #尾节点的下一个节点指向当前节点
$tail = $node; #当前节点移为尾节点
}
return $head;
}
$lk = create_link_list_tail([1, 2, 3, 4, 5]);
print_link_list($lk);
echo "\n";

边栏推荐
- YOLOv3&YOLOv5输出结果说明
- Integral link, inertia link and proportion link in Simulink
- Development skills of rxjs observable custom operator
- The conference on the growth of all things was held in Hangzhou, and dangbei was selected into the top 100 list of future unicorns in China in 2022
- In 2021, the global styrene butadiene styrene (SBS) revenue was about $3722.7 million, and it is expected to reach $5679.6 million in 2028
- Subcontracting configuration of uniapp applet subpackages
- selenium 在pycharm中安装selenium
- OpenHarmony笔记-----------(四)
- Default slot, named slot, scope slot
- Federated Search: all requirements in search
猜你喜欢

Dangbei projection 4K laser projection X3 Pro received unanimous praise: 10000 yuan projector preferred

Selenium, element operation and browser operation methods

TeamTalk源码分析之win-client

Custom events, global event bus, message subscription and publishing, $nexttick

快解析:轻松实现共享上网

Launcher startup process

Analysis of CPU surge in production environment service

Browser driven Download

selenium,元素操作以及浏览器操作方法

代码实现MNLM
随机推荐
Default slot, named slot, scope slot
Systemserver process
YOLOv3&YOLOv5输出结果说明
Quarkus learning IV - project development to deployment
P1908 reverse sequence pair
The 29 year old programmer in Shanghai was sentenced to 10 months for "deleting the database and running away" on the day of his resignation!
[to be continued] [UE4 notes] l5ue4 model import
P1908 逆序对
【模板】最长公共子序列 (【DP or 贪心】板子)
Data Lake (11): Iceberg table data organization and query
MySQL45讲——学习极客时间MySQL实战45讲笔记—— 04 | 深入浅出索引(上)
OpenHarmony笔记-----------(四)
P1347 排序(拓扑 + spfa判断环 or 拓扑[内判断环])
P1042 [NOIP2003 普及组] 乒乓球
On flow delivery between microservices
P1042 [noip2003 popularization group] Table Tennis
[development environment] 010 editor tool (tool download | binary file analysis template template installation | shortcut key viewing and setting)
QT - make a simple calculator - realize four operations
Launcher启动过程
Data consistency between redis and database