当前位置:网站首页>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";
边栏推荐
- Pattern matching and regular expressions in PostgreSQL - Das
- Chaos engineering platform chaosblade box new heavy release
- How many knowledge points can a callable interface have?
- 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
- 抓包工具fiddler学习
- Selenium element positioning method
- 故事點 vs. 人天
- Default slot, named slot, scope slot
- 2022家用投影仪首选!当贝F5强悍音画效果带来极致视听体验
- P1908 reverse sequence pair
猜你喜欢
【虹科技术分享】如何测试 DNS 服务器:DNS 性能和响应时间测试
MySQL 45 lecture - learning from the actual battle of geek time MySQL 45 Lecture Notes - 04 | easy to understand index (Part 1)
当贝投影4K激光投影X3 Pro获得一致好评:万元投影仪首选
Analysis of CPU surge in production environment service
你知道Oracle的数据文件大小有上限么?
2022家用投影仪首选!当贝F5强悍音画效果带来极致视听体验
[to be continued] [UE4 notes] l5ue4 model import
腾讯云 TStor 统一存储通过信通院首批文件存储基础能力评测
Teamtalk source code analysis win client
Selenium element positioning method
随机推荐
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
Use bloc to build a page instance of shutter
<口算练习机 方案开发原理图>口算练习机/口算宝/儿童数学宝/儿童计算器 LCD液晶显示驱动IC-VK1621B,提供技术支持
《可供方案开发》口算训练机/数学宝/儿童口算宝/智能数学宝 LCD液晶显示驱动IC-VK1622(LQFP64封装),原厂技术支持
P1347 sorting (topology + SPFA judgment ring or topology [inner judgment ring])
Do you know that there is an upper limit on the size of Oracle data files?
D language, possible 'string plug-ins'
go操作redis
Origin绘制热重TG和微分热重DTG曲线
一般来讲,如果频繁出现inconsistent tab and space的报错
Certik released the defi security report in 2021, disclosing key data of industry development (PDF download link attached)
Just 1000 fans, record it
php链表创建和遍历
Daily learning 3
Slashgear shares 2021 life changing technology products, which are somewhat unexpected
OpenHarmony笔记-----------(四)
Qt新项目_MyNotepad++
Qt原代码基本知识
Selenium element positioning method
How kaggle uses utility script