php雙向鏈表+性能,PHP雙向鏈表定義與用法示例

本文實例講述了PHP雙向鏈表定義與用法。分享給大家供大家參考,具體如下:

由于需要對一組數據多次進行移動操作,所以寫個雙向鏈表。但對php實在不熟悉,雖然測試各個方法沒啥問題,就是不知道php語言深層的這些指針和unset有什么注意的地方,貼出來讓大家教育吧。效率沒測試....求諒解~

/**

* **雙向鏈表

* @author zhiyuan12@

*/

/**

* 鏈表元素結點類

*/

class Node_Element {

public $pre = NULL; // 前驅

public $next = NULL; // 后繼

public $key = NULL; // 元素鍵值

public $data = NULL; // 結點值

function __Construct($key, $data) {

$this->key = $key;

$this->data = $data;

}

}

/**

* 雙向鏈表類

*/

class DoubleLinkedList {

private $head; // 頭指針

private $tail; // 尾指針

private $current; // 當前指針

private $len; // 鏈表長度

function __Construct() {

$this->head = self::_getNode ( null, null );

$this->curelement = $this->head;

$this->tail = $this->head;

$len = 0;

}

/**

* @ desc: 讀取鏈表全部結點

*/

public function readAll() {

$tmp = $this->head;

while ( $tmp->next !== null ) {

$tmp = $tmp->next;

var_dump ( $tmp->key, $tmp->data );

}

}

public function move($pos1, $pos2) {

$pos1Node = $this->findPosition ( $pos1 );

$pos2Node = $this->findPosition ( $pos2 );

if ($pos1Node !== null && $pos2Node !== null) {

$tmpKey = $pos1Node->key;

$tmpData = $pos1Node->data;

$pos1Node->key = $pos2Node->key;

$pos1Node->data = $pos2Node->data;

$pos2Node->key = $tmpKey;

$pos2Node->data = $tmpData;

return true;

}

return false;

}

/**

* @ desc: 在指定關鍵詞刪除結點

*

* @param : $key

* 指定位置的鏈表元素key

*/

public function delete($key) {

$pos = $this->find ( $key );

if ($pos !== null) {

$tmp = $pos;

$last = null;

$first = true;

while ( $tmp->next !== null && $tmp->next->key === $key ) {

$tmp = $tmp->next;

if (! $first) {

$this->delNode ( $last );

} else {

$first = false;

}

$last = $tmp;

}

if ($tmp->next !== null) {

$pos->pre->next = $tmp->next;

$tmp->next->pre = $pos->pre;

} else {

$pos->pre->next = null;

}

$this->delNode ( $pos );

$this->delNode ( $tmp );

}

}

/**

* @ desc: 在指定位置刪除結點

*

* @param : $key

* 指定位置的鏈表元素key

*/

public function deletePosition($pos) {

$tmp = $this->findPosition ( $pos );

if ($tmp === null) {

return true;

}

if ($tmp === $this->getTail ()) {

$tmp->pre->next = null;

$this->delNode ( $tmp );

return true;

}

$tmp->pre->next = $tmp->next;

$tmp->next->pre = $tmp->pre;

$this->delNode ( $tmp );

}

/**

* @ desc: 在指定鍵值之前插入結點

*

* @param : $key

* //指定位置的鏈表元素key

* @param : $data

* //要插入的鏈表元素數據

* @param : $flag

* //是否順序查找位置進行插入

*/

public function insert($key, $data, $flag = true) {

$newNode = self::_getNode ( $key, $data );

$tmp = $this->find ( $key, $flag );

if ($tmp !== null) {

$newNode->pre = $tmp->pre;

$newNode->next = $tmp;

$tmp->pre = $newNode;

$newNode->pre->next = $newNode;

} else {

$newNode->pre = $this->tail;

$this->tail->next = $newNode;

$this->tail = $newNode;

}

$this->len ++;

}

/**

* @ desc: 在指定位置之前插入結點

*

* @param : $pos

* 指定插入鏈表的位置

* @param : $key

* 指定位置的鏈表元素key

* @param : $data

* 要插入的鏈表元素數據

*/

public function insertPosition($pos, $key, $data) {

$newNode = self::_getNode ( $key, $data );

$tmp = $this->findPosition ( $pos );

if ($tmp !== null) {

$newNode->pre = $tmp->pre;

$newNode->next = $tmp;

$tmp->pre = $newNode;

$newNode->pre->next = $newNode;

} else {

$newNode->pre = $this->tail;

$this->tail->next = $newNode;

$this->tail = $newNode;

}

$this->len ++;

return true;

}

/**

* @ desc: 根據key值查詢指定位置數據

*

* @param : $key

* //指定位置的鏈表元素key

* @param : $flag

* //是否順序查找

*/

public function find($key, $flag = true) {

if ($flag) {

$tmp = $this->head;

while ( $tmp->next !== null ) {

$tmp = $tmp->next;

if ($tmp->key === $key) {

return $tmp;

}

}

} else {

$tmp = $this->getTail ();

while ( $tmp->pre !== null ) {

if ($tmp->key === $key) {

return $tmp;

}

$tmp = $tmp->pre;

}

}

return null;

}

/**

* @ desc: 根據位置查詢指定位置數據

*

* @param : $pos

* //指定位置的鏈表元素key

*/

public function findPosition($pos) {

if ($pos <= 0 || $pos > $this->len)

return null;

if ($pos < ($this->len / 2 + 1)) {

$tmp = $this->head;

$count = 0;

while ( $tmp->next !== null ) {

$tmp = $tmp->next;

$count ++;

if ($count === $pos) {

return $tmp;

}

}

} else {

$tmp = $this->tail;

$pos = $this->len - $pos + 1;

$count = 1;

while ( $tmp->pre !== null ) {

if ($count === $pos) {

return $tmp;

}

$tmp = $tmp->pre;

$count ++;

}

}

return null;

}

/**

* @ desc: 返回鏈表頭節點

*/

public function getHead() {

return $this->head->next;

}

/**

* @ desc: 返回鏈表尾節點

*/

public function getTail() {

return $this->tail;

}

/**

* @ desc: 查詢鏈表節點個數

*/

public function getLength() {

return $this->len;

}

private static function _getNode($key, $data) {

$newNode = new Node_Element ( $key, $data );

if ($newNode === null) {

echo "new node fail!";

}

return $newNode;

}

private function delNode($node) {

unset ( $node );

$this->len --;

}

}

