SPL提供了雙向連結串列、堆疊、佇列、堆、降序堆、升序堆、優先順序佇列、定長陣列、物件容器
進出異端,先進先出
<?php$obj = new SplQueue();//插入一個節點到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 ( ) ) *///向堆疊放入一個節點到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節點靠近bottom位置的相鄰節點$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節點,而雙向連結串列的rewind指向bottom節點$obj->rewind();//堆疊的next是使指標向bottom方向相鄰的節點移動一位,而連結串列是向top方向移動$obj->next();//遍歷堆疊$obj->rewind();while($obj->valid()){ echo $obj->key().'=>'.$obj->current(); $obj->next(); }/** 2=>'C' 1=>'B' 0=>1 *///從堆疊中取出top位置的節點並刪除$obj->pop();複製程式碼
$obj = new SplDoublyLinkedList();//SplDoublyLinkedList Object ( [flags:SplDoublyLinkedList:private] => 0 [dllist:SplDoublyLinkedList:private] => Array ( ) )//把新的節點資料新增到連結串列的頂部Top$obj->push(1); $obj->push(2); $obj->push(3);//把新的節點資料新增到連結串列底部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節點$obj->rewind();//獲取當前指標指向的節點$obj->current();//輸出: 4//將執行挪到下一個$obj->next(); $obj->current();//輸出: 1$obj->prev(); $obj->current();//輸出: 4//越界了$obj->next(); $obj->next(); $obj->next(); $obj->next(); $obj->current();//輸出: null//判斷當前節點是否有效$obj->valid()//輸出: false$obj->prev(); $obj->valid()//輸出: true$obj->shift();//把top節點從連結串列中刪除並返回$obj->pop();//輸出 3//把bottom節點從連結串列中刪除並返回$obj->shift();//輸出 4//返回top節點$obj->top();//返回bottom節點$obj->bottom();複製程式碼
感謝您的閱讀,如果對您有幫助,歡迎關注"CRMEB"。碼雲上有我們開源的商城專案,知識付費專案,均是基於PHP+vue開發,學習研究歡迎使用,關注我們保持聯絡!
想了解更多程式設計學習,敬請關注欄目!
以上就是解析PHP標準庫SPL資料結構的詳細內容,更多請關注TW511.COM其它相關文章!