一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - PHP教程 - PHP實現(xiàn)鏈表的定義與反轉(zhuǎn)功能示例

PHP實現(xiàn)鏈表的定義與反轉(zhuǎn)功能示例

2019-09-28 12:33徐彬 PHP教程

這篇文章主要介紹了PHP實現(xiàn)鏈表的定義與反轉(zhuǎn)功能,結(jié)合實例形式分析了PHP鏈表的基本定義、添加、移除、遍歷以及兩種反轉(zhuǎn)操作相關(guān)實現(xiàn)技巧,需要的朋友可以參考下

本文實例講述了PHP實現(xiàn)鏈表的定義與反轉(zhuǎ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>';
?>

反轉(zhuǎn)鏈表操作:

1. 常用的方法:左右交替,下一個結(jié)點保存,上一個結(jié)點替換該結(jié)點的下個結(jié)點。實現(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é)點,首節(jié)點,第二個結(jié)點。把首節(jié)點后面的所有結(jié)點當(dāng)成第二個結(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程序設(shè)計有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 晓雪老师我要进你里面好爽 | 国产资源免费观看 | 亚洲国产成人久久77 | 福利视频一区二区牛牛 | luanlun绝对真实乱 | 亚洲精品成人a | 久久AV国产麻豆HD真实 | 四虎影视4hutv最新地址在线 | 无码区国产区在线播放 | 国产综合图区 | 催奶师小说 | 91短视频在线观看2019 | 国产激情一区二区三区四区 | 色先锋影音资源 | 韩国三级在线播放 | 成年人在线免费看 | 成 人 亚洲 综合天堂 | 国产麻豆在线观看网站 | 3黑人巨大vs北岛玲 3d肉浦团在线观看 3d动漫免费 | 2022色婷婷综合久久久 | 青青草亚洲 | 色婷婷精品 | 国内精品久久久久久中文字幕 | 五月性| 欧美成人免费tv在线播放 | 国产精品毛片va一区二区三区 | 亚洲 日韩 国产 中文视频 | 日韩精品免费一区二区三区 | 亚洲第一区在线观看 | 久草大| 999热这里全都是精品 | 亚洲国产精品久久无套麻豆 | 日本动漫啪啪动画片mv | 99久久精品国内 | 日本草草视频 | 大象传媒短视频网站 | 99国产牛牛视频在线网站 | 草莓茄子丝瓜番茄小蝌蚪 | 日本老妇成熟 | 白丝超短裙被输出娇喘不停小说 | 5x社区发源地最新地址 |