$myList = new DoubleLinkedList ();

$myList->insert ( 1, "test1" );

$myList->insert ( 2, "test2" );

$myList->insert ( "2b", "test2-b" );

$myList->insert ( 2, "test2-c" );

$myList->insert ( 3, "test3" );

$myList->insertPosition ( 5, "t", "testt" );

$myList->readAll ();

echo "+++";

$myList->deletePosition(0);

$myList->readAll ();

echo "..." . $myList->getLength ();

var_dump ( $myList->findPosition ( 3 )->data );

?>

運行結果:

int(1)

string(5) "test1"

int(2)

string(7) "test2-c"

int(2)

string(5) "test2"

string(2) "2b"

string(7) "test2-b"

string(1) "t"

string(5) "testt"

int(3)

string(5) "test3"

+++int(1)

string(5) "test1"

int(2)

string(7) "test2-c"

int(2)

string(5) "test2"

string(2) "2b"

string(7) "test2-b"

string(1) "t"

string(5) "testt"

int(3)

string(5) "test3"

...6string(5) "test2"

希望本文所述對大家PHP程序設計有所幫助。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/539556.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/539556.shtml
英文地址,請注明出處:http://en.pswp.cn/news/539556.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

反擊爬蟲,前端工程師的腦洞可以有多大?

對于一張網頁&#xff0c;我們往往希望它是結構良好&#xff0c;內容清晰的&#xff0c;這樣搜索引擎才能準確地認知它。 而反過來&#xff0c;又有一些情景&#xff0c;我們不希望內容能被輕易獲取&#xff0c; 前言 比方說電商網站的交易額&#xff0c;教育網站的題目等。因為…

