当前位置:网站首页>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";

边栏推荐
- Codeforces Round #803 (Div. 2)(A~D)
- 【文档树、设置】字体变小
- 错误:EACCES:权限被拒绝,访问“/usr/lib/node_modules”
- P3008 [USACO11JAN]Roads and Planes G (SPFA + SLF优化)
- 693. Travel sequencing (map + topology)
- [deep learning] simple implementation of neural network forward propagation
- docker mysql
- 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
- The global special paper revenue in 2021 was about $27 million, and it is expected to reach $35 million in 2028. From 2022 to 2028, the CAGR was 3.8%
- 如何设置Qt手工布局
猜你喜欢

Will your sleep service dream of the extra bookinfo on the service network

Qt-制作一个简单的计算器-实现四则运算

Codeforces Round #803 (Div. 2)(A~D)

错误:EACCES:权限被拒绝,访问“/usr/lib/node_modules”

Subcontracting configuration of uniapp applet subpackages

Design of non main lamp: how to make intelligent lighting more "intelligent"?

Tencent cloud tstor unified storage passed the evaluation of the first batch of basic file storage capabilities of the ICT Institute

Packet capturing tool Fiddler learning

万物生长大会在杭召开,当贝入选2022中国未来独角兽TOP100榜单

qt中uic的使用
随机推荐
路由(二)
浏览器驱动的下载
uniapp小程序 subPackages分包配置
P1347 sorting (topology + SPFA judgment ring or topology [inner judgment ring])
【模板】最长公共子序列 (【DP or 贪心】板子)
Use of swagger
Solve the problem that openocd fails to burn STM32 and cannot connect through SWD
HMS core machine learning service helps zaful users to shop conveniently
693. Travel sequencing (map + topology)
PyQt5_QScrollArea内容保存成图片
[to be continued] [UE4 notes] l5ue4 model import
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
[template] longest common subsequence ([DP or greedy] board)
Using computed in uni app solves the abnormal display of data () value in tab switching
Selenium, element operation and browser operation methods
Yolov3 & yolov5 output result description
Launcher启动过程
MySQL 45 lecture - learning the actual battle of MySQL in Geek time 45 Lecture Notes - 05 | easy to understand index (Part 2)
docker mysql
卷积神经网络(入门)