php鏈表用法實(shí)例分析
本文實(shí)例講述了php鏈表用法。分享給大家供大家參考。具體如下:
這里簡(jiǎn)單介紹了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;//長(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);
}
}
?>
希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。
- PHP實(shí)現(xiàn)找出鏈表中環(huán)的入口節(jié)點(diǎn)
- PHP實(shí)現(xiàn)雙鏈表刪除與插入節(jié)點(diǎn)的方法示例
- php實(shí)現(xiàn)單鏈表的實(shí)例代碼
- PHP 雙鏈表(SplDoublyLinkedList)簡(jiǎn)介和使用實(shí)例
- PHP小教程之實(shí)現(xiàn)雙向鏈表
- PHP中模擬鏈表和鏈表的基本操作示例
- PHP實(shí)現(xiàn)的基于單向鏈表解決約瑟夫環(huán)問(wèn)題示例
- PHP實(shí)現(xiàn)單鏈表翻轉(zhuǎn)操作示例
- php 數(shù)據(jù)結(jié)構(gòu)之鏈表隊(duì)列
- PHP基于雙向鏈表與排序操作實(shí)現(xiàn)的會(huì)員排名功能示例
- PHP獲取鏈表中倒數(shù)第K個(gè)節(jié)點(diǎn)的方法
相關(guān)文章
PHP session_start()問(wèn)題解疑(詳細(xì)介紹)
對(duì)于PHP的session功能,始終找不到合適的答案,尤其是一些錯(cuò)誤,還有一些沒(méi)有錯(cuò)誤的結(jié)果,最可怕的就是后者,一直為許多的初學(xué)者為難。就連有些老手,有時(shí)都被搞得莫名其妙2013-07-07
ThinkPHP連接ORACLE數(shù)據(jù)庫(kù)的詳細(xì)教程
ThinkPHP要連接Oracle數(shù)據(jù)庫(kù),必須有兩個(gè)東西,一個(gè)PHP官方寫(xiě)的擴(kuò)展,一個(gè)Oracle官方寫(xiě)的客戶端,本文小編給大家詳細(xì)介紹了ThinkPHP連接ORACLE數(shù)據(jù)庫(kù)的教程,文中通過(guò)圖文結(jié)合的方式講解的非常詳細(xì),需要的朋友可以參考下2023-12-12
PHP定時(shí)自動(dòng)生成靜態(tài)HTML的實(shí)現(xiàn)代碼
為了提高網(wǎng)站的訪問(wèn)速度,我們往往采用生成靜態(tài)的方式來(lái)實(shí)現(xiàn),這樣確實(shí)把網(wǎng)站的訪問(wèn)速度提高了非常多.2010-06-06
CURL的學(xué)習(xí)和應(yīng)用(附多線程實(shí)現(xiàn))
這篇文章主要介紹了CURL的安裝與多線程實(shí)現(xiàn)方法,需要的朋友可以參考下2013-06-06
php token使用與驗(yàn)證示例【測(cè)試可用】
這篇文章主要介紹了php token使用與驗(yàn)證方法,通過(guò)對(duì)form表單hidden提交字段的處理實(shí)現(xiàn)token驗(yàn)證功能,防止非法來(lái)源數(shù)據(jù)的訪問(wèn),需要的朋友可以參考下2017-08-08
PHP7基于curl實(shí)現(xiàn)的上傳圖片功能
這篇文章主要介紹了PHP7基于curl實(shí)現(xiàn)的上傳圖片功能,結(jié)合實(shí)例形式對(duì)比分析了php5.5之前與php7版本的curl圖片上傳功能相關(guān)實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下2018-05-05
PHP遞歸寫(xiě)入MySQL實(shí)現(xiàn)無(wú)限級(jí)分類數(shù)據(jù)操作示例
這篇文章主要介紹了PHP遞歸寫(xiě)入MySQL實(shí)現(xiàn)無(wú)限級(jí)分類數(shù)據(jù)操作,涉及mysql數(shù)據(jù)庫(kù)的創(chuàng)建以及php遞歸寫(xiě)入、讀取數(shù)據(jù)庫(kù)分類相關(guān)操作技巧,需要的朋友可以參考下2018-07-07