Spring與Struts框架整合

Spring&#xff0c;負責對象對象創建 Struts&#xff0c;用Action處理請求 Spring與Struts框架整合&#xff0c;關鍵點&#xff1a;讓struts框架action對象的創建&#xff0c;交給spring完成&#xff01; 1.步驟&#xff1a; 引入jar文件 1&#xff09;引入struts .jar相關文件…

esxi能直通的顯卡型號_顯卡刷bios教程

一般來說顯卡默認的出廠bios就已經很穩定&#xff0c;如果沒有特殊情況下建議不要刷顯卡bios。一般而言部分網友刷顯卡BIOS目的是開核或超頻&#xff0c;那么對于一個不會刷顯卡bios的網友來說肯定會問顯卡怎么刷bios類似的問題&#xff0c;那么本文這里就說一下有關顯卡怎么刷…

關于Linux網卡調優之:RPS (Receive Packet Steering)

昨天在查LVS調度均衡性問題時&#xff0c;最終確定是 persistence_timeout 參數會使用IP哈希。目的是為了保證長連接&#xff0c;即一定時間內訪問到的是同一臺機器。而我們內部系統&#xff0c;由于出口IP相對單一&#xff0c;所以總會被哈希到相同的RealServer。 過去使用LVS…

footer.php置底,CSS五種方式實現Footer置底

頁腳置底(Sticky footer)就是讓網頁的footer部分始終在瀏覽器窗口的底部。當網頁內容足夠長以至超出瀏覽器可視高度時&#xff0c;頁腳會隨著內容被推到網頁底部&#xff1b;但如果網頁內容不夠長&#xff0c;置底的頁腳就會保持在瀏覽器窗口底部。方法一&#xff1a;將內容部分…

安卓adapter適配器作用_自帶安卓系統的便攜屏,能玩出什么花樣?

之前說到去年出差太多&#xff0c;平常就把便攜屏帶上了。之前也說了如果是像筆者這樣的出差狗也知道&#xff0c;托運需要提前去機場一路著急忙慌&#xff0c;不托運只需要打印登機牌(紙質才給報銷)排隊安檢登機就完了。有的時候可以把標準顯示器來回寄&#xff0c;只要包裝強…

Gradle插件學習筆記(二)

之前介紹了Gradle插件的開發&#xff0c;這次會對功能進行一部分拓展&#xff0c;建議沒有讀過第一篇文章的朋友&#xff0c;先看一下Gradle插件學習筆記&#xff08;一&#xff09; Extension 之前的文章提到過&#xff0c;如何編寫一個插件&#xff0c;但是并不能通過外面傳遞…

php抽象類繼承抽象類,PHP面向對象程序設計高級特性詳解(接口,繼承,抽象類,析構,克隆等)...

本文實例講述了PHP面向對象程序設計高級特性。分享給大家供大家參考&#xff0c;具體如下&#xff1a;靜態屬性class StaticExample {static public $aNum 0; // 靜態共有屬性static public function sayHello() { // 靜態共有方法print "hello";}}print StaticExam…

Typora markdown公式換行等號對齊_Typora編寫博客格式化文檔的最佳軟件

Typora-編寫博客格式化文檔的最佳軟件Typora 不僅是一款支持實時預覽的 Markdown 文本編輯器&#xff0c;而且還支持數學公式、代碼塊、思維導圖等功能。它有 OS X、Windows、Linux 三個平臺的版本&#xff0c;是完全免費的。作為技術人員或者專業人員&#xff0c;使用Markdown…

Bootstrap靜態cdn

百度的靜態資源庫的 CDN 服務http://cdn.code.baidu.com/ &#xff0c;訪問速度更快、加速效果更明顯、沒有速度和帶寬限制、永久免費,引入代碼如下&#xff1a; <!-- 新 Bootstrap 核心 CSS 文件 --> <link href"http://apps.bdimg.com/libs/bootstrap/3.3.0/…

