当前位置:网站首页>PHP linked list creation and traversal
PHP linked list creation and traversal
2022-07-02 14:16:00 【liuliang514218119】
<?php
#php Linked list creation and traversal
/** * Class node * Linked list node element */
class node
{
public $item; # The current node stores elements
public $next; # Point to the address of the next node
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);
# The first interpolation
function create_link_list_head($arr = [])
{
$head = new node($arr[0]); # Head node
for ($i = 1; $i < count($arr); $i++) {
$node = new node($arr[$i]);
$node->next = $head; # The next node of the current node refers to the head node
$head = $node; # The current node is moved to the head node
}
return $head;
}
$lk = create_link_list_head([1, 2, 3, 4, 5]);
print_link_list($lk);
echo "\n";
# Traverse and print linked list elements
/** * @param $link_list node */
function print_link_list($link_list)
{
while ($link_list) {
print($link_list->item . ",");
$link_list = $link_list->next;
}
}
# The tail interpolation
function create_link_list_tail($arr = [])
{
$head = new node($arr[0]); # Head node
$tail = $head; // Tail node
for ($i = 1; $i < count($arr); $i++) {
$node = new node($arr[$i]);
$tail->next = $node; # The next node of the tail node points to the current node
$tail = $node; # Move the current node to the tail node
}
return $head;
}
$lk = create_link_list_tail([1, 2, 3, 4, 5]);
print_link_list($lk);
echo "\n";
边栏推荐
- [development environment] 010 editor tool (tool download | binary file analysis template template installation | shortcut key viewing and setting)
- [USACO05JAN]Watchcow S(欧拉回路)
- 693. Travel sequencing (map + topology)
- YOLOv3&YOLOv5输出结果说明
- Characteristics of selenium
- 软件测试的方法
- Use bloc to build a page instance of shutter
- php链表创建和遍历
- Federated Search: all requirements in search
- Pattern matching and regular expressions in PostgreSQL - Das
猜你喜欢
Do you know that there is an upper limit on the size of Oracle data files?
Code implementation MNLM
PyQt5_ Qscrollarea content is saved as a picture
The most complete analysis of Flink frame window function
Launcher启动过程
联合搜索:搜索中的所有需求
【文档树、设置】字体变小
Analysis of CPU surge in production environment service
<口算练习机 方案开发原理图>口算练习机/口算宝/儿童数学宝/儿童计算器 LCD液晶显示驱动IC-VK1621B,提供技术支持
[Hongke technology sharing] how to test DNS server: DNS performance and response time test
随机推荐
P3008 [usaco11jan]roads and planes g (SPFA + SLF optimization)
关于Flink框架窗口(window)函数最全解析
Openharmony notes --------- (4)
kaggle如何使用utility script
Dangbei projection 4K laser projection X3 Pro received unanimous praise: 10000 yuan projector preferred
<口算练习机 方案开发原理图>口算练习机/口算宝/儿童数学宝/儿童计算器 LCD液晶显示驱动IC-VK1621B,提供技术支持
In 2021, the global revenue of structural bolts was about $796.4 million, and it is expected to reach $1097.6 million in 2028
Quarkus学习四 - 项目开发到部署
万物生长大会在杭召开,当贝入选2022中国未来独角兽TOP100榜单
Federated Search: all requirements in search
Error: eacces: permission denied, access to "/usr/lib/node_modules"
[USACO05JAN]Watchcow S(欧拉回路)
P1347 排序(拓扑 + spfa判断环 or 拓扑[内判断环])
Methods of software testing
Use bloc to build a page instance of shutter
自定义事件,全局事件总线,消息订阅与发布,$nextTick
数据湖(十一):Iceberg表数据组织与查询
Qt-制作一个简单的计算器-实现四则运算-将结果以对话框的形式弹出来
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
Pattern matching and regular expressions in PostgreSQL - Das