当前位置:网站首页>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";
边栏推荐
- Will your sleep service dream of the extra bookinfo on the service network
- Who is better, Qianyuan projection Xiaoming Q1 pro or Jimi new play? Which configuration is higher than haqu K1?
- Just 1000 fans, record it
- Selenium element positioning method
- 浏览器驱动的下载
- 【文档树、设置】字体变小
- How to use SAP's metadata framework (MDF) to build custom business rules?
- MySQL -- convert rownum in Oracle to MySQL
- P3008 [USACO11JAN]Roads and Planes G (SPFA + SLF优化)
- QT how to set fixed size
猜你喜欢
Will your sleep service dream of the extra bookinfo on the service network
抓包工具fiddler学习
错误:EACCES:权限被拒绝,访问“/usr/lib/node_modules”
每日学习3
2022家用投影仪首选!当贝F5强悍音画效果带来极致视听体验
Launcher startup process
Origin plots thermogravimetric TG and differential thermogravimetric DTG curves
你知道Oracle的数据文件大小有上限么?
Use of swagger
刚好1000粉丝,记录一下
随机推荐
Pattern matching and regular expressions in PostgreSQL - Das
c# 水晶报表打印
Route (II)
Selenium installing selenium in pycharm
Stone merging Board [interval DP] (ordinary stone Merging & Ring Stone merging)
[document tree, setting] font becomes smaller
Golang 快速生成数据库表的 model 和 queryset
selenium 在pycharm中安装selenium
【虹科技术分享】如何测试 DNS 服务器:DNS 性能和响应时间测试
2022 home projector preferred! Dangbei F5 brings the ultimate audio-visual experience with its powerful audio-visual effect
P1042 [noip2003 popularization group] Table Tennis
Will your sleep service dream of the extra bookinfo on the service network
Use of UIC in QT
2022家用投影仪首选!当贝F5强悍音画效果带来极致视听体验
Subcontracting configuration of uniapp applet subpackages
Who is better, Qianyuan projection Xiaoming Q1 pro or Jimi new play? Which configuration is higher than haqu K1?
P3008 [USACO11JAN]Roads and Planes G (SPFA + SLF优化)
Design of non main lamp: how to make intelligent lighting more "intelligent"?
TeamTalk源码分析之win-client
How to use SAP's metadata framework (MDF) to build custom business rules?