php復習,PHP排序算法的復習和總結

直接上代碼吧&#xff01;/** 插入排序(一維數組)* 每次將一個待排序的數據元素&#xff0c;插入到前面已經排好序的數列中的適當的位置&#xff0c;使數列依然有序&#xff1b;直到待排序的數據元素全部插入完成為止。*/function insertSort($arr){if(!is_array($arr) || coun…

docker-machine

vbox安裝 sudo /sbin/vboxconfig &#xfffc; yum install gcc make yum install kernel-devel-3.10.0-514.26.2.el7.x86_64 轉載于:https://www.cnblogs.com/yixiaoyi/p/dockermachine.html

intention lock_寫作技巧:你寫出來的情節有用嗎?好情節的原則——LOCK系統

讀者喜歡一本小說的原因只有一個&#xff1a;很棒的故事。——Donald Maass來&#xff0c;話筒對準這位小作家&#xff0c;請問你是如何構思故事的&#xff1f;是習慣于現在腦海中把故事都想好了&#xff0c;才開始寫作&#xff1f;還是習慣于臨場發揮&#xff0c;喜歡一屁股坐…

zookeeper基本操作

1.客戶端連接 [txtest1 bin]$ jps 23433 Jps 23370 QuorumPeerMain #zookeeper進程[txtest1 bin]$ ./zkCli.sh -server test1:2182 Connecting to test1:2182 2018-01-24 23:42:09,024 [myid:] - INFO [main:Environment100] - Client environment:zookeeper.version3.4.5-…

sqllite java 密碼,SQLite登錄檢查用戶名和密碼

我正在創建一個應用程序(使用Java和SQLite)(JFrame&#xff0c;使用Netbeans)我有我想要登錄的用戶 . (我有所有正確的包JDBC&#xff0c;SQLite等)我遇到的問題似乎是獲取用戶名/密碼來檢查我的users.db文件..我正在使用Java和SQLite . 我也在使用JDBC .我的一些代碼作為一個例…

springmvc與struts2的區別

1&#xff09;springmvc的入口是一個servlet&#xff0c;即前端控制器&#xff0c;例如&#xff1a;*.action struts2入口是一個filter過慮器&#xff0c;即前端過濾器&#xff0c;例如&#xff1a;/* 2&#xff09;springmvc是基于方法開發&#xff0c;傳遞參數是通過方法形…

power designer數據流圖_鯤云公開課 | 三分鐘帶你了解數據流架構

目前&#xff0c;市場上的芯片主要包括指令集架構和數據流架構兩種實現方式。指令集架構主要包括X86架構、ARM架構、精簡指令集運算RISC-V開源架構&#xff0c;以及SIMD架構。總體來說&#xff0c;四者都屬于傳統的通用指令集架構。傳統的指令集架構采用馮諾依曼計算方式&#…

onCreate源碼分析

原文地址Android面試題-onCreate源碼都沒看過&#xff0c;怎好意思說自己做android Activity扮演了一個界面展示的角色&#xff0c;堪稱四大組件之首&#xff0c;onCreate是Activity的執行入口&#xff0c;都不知道入口到底干了嘛&#xff0c;還學什么android,所以本文會從源碼…

linux php環境搭建教程,linux php環境搭建教程

linux php環境搭建的方法&#xff1a;首先獲取相關安裝包&#xff1b;然后安裝Apache以及mysql&#xff1b;接著修改配置文件“httpd.conf”&#xff1b;最后設置環境變量和開機自啟&#xff0c;并編譯安裝PHP即可。一、獲取安裝包PHP下載地址&#xff1a;http://cn.php.net/di…

Apache2.2與Tomcat7集成方案詳解

原文地址&#xff1a;http://my.oschina.net/u/919173/blog/159206 ------------------------------------ 首先談一下為什么要集成Apache和tomcat7&#xff1f; Apache是當前使用最為廣泛的WWW服務器軟件&#xff0c;具有相當強大的靜態HTML處理的能力。 Tomcat服務器是一個…