- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章利用PHP实现递归删除链表元素的方法示例由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
前言 。
这篇文章介绍一下 递归,递归的本质是将原来的问题转化为更小的同一个问题,解决这些更小问题的过程。下面通过两个递归的例子帮助学习对递归的理解.
1.递归数组求和 。
例如某个数组 $arr = [1,2,3,4,5,6,7,8,9,10]; 需要求和,通过实现递归函数对数组求和来帮助学习对递归的理解.
1.1 输出文件 output_recursion.php 。
1
2
3
4
5
6
7
|
<?php
require
'ArrayRecursion.php'
;
/**
* 递归实现数组求和
*/
$arr
= [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
echo
ArrayRecursion::recursionSum(
$arr
);
|
1.2 ArrayRecursion 类 。
这是一个实现数组递归求和的代码,其中 recursionSum() 是一个递归函数,相当于把求和过程转化为更小的求和,最终实现想要的结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<?php
/**
* 使用递归对数组求和 方便对递归的理解
* Class ArrayRecursion
*/
class
ArrayRecursion
{
public
static
function
sum(
array
$arr
) {
return
self::recursionSum(
$arr
);
}
public
static
function
recursionSum(
array
$arr
,
$i
= 0) {
if
(
count
(
$arr
) ==
$i
) {
return
0;
}
return
$arr
[
$i
] + self::recursionSum(
$arr
,
$i
+ 1);
}
}
|
Tips:这个求和过程仅仅只是帮助学习递归思想,实际求和可以直接遍历数组.
2.递归删除链表某个元素 。
例如某个链表 10->9->8->99->7->99->6->5->99->4->3->2->1->null 需要删除其中值等于 99 的元素,可以通过实现递归来得到删除指定元素的效果.
2.1 输出文件 output_recursion.php 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
<?php
require
'LinkedList.php'
;
require
'LinkedListRecursion.php'
;
/**
* 首先实例化一个链表,向链表中添加50个元素
*/
$linkedList
=
new
LinkedList();
for
(
$i
= 0;
$i
< 50;
$i
++) {
if
(
$i
% 7 == 0) {
$linkedList
->addFirst(99);
}
else
{
$linkedList
->addFirst(
$i
);
}
}
echo
$linkedList
->toString();
/**打印链表中元素
* 99->48->47->46->45->44->43->99->41->40->39->
* 38->37->36->99->34->33->32->31->30->29->99->27->
* 26->25->24->23->22->99->20->19->18->17->16->15->
* 99->13->12->11->10->9->8->99->6->5->4->3->2->1->99->null
*/
//将链表对象传入一个能删除指定元素的方法,如 99
echo
LinkedListRecursion::deleteElement(
$linkedList
, 99)->toString();
/**打印
* 48->47->46->45->44->43->41->40->
* 39->38->37->36->34->33->32->31->
* 30->29->27->26->25->24->23->22->
* 20->19->18->17->16->15->13->12->
* 11->10->9->8->6->5->4->3->2->1->null
*/
|
2.2 LinkedList & Node 链表类 。
这是一个链表类,可以使用 addFirst() 方法向链表头部添加元素,可使用 getHead() 获取链表 head 节点对象信息,可以使用 setHead() 改变 head,另外下面定义了一个链表节点类 Node:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
<?php
/**
* 链表的实现
* Class LinkedList
*/
class
LinkedList
{
private
$dummyHead
;
private
$size
;
/**
* 初始化链表 null->null
* LinkedList constructor.
*/
public
function
__construct() {
$this
->dummyHead =
new
Node(null, null);
$this
->size = 0;
}
/**
* 获取链表大小
* @return int
*/
public
function
getSize(): int {
return
$this
->size;
}
/**
* 判断链表是否为空
* @return bool
*/
public
function
isEmpty(): bool {
return
$this
->size == 0;
}
/**
* 在链表的第 index 位置添加元素
* @param int $index
* @param $e
*/
public
function
add(int
$index
,
$e
): void {
if
(
$index
< 0 ||
$index
>
$this
->size) {
echo
"索引范围错误"
;
exit
;
}
$prve
=
$this
->dummyHead;
for
(
$i
= 0;
$i
<
$index
;
$i
++) {
$prve
=
$prve
->next;
}
//将上插入位置的上一个位置的 next 节点指向插入节点,插入节点的 next 节点信息指向原上节点的 next 节点
$prve
->next =
new
Node(
$e
,
$prve
->next);
$this
->size++;
}
/**
* 向链表开头添加元素
* @param $e
*/
public
function
addFirst(
$e
): void {
$this
->add(0,
$e
);
}
/**
* 向链表末尾添加元素
* @param $e
*/
public
function
addLast(
$e
): void {
$this
->add(
$this
->size,
$e
);
}
/**
* 获取链表第 index 位置元素
* @param $index
*/
public
function
get(
$index
) {
if
(
$index
< 0 ||
$index
>
$this
->size) {
echo
"索引范围错误"
;
exit
;
}
$node
=
$this
->dummyHead;
for
(
$i
= 0;
$i
<
$index
+ 1;
$i
++) {
$node
=
$node
->next;
}
return
$node
->e;
}
/**
* 获取链表第一个元素
* @return mixed
*/
public
function
getFirst() {
return
$this
->get(0);
}
/**
* 获取链表最后一个元素
* @return mixed
*/
public
function
getLast() {
return
$this
->get(
$this
->size - 1);
}
/**
* 修改链表中第 index 位置元素值
* @param $index
* @param $e
*/
public
function
update(
$index
,
$e
) {
if
(
$index
< 0 ||
$index
>
$this
->size) {
echo
"索引范围错误"
;
exit
;
}
$node
=
$this
->dummyHead;
for
(
$i
= 0;
$i
<
$index
+ 1;
$i
++) {
$node
=
$node
->next;
}
$node
->e =
$e
;
}
/**
* 判断链表中是否存在某个元素
* @param $e
* @return bool
*/
public
function
contains(
$e
): bool {
for
(
$node
=
$this
->dummyHead->next;
$node
!= null;
$node
=
$node
->next) {
if
(
$node
->e ==
$e
) {
return
true;
}
}
return
true;
}
/**
* 删除链表中第 index 位置元素
* @param $index
*/
public
function
remove(
$index
) {
if
(
$index
< 0 ||
$index
>
$this
->size) {
echo
"索引范围错误"
;
exit
;
}
if
(
$this
->size == 0) {
echo
"链表已经是空"
;
exit
;
}
$prve
=
$this
->dummyHead;
for
(
$i
= 0;
$i
<
$index
;
$i
++) {
$prve
=
$prve
->next;
}
$node
=
$prve
->next;
$prve
->next =
$node
->next;
$this
->size--;
return
$node
->e;
}
/**
* 删除链表头元素
*/
public
function
removeFirst() {
return
$this
->remove(0);
}
/**
* 删除链表末尾元素
*/
public
function
removeLast() {
return
$this
->remove(
$this
->size - 1);
}
/**
* 获取头结点信息
* @return mixed
*/
public
function
getHead() {
return
$this
->dummyHead->next;
}
/**
* 设置头
* @param Node $head
*/
public
function
setHead(Node
$head
) {
$this
->dummyHead->next =
$head
;
}
/**
* 链表元素转化为字符串显示
* @return string
*/
public
function
toString(): string {
$str
=
""
;
for
(
$node
=
$this
->dummyHead->next;
$node
!= null;
$node
=
$node
->next) {
$str
.=
$node
->e .
"->"
;
}
return
$str
.
"null"
;
}
}
class
Node
{
public
$e
;
//节点元素
public
$next
;
//下个节点信息
/**
* 构造函数 设置节点信息
* Node constructor.
* @param $e
* @param $next
*/
public
function
__construct(
$e
,
$next
) {
$this
->e =
$e
;
$this
->next =
$next
;
}
}
|
2.3 LinkedListRecursion 类 。
这个类定义了一个 deleteElement(LinkedList $linkedList, $val) 方法可以将传进的链表类中指定元素值的节点删除掉(实际是节点的 next 重新指向),recursionDelete($head, $val) 方法是一个递归函数,它能递归删除 head 中指定元素值等于 $val 的节点删除
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
<?php
/**
* 递归删除链表指定元素
* Class LinkedListRecursion
*/
class
LinkedListRecursion
{
public
static
function
deleteElement(LinkedList
$linkedList
,
$val
) {
$linkedList
->setHead(self::recursionDelete(
$linkedList
->getHead(),
$val
));
return
$linkedList
;
}
/**
* 递归函数 递归删除链表元素
* @param $head
* @param $val
* @return null
*/
private
static
function
recursionDelete(
$head
,
$val
) {
if
(
$head
== null) {
return
null;
}
else
{
if
(
$head
->e ==
$val
) {
return
self::recursionDelete(
$head
->next,
$val
);
}
else
{
$head
->next = self::recursionDelete(
$head
->next,
$val
);
return
$head
;
}
}
}
}
|
代码仓库 :https://gitee.com/love-for-po... 。
总结 。
到此这篇关于利用PHP实现递归删除链表元素的文章就介绍到这了,更多相关PHP递归删除链表元素内容请搜索我以前的文章或继续浏览下面的相关文章希望大家以后多多支持我! 。
原文链接:https://segmentfault.com/a/1190000037572830 。
最后此篇关于利用PHP实现递归删除链表元素的方法示例的文章就讲到这里了,如果你想了解更多关于利用PHP实现递归删除链表元素的方法示例的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Sample data for IPv6? 除了 wireshark 在其网站上提供的内容之外,是否有可以下
我正在寻找可以集成到现有应用程序中并使用多拖放功能的示例或任何现成的解决方案。我在互联网上找到的大多数解决方案在将多个项目从 ListBox 等控件拖放到另一个 ListBox 时效果不佳。谁能指出我
我是 GATE Embedded 的新手,我尝试了简单的示例并得到了 NoClassDefFoundError。首先我会解释我尝试了什么 在 D:\project\gate-7.0 中下载并提取 Ga
是否有像 Eclipse 中的 SWT 示例那样的多合一 JFace 控件示例?搜索(在 stackoverflow.com 上使用谷歌搜索和搜索)对我没有帮助。 如果它是一个独立的应用程序或 ecl
我找不到任何可以清楚地解释如何通过 .net API(特别是 c#)使用谷歌计算引擎的内容。有没有人可以指点我什么? 附言我知道 API 引用 ( https://developers.google.
最近在做公司的一个项目时,客户需要我们定时获取他们矩阵系统的数据。在与客户进行对接时,提到他们的接口使用的目前不常用的BASIC 认证。天呢,它好不安全,容易被不法人监听,咋还在使用呀。但是没办法呀,
最近在做公司的一个项目时,客户需要我们定时获取他们矩阵系统的数据。在与客户进行对接时,提到他们的接口使用的目前不常用的BASIC 认证。天呢,它好不安全,容易被不法人监听,咋还在使用呀。但是没办法呀,
我正在尝试为我的应用程序设计配置文件格式并选择了 YAML。但是,这(显然)意味着我需要能够定义、解析和验证正确的 YAML 语法! 在配置文件中,必须有一个名为 widgets 的集合/序列。 .这
你能给我一个使用 pysmb 库连接到一些 samba 服务器的例子吗?我读过有类 smb.SMBConnection.SMBConnection(用户名、密码、my_name、remote_name
linux服务器默认通过22端口用ssh协议登录,这种不安全。今天想做限制,即允许部分来源ip连接服务器。 案例目标:通过iptables规则限制对linux服务器的登录。 处理方法:编
我一直在寻找任何 PostProjectAnalysisTask 工作代码示例,但没有看。 This页面指出 HipChat plugin使用这个钩子(Hook),但在我看来它仍然使用遗留的 Po
我发现了 GWT 的 CustomScrollPanel 以及如何自定义滚动条,但我找不到任何示例或如何设置它。是否有任何示例显示正在使用的自定义滚动条? 最佳答案 这是自定义 native 滚动条的
我正在尝试开发一个 Backbone Marionette 应用程序,我需要知道如何以最佳方式执行 CRUD(创建、读取、更新和销毁)操作。我找不到任何解释这一点的资源(仅适用于 Backbone)。
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题?通过 editing this post 添加详细信息并澄清问题. 去年关闭。 Improve this
我需要一个提交多个单独请求的 django 表单,如果没有大量定制,我找不到如何做到这一点的示例。即,假设有一个汽车维修店使用的表格。该表格将列出商店能够进行的所有可能的维修,并且用户将选择他们想要进
我有一个 Multi-Tenancy 应用程序。然而,这个相同的应用程序有 liquibase。我需要在我的所有数据源中运行 liquibase,但是我不能使用这个 Bean。 我的应用程序.yml
我了解有关单元测试的一般思想,并已在系统中发生复杂交互的场景中使用它,但我仍然对所有这些原则结合在一起有疑问。 我们被警告不要测试框架或数据库。好的 UI 设计不适合非人工测试。 MVC 框架不包括一
我正在使用 docjure并且它的 select-columns 函数需要一个列映射。我想获取所有列而无需手动指定。 如何将以下内容生成为惰性无限向量序列 [:A :B :C :D :E ... :A
$condition使用说明和 $param在 findByAttributes在 Yii 在大多数情况下,这就是我使用 findByAttributes 的方式 Person::model()->f
我在 Ubuntu 11.10 上安装了 qtcreator sudo apt-get install qtcreator 安装的版本有:QT Creator 2.2.1、QT 4.7.3 当我启动
我是一名优秀的程序员,十分优秀!