php鏈表用法實例分析
更新時間:2015年07月09日 11:15:40 作者:silencer
這篇文章主要介紹了php鏈表用法,實例分析了php創(chuàng)建鏈表及針對鏈表節(jié)點(diǎn)的增加、刪除、更新與遍歷等常用操作,需要的朋友可以參考下
本文實例講述了php鏈表用法。分享給大家供大家參考。具體如下:
這里簡單介紹了php鏈表的基本用法,包括鏈表節(jié)點(diǎn)的創(chuàng)建、遍歷、更新等操作。
<?php
/**
* @author MzXy
* @copyright 2011
* @param PHP鏈表
*/
/**
*
*節(jié)點(diǎn)類
*/
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;//長度
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--要訪問的節(jié)點(diǎn)的數(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é)點(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);
}
}
?>
希望本文所述對大家的php程序設(shè)計有所幫助。
您可能感興趣的文章:
- PHP實現(xiàn)找出鏈表中環(huán)的入口節(jié)點(diǎn)
- PHP實現(xiàn)雙鏈表刪除與插入節(jié)點(diǎn)的方法示例
- php實現(xiàn)單鏈表的實例代碼
- PHP 雙鏈表(SplDoublyLinkedList)簡介和使用實例
- PHP小教程之實現(xiàn)雙向鏈表
- PHP中模擬鏈表和鏈表的基本操作示例
- PHP實現(xiàn)的基于單向鏈表解決約瑟夫環(huán)問題示例
- PHP實現(xiàn)單鏈表翻轉(zhuǎn)操作示例
- php 數(shù)據(jù)結(jié)構(gòu)之鏈表隊列
- PHP基于雙向鏈表與排序操作實現(xiàn)的會員排名功能示例
- PHP獲取鏈表中倒數(shù)第K個節(jié)點(diǎn)的方法
相關(guān)文章
PHP session_start()問題解疑(詳細(xì)介紹)
對于PHP的session功能,始終找不到合適的答案,尤其是一些錯誤,還有一些沒有錯誤的結(jié)果,最可怕的就是后者,一直為許多的初學(xué)者為難。就連有些老手,有時都被搞得莫名其妙2013-07-07
ThinkPHP連接ORACLE數(shù)據(jù)庫的詳細(xì)教程
ThinkPHP要連接Oracle數(shù)據(jù)庫,必須有兩個東西,一個PHP官方寫的擴(kuò)展,一個Oracle官方寫的客戶端,本文小編給大家詳細(xì)介紹了ThinkPHP連接ORACLE數(shù)據(jù)庫的教程,文中通過圖文結(jié)合的方式講解的非常詳細(xì),需要的朋友可以參考下2023-12-12
PHP定時自動生成靜態(tài)HTML的實現(xiàn)代碼
為了提高網(wǎng)站的訪問速度,我們往往采用生成靜態(tài)的方式來實現(xiàn),這樣確實把網(wǎng)站的訪問速度提高了非常多.2010-06-06
CURL的學(xué)習(xí)和應(yīng)用(附多線程實現(xiàn))
這篇文章主要介紹了CURL的安裝與多線程實現(xiàn)方法,需要的朋友可以參考下2013-06-06
PHP遞歸寫入MySQL實現(xiàn)無限級分類數(shù)據(jù)操作示例
這篇文章主要介紹了PHP遞歸寫入MySQL實現(xiàn)無限級分類數(shù)據(jù)操作,涉及mysql數(shù)據(jù)庫的創(chuàng)建以及php遞歸寫入、讀取數(shù)據(jù)庫分類相關(guān)操作技巧,需要的朋友可以參考下2018-07-07

