这篇文章主要为大家详细介绍了使用PHP怎么实现链表的定义与反转,文中示例代码介绍的非常详细,具有一定的参考价值,发现的小伙伴们可以参考一下:
php是一个嵌套的缩写名称,指的是英文超级文本预处理语言(php:Hypertext Preprocessor)的缩写,它的语法混合了C、Java、Perl以及php自创新的语法,主要用来做网站开发,许多小型网站都用php开发,因为php是开源的,从而使得php经久不衰。
PHP定义链表及添加、移除、遍历等操作:
<?php
class Node
{
private $Data;//节点数据
private $Next;//下一节点
public function setData($value){
$this->Data=$value;
}
public function setNext($value){
$this->Next=$value;
}
public function getData(){
return $this->Data;
}
public function getNext(){
return $this->Next;
}
public function __construct($data,$next){
$this->setData($data);
$this->setNext($next);
}
}
class LinkList
{
private $header;//头节点
private $size;//长度
public function getSize()
{
$i=0;
$node=$this->header;
while($node->getNext()!=null)
{
$i++;
$node=$node->getNext();
}
return $i;
}
public function setHeader($value){
$this->header=$value;
}
public function getHeader(){
return $this->header;
}
public function __construct(){
header("content-type:text/html; charset=utf-8");
$this->setHeader(new Node(null,null));
}
/**
*@author MzXy
*@param $data--要添加节点的数据
*
*/
public function add($data)
{
$node=$this->header;
while($node->getNext()!=null)
{
$node=$node->getNext();
}
$node->setNext(new Node($data,null));
}
/**
*@author MzXy
*@param $data--要移除节点的数据
*
*/
public function removeAt($data)
{
$node=$this->header;
while($node->getData()!=$data)
{
$node=$node->getNext();
}
$node->setNext($node->getNext());
$node->setData($node->getNext()->getData());
}
/**
*@author MzXy
*@param 遍历
*
*/
public function get()
{
$node=$this->header;
if($node->getNext()==null){
print("数据集为空!");
return;
}
while($node->getNext()!=null)
{
print('['.$node->getNext()->getData().'] -> ');
if($node->getNext()->getNext()==null){break;}
$node=$node->getNext();
}
}
/**
*@author MzXy
*@param $data--要访问的节点的数据
* @param 此方法只是演示不具有实际意义
*
*/
public function getAt($data)
{
$node=$this->header->getNext();
if($node->getNext()==null){
print("数据集为空!");
return;
}
while($node->getData()!=$data)
{
if($node->getNext()==null){break;}
$node=$node->getNext();
}
return $node->getData();
}
/**
*@author MzXy
*@param $value--需要更新的节点的原数据 --$initial---更新后的数据
*
*/
public function update($initial,$value)
{
$node=$this->header->getNext();
if($node->getNext()==null){
print("数据集为空!");
return;
}
while($node->getData()!=$data)
{
if($node->getNext()==null){break;}
$node=$node->getNext();
}
$node->setData($initial);
}
}
$lists = new LinkList();
$lists -> add(1);
$lists -> add(2);
$lists -> get();
echo '<pre>';
print_r($lists);
echo '</pre>';
?>
反转链表操作:
1. 常用的方法:左右交替,下一个结点保存,上一个结点替换该结点的下个结点。实现替换。
代码:
function ReverseList($pHead)
{
// write code here
if($pHead == null || $pHead->next == null){
return $pHead;
}
$p = $pHead;
$q = $pHead->next;
$pHead->next = null;//$pHead 变为尾指针
while($q){
$r = $q->next;
$q->next = $p;
$p = $q;
$q = $r;
}
return $p;
}
2. 使用递归方法。三个结点,头结点,首节点,第二个结点。把首节点后面的所有结点当成第二个结点,依次循环下去,由于要满足 $pHead != null || $pHead->next != null
;所以不会出现遍历不完的情况
function ReverseList($pHead)
{
// write code here
if($pHead == null || $pHead->next == null){
return $pHead;
}
$res = ReverseList($pHead->next);
$pHead->next->next = $pHead;
$pHead->next = null;
return $res;
}
以上就是亿速云小编为大家收集整理的使用PHP怎么实现链表的定义与反转,如何觉得亿速云网站的内容还不错,欢迎将亿速云网站推荐给身边好友。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。