当前位置:网站首页>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";
边栏推荐
- Just 1000 fans, record it
- MySQL -- convert rownum in Oracle to MySQL
- Development skills of rxjs observable custom operator
- Chaos engineering platform chaosblade box new heavy release
- Will your sleep service dream of the extra bookinfo on the service network
- Halcon extract orange (Orange)
- (POJ - 1984) navigation nightare (weighted and search set)
- P1042 [NOIP2003 普及组] 乒乓球
- 浏览器驱动的下载
- Which do you choose between Alibaba P7 with an annual salary of 900000 and deputy department level cadres?
猜你喜欢
Selenium element positioning method
Federated Search: all requirements in search
腾讯云 TStor 统一存储通过信通院首批文件存储基础能力评测
Whole house Wi Fi: a pain point that no one can solve?
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%
关于Flink框架窗口(window)函数最全解析
[to be continued] [UE4 notes] l5ue4 model import
[deep learning] simple implementation of neural network forward propagation
[Blue Bridge Cup] children's worship circle
In 2021, the global TCB adapter revenue was about $93 million, and it is expected to reach $315.5 million in 2028
随机推荐
Custom events, global event bus, message subscription and publishing, $nexttick
Default slot, named slot, scope slot
Do you know that there is an upper limit on the size of Oracle data files?
Word frequency statistics & sorting
QT how to set fixed size
Route (II)
一般来讲,如果频繁出现inconsistent tab and space的报错
selenium,元素操作以及浏览器操作方法
Start to write a small demo - three piece chess
YOLOv3&YOLOv5输出结果说明
Analysis of CPU surge in production environment service
selenium 在pycharm中安装selenium
C crystal report printing
On flow delivery between microservices
Selenium installing selenium in pycharm
错误:EACCES:权限被拒绝,访问“/usr/lib/node_modules”
Qt-制作一个简单的计算器-实现四则运算
Data consistency between redis and database
Story point vs. Human Sky
Common options of tcpdump command: Three