您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關PHP標準庫SPL數(shù)據(jù)結構是什么的內容。小編覺得挺實用的,因此分享給大家做個參考。一起跟隨小編過來看看吧。
SPL提供了雙向鏈表、堆棧、隊列、堆、降序堆、升序堆、優(yōu)先級隊列、定長數(shù)組、對象容器
進出異端,先進先出
<?php$obj = new SplQueue();//插入一個節(jié)點到top位置$obj->enqueue(1); $obj->enqueue(2); $obj->enqueue(3);/** SplQueue Object ( [flags:SplDoublyLinkedList:private] => 4 [dllist:SplDoublyLinkedList:private] => Array ( [0] => 1 [1] => 2 [2] => 3 ) ) */$obj->offsetSet(0,'C');/** SplQueue Object ( [flags:SplDoublyLinkedList:private] => 4 [dllist:SplDoublyLinkedList:private] => Array ( [0] => 'C' [1] => 2 [2] => 3 ) ) */$obj->rewind();//從隊列中返回并刪除bottom位置的元素$obj->dequeue();/** 'C' SplQueue Object ( [flags:SplDoublyLinkedList:private] => 4 [dllist:SplDoublyLinkedList:private] => Array ( [0] => 2 [1] => 3 ) ) */復制代碼
單端進出時,后進先出,先進后出
$obj = new SplStack();/** SplStack Object ( [flags:SplDoublyLinkedList:private] => 6 [dllist:SplDoublyLinkedList:private] => Array ( ) ) *///向堆棧放入一個節(jié)點到top位置$obj->push(1); $obj->push(2); $obj->push(3);/** SplStack Object ( [flags:SplDoublyLinkedList:private] => 6 [dllist:SplDoublyLinkedList:private] => Array ( [0] => 1 [1] => 2 [2] => 3 ) ) *///堆棧的offset = 0 是Top所在的位置,offset = 1 是top節(jié)點靠近bottom位置的相鄰節(jié)點$obj->offsetSet(0,'C');/** SplStack Object ( [flags:SplDoublyLinkedList:private] => 6 [dllist:SplDoublyLinkedList:private] => Array ( [0] => 1 [1] => 2 [2] => 'C' ) ) */$obj->offsetSet(1,'B');/** SplStack Object ( [flags:SplDoublyLinkedList:private] => 6 [dllist:SplDoublyLinkedList:private] => Array ( [0] => 1 [1] => 'B' [2] => 'C' ) ) *///堆棧的rewind使指針指向top節(jié)點,而雙向鏈表的rewind指向bottom節(jié)點$obj->rewind();//堆棧的next是使指針向bottom方向相鄰的節(jié)點移動一位,而鏈表是向top方向移動$obj->next();//遍歷堆棧$obj->rewind();while($obj->valid()){ echo $obj->key().'=>'.$obj->current(); $obj->next(); }/** 2=>'C' 1=>'B' 0=>1 *///從堆棧中取出top位置的節(jié)點并刪除$obj->pop();復制代碼
$obj = new SplDoublyLinkedList();//SplDoublyLinkedList Object ( [flags:SplDoublyLinkedList:private] => 0 [dllist:SplDoublyLinkedList:private] => Array ( ) )//把新的節(jié)點數(shù)據(jù)添加到鏈表的頂部Top$obj->push(1); $obj->push(2); $obj->push(3);//把新的節(jié)點數(shù)據(jù)添加到鏈表底部bottom$obj->unshift(4);/** SplDoublyLinkedList Object ( [flags:SplDoublyLinkedList:private] => 0 [dllist:SplDoublyLinkedList:private] => Array ( [0] => 4 [1] => 1 [2] => 2 [3] => 3 ) ) *///在沒有調用rewind()方法之前輸出為nullecho 'current'.$obj->current();//輸出: null//把指針指向bottom節(jié)點$obj->rewind();//獲取當前指針指向的節(jié)點$obj->current();//輸出: 4//將執(zhí)行挪到下一個$obj->next(); $obj->current();//輸出: 1$obj->prev(); $obj->current();//輸出: 4//越界了$obj->next(); $obj->next(); $obj->next(); $obj->next(); $obj->current();//輸出: null//判斷當前節(jié)點是否有效$obj->valid()//輸出: false$obj->prev(); $obj->valid()//輸出: true$obj->shift();//把top節(jié)點從鏈表中刪除并返回$obj->pop();//輸出 3//把bottom節(jié)點從鏈表中刪除并返回$obj->shift();//輸出 4//返回top節(jié)點$obj->top();//返回bottom節(jié)點$obj->bottom();復制代碼
感謝各位的閱讀!關于PHP標準庫SPL數(shù)據(jù)結構是什么就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。