本文介绍递归。递归的本质是将原问题转化为更小的相同问题,并求解这些更小的问题的过程。下面两个递归的例子有助于学习递归的理解。1.递归数组求和比如数组$arr=[1,2,3,4,5,6,7,8,9,10];求和是必需的,并且通过实现递归函数来对数组求和递归的理解来促进学习。1.1输出文件output_recursion.php9->8->99->7->99->6->5->99->4->3->2->1->null需要删除值为99的元素,通过递归实现删除指定元素的效果。2.1输出文件output_recursion.phpaddFirst(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*///将链表对象传入可以删除指定元素的方法,如99echoLinkedListRecursion::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.2LinkedList&Node链表类这是一个链表类,可以使用addFirst()方法向链表头部添加元素链表,可以使用getHead()获取链表的头节点对象信息,可以使用setHead()改变头部。另外,下面定义了一个链表节点类Node:null*LinkedList构造函数。*/publicfunction__construct(){$this->dummyHead=newNode(null,null);$this->size=0;}/***获取链表的大小*@returnint*/publicfunctiongetSize():int{return$this->size;}/***判断链表是否为空*@returnbool*/publicfunctionisEmpty():bool{return$this->size==0;}/***在链表的索引位置添加一个元素*@paramint$index*@param$e*/publicfunctionadd(int$index,$e):void{if($index<0||$index>$this->size){echo"索引范围错误";出口;}$prve=$this->dummyHead;对于($i=0;$i<$index;$i++){$prve=$prve->next;}//将插入位置上一个位置的下一个节点指向插入节点,插入节点的下一个节点信息指向上一个节点的下一个节点$prve->next=newNode($e,$prve->下一个);$这个->尺寸++;}/***添加元素到链表的开头*@param$e*/publicfunctionaddFirst($e):void{$this->add(0,$e);}/***添加一个元素到链表的末尾*@param$e*/publicfunctionaddLast($e):void{$this->add($this->size,$e);}/***获取链表索引位置的元素*@param$index*/publicfunctionget($index){if($index<0||$index>$this->size){echo“索引范围错误”;出口;}$node=$this->dummyHead;对于($i=0;$i<$index+1;$i++){$node=$node->next;}返回$node->e;}/***获取链表的第一个元素*@returnmixed*/publicfunctiongetFirst(){return$this->get(0);}/***获取链表的最后一个元素*@returnmixed*/publicfunctiongetLast(){return$this->get($this->size-1);}/***修改链表中index位置的元素值*@param$index*@param$e*/publicfunctionupdate($index,$e){if($index<0||$index>$this->size){echo"索引范围错误";出口;}$node=$this->dummyHead;对于($i=0;$i<$index+1;$i++){$node=$node->next;}$node->e=$e;}/***判断是否有元素*@param$e*@returnbool*/publicfunctioncontains($e):bool{for($node=$this->dummyHead->next;$node!=null;$node=$node->next){if($node->e==$e){返回真;}}返回真;}/***删除链表中index位置的元素*@param$index*/publicfunctionremove($index){if($index<0||$index>$this->size){echo"索引范围错误";出口;}if($this->size==0){echo"列表为空";出口;}$prve=$this->dummyHead;对于($i=0;$i<$index;$i++){$prve=$prve->next;}$node=$prve->next;$prve->next=$node->next;$这个->大小--;返回$node->e;}/***删除链表的头元素*/publicfunctionremoveFirst(){return$this->remove(0);}/***删除链表的最后一个元素*/publicfunctionremoveLast(){return$this->remove($this->size-1);}/***获取头节点信息*@returnmixed*/publicfunctiongetHead(){return$this->dummyHead->next;}/***设置标题*@paramNode$head*/publicfunctionsetHead(Node$head){$this->dummyHead->next=$head;}/***链表元素转换为字符串显示*@returnstring*/publicfunctiontoString():string{$str="";对于($node=$this->dummyHead->next;$node!=null;$node=$node->next){$str.=$node->e.“->”;}返回$str。“无效的”;}}classNode{public$e;//节点元素public$next;//下一个节点信息/***构造函数设置节点信息*节点构造函数。*@param$e*@param$next*/publicfunction__construct($e,$next){$this->e=$e;$this->next=$next;}}2.3LinkedListRecursion类该类定义了一个deleteElement(LinkedList$linkedList,$val)方法可以删除传入链表类中指定元素值的节点(实际上是重新指向该节点的下一个点)。recursionDelete($head,$val)方法是一个递归函数,可以递归删除head中指定的节点删除元素值等于$val的节点:setHead(self::recursionDelete($linkedList->getHead(),$val));返回$链表;}/***递归函数递归删除链表元素*@param$head*@param$val*@returnnull*/privatestaticfunctionrecursionDelete($head,$val){if($head==null){返回空值;}else{if($head->e==$val){returnself::recursionDelete($head->next,$val);}else{$head->next=self::recursionDelete($head->next,$val);返回$头;}}}}代码仓库:https://gitee.com/love-for-po...扫描二维码关注爱音诗仙公众号
