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 header 詳細(xì)使用說(shuō)明與使用心得
下面是關(guān)于header函數(shù)的詳細(xì)使用說(shuō)明2009-11-11
How do I change MySQL timezone?
The MySQL timezone is set to MST (-7 hours GMT/UTC) and is not configurable by you. MySQL is only capable of having 1 timezone setting per mysql daemon. Therefore, you cannot select NOW() and expect a result in a timezone other than MST.2008-03-03
PHP實(shí)現(xiàn)基本留言板功能原理與步驟詳解
這篇文章主要介紹了PHP實(shí)現(xiàn)基本留言板功能,結(jié)合實(shí)例形式分析了PHP實(shí)現(xiàn)基本留言板功能的相關(guān)原理、數(shù)據(jù)庫(kù)構(gòu)建、功能實(shí)現(xiàn)等步驟與相關(guān)操作技巧,需要的朋友可以參考下2020-03-03
php的$_FILES的臨時(shí)儲(chǔ)存文件與回收機(jī)制實(shí)測(cè)過(guò)程
上傳文件是怎么個(gè)原理,大概的想了下,應(yīng)該是一種回收機(jī)制:點(diǎn)擊了臨時(shí)文件空間,那么,php自身應(yīng)該自己維護(hù)這塊空間的回收,具體的測(cè)試過(guò)程如下,感興趣的朋友可以參考下哈2013-07-07
PHP curl 獲取響應(yīng)的狀態(tài)碼的方法
PHP curl可以從服務(wù)器端模擬一個(gè)http請(qǐng)求,例如抓取網(wǎng)頁(yè)、模擬登陸等,想要獲取狀態(tài)碼,需要在執(zhí)行curl_exec后再通過(guò)curl_getinfo來(lái)獲取2014-01-01
wamp服務(wù)器訪問(wèn)php非常緩慢的解決過(guò)程
這篇文章主要介紹了wamp服務(wù)器訪問(wèn)php非常緩慢的解決過(guò)程,十分的簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下。2015-07-07

