PHP實(shí)現(xiàn)鏈表的定義與反轉(zhuǎn)功能示例
本文實(shí)例講述了PHP實(shí)現(xiàn)鏈表的定義與反轉(zhuǎn)功能。分享給大家供大家參考,具體如下:
PHP定義鏈表及添加、移除、遍歷等操作:
<?php
class Node
{
private $Data;//節(jié)點(diǎn)數(shù)據(jù)
private $Next;//下一節(jié)點(diǎn)
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é)點(diǎn)
private $size;//長(zhǎng)度
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é)點(diǎn)的數(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é)點(diǎn)的數(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--要訪問(wèn)的節(jié)點(diǎn)的數(shù)據(jù)
* @param 此方法只是演示不具有實(shí)際意義
*
*/
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é)點(diǎn)的原數(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>';
?>
反轉(zhuǎn)鏈表操作:
1. 常用的方法:左右交替,下一個(gè)結(jié)點(diǎn)保存,上一個(gè)結(jié)點(diǎn)替換該結(jié)點(diǎn)的下個(gè)結(jié)點(diǎn)。實(shí)現(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. 使用遞歸方法。三個(gè)結(jié)點(diǎn),頭結(jié)點(diǎn),首節(jié)點(diǎn),第二個(gè)結(jié)點(diǎn)。把首節(jié)點(diǎn)后面的所有結(jié)點(diǎn)當(dāng)成第二個(gè)結(jié)點(diǎn),依次循環(huán)下去,由于要滿足 $pHead != null || $pHead->next != null ;所以不會(huì)出現(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;
}
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計(jì)算法總結(jié)》、《php字符串(string)用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP常用遍歷算法與技巧總結(jié)》及《PHP數(shù)學(xué)運(yùn)算技巧總結(jié)》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- php數(shù)組和鏈表的區(qū)別總結(jié)
- PHP雙向鏈表定義與用法示例
- php數(shù)據(jù)結(jié)構(gòu)之順序鏈表與鏈?zhǔn)骄€性表示例
- PHP實(shí)現(xiàn)合并兩個(gè)排序鏈表的方法
- php數(shù)組指針操作詳解
- php each 返回?cái)?shù)組中當(dāng)前的鍵值對(duì)并將數(shù)組指針向前移動(dòng)一步實(shí)例
- PHP7生產(chǎn)環(huán)境隊(duì)列Beanstalkd用法詳解
- php使用redis的有序集合zset實(shí)現(xiàn)延遲隊(duì)列應(yīng)用示例
- php+redis實(shí)現(xiàn)消息隊(duì)列功能示例
- PHP如何通過(guò)帶尾指針的鏈表實(shí)現(xiàn)''隊(duì)列''
相關(guān)文章
php+mysql+jquery實(shí)現(xiàn)簡(jiǎn)易的檢索自動(dòng)補(bǔ)全提示功能
這篇文章主要介紹了php+mysql+jquery實(shí)現(xiàn)簡(jiǎn)易的檢索自動(dòng)補(bǔ)全提示功能,涉及基于jQuery的ajax數(shù)據(jù)庫(kù)查詢與事件動(dòng)態(tài)響應(yīng)相關(guān)操作技巧,需要的朋友可以參考下2017-04-04
學(xué)習(xí)php設(shè)計(jì)模式 php實(shí)現(xiàn)抽象工廠模式
這篇文章主要介紹了php設(shè)計(jì)模式中的抽象工廠模式,使用php實(shí)現(xiàn)抽象工廠模式,感興趣的小伙伴們可以參考一下2015-12-12
php性能優(yōu)化進(jìn)階不要在for循環(huán)中操作DB
這篇文章主要為大家介紹了php性能優(yōu)化進(jìn)階不要在for循環(huán)中操作DB,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
PHP英文字母大小寫(xiě)轉(zhuǎn)換函數(shù)小結(jié)
這篇文章主要介紹了幾個(gè)PHP英文字母大小寫(xiě)轉(zhuǎn)換函數(shù),分為首字母大小寫(xiě)轉(zhuǎn)換和所有字母大小寫(xiě)轉(zhuǎn)換,需要的朋友可以參考下2014-05-05
CURL的學(xué)習(xí)和應(yīng)用(附多線程實(shí)現(xiàn))
這篇文章主要介紹了CURL的安裝與多線程實(shí)現(xiàn)方法,需要的朋友可以參考下2013-06-06

