PHP實現(xiàn)鏈表的定義與反轉功能示例
本文實例講述了PHP實現(xiàn)鏈表的定義與反轉功能。分享給大家供大家參考,具體如下:
PHP定義鏈表及添加、移除、遍歷等操作:
<?php
class Node
{
private $Data;//節(jié)點數(shù)據(jù)
private $Next;//下一節(jié)點
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;//頭節(jié)點
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--要添加節(jié)點的數(shù)據(jù)
*
*/
public function add($data)
{
$node=$this->header;
while($node->getNext()!=null)
{
$node=$node->getNext();
}
$node->setNext(new Node($data,null));
}
/**
*@author MzXy
*@param $data--要移除節(jié)點的數(shù)據(jù)
*
*/
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("數(shù)據(jù)集為空!");
return;
}
while($node->getNext()!=null)
{
print('['.$node->getNext()->getData().'] -> ');
if($node->getNext()->getNext()==null){break;}
$node=$node->getNext();
}
}
/**
*@author MzXy
*@param $data--要訪問的節(jié)點的數(shù)據(jù)
* @param 此方法只是演示不具有實際意義
*
*/
public function getAt($data)
{
$node=$this->header->getNext();
if($node->getNext()==null){
print("數(shù)據(jù)集為空!");
return;
}
while($node->getData()!=$data)
{
if($node->getNext()==null){break;}
$node=$node->getNext();
}
return $node->getData();
}
/**
*@author MzXy
*@param $value--需要更新的節(jié)點的原數(shù)據(jù) --$initial---更新后的數(shù)據(jù)
*
*/
public function update($initial,$value)
{
$node=$this->header->getNext();
if($node->getNext()==null){
print("數(shù)據(jù)集為空!");
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. 常用的方法:左右交替,下一個結點保存,上一個結點替換該結點的下個結點。實現(xiàn)替換。
代碼:
function ReverseList($pHead)
{
// write code here
if($pHead == null || $pHead->next == null){
return $pHead;
}
$p = $pHead;
$q = $pHead->next;
$pHead->next = null;//$pHead 變?yōu)槲仓羔?
while($q){
$r = $q->next;
$q->next = $p;
$p = $q;
$q = $r;
}
return $p;
}
2. 使用遞歸方法。三個結點,頭結點,首節(jié)點,第二個結點。把首節(jié)點后面的所有結點當成第二個結點,依次循環(huán)下去,由于要滿足 $pHead != null || $pHead->next != null ;所以不會出現(xiàn)遍歷不完的情況
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相關內容感興趣的讀者可查看本站專題:《PHP數(shù)據(jù)結構與算法教程》、《php程序設計算法總結》、《php字符串(string)用法總結》、《PHP數(shù)組(Array)操作技巧大全》、《PHP常用遍歷算法與技巧總結》及《PHP數(shù)學運算技巧總結》
希望本文所述對大家PHP程序設計有所幫助。
相關文章
php+mysql+jquery實現(xiàn)簡易的檢索自動補全提示功能
這篇文章主要介紹了php+mysql+jquery實現(xiàn)簡易的檢索自動補全提示功能,涉及基于jQuery的ajax數(shù)據(jù)庫查詢與事件動態(tài)響應相關操作技巧,需要的朋友可以參考下2017-04-04
php性能優(yōu)化進階不要在for循環(huán)中操作DB
這篇文章主要為大家介紹了php性能優(yōu)化進階不要在for循環(huán)中操作DB,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06

