SPL,PHP 标准库(Standard PHP Library) ,从 PHP 5.0 起内置的组件和接口,且从 PHP5.3 已逐渐的成熟。SPL 在所有的 PHP5 开发环境中被内置,同时无需任何设置。
双向链表原理图
SplDoublyLinkedList
双向链表是一种重要的线性存储结构,对于双向链表中的每个节点,不仅仅存储自己的信息,还要保存前驱和后继节点的地址。
PHP SPL中的SplDoublyLinkedList类提供了对双向链表的操作。
参考资料
PHP-SPL-SplDoublyLinkedList
SplDoublyLinkedList类说明
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| SplDoublyLinkedList implements Iterator , ArrayAccess , Countable { public __construct ( void ) public void add ( mixed $index , mixed $newval ) public mixed top ( void ) public mixed bottom ( void ) public int count ( void ) public bool isEmpty ( void ) public mixed key ( void ) public void prev ( void ) public void next ( void ) public mixed current ( void ) public void rewind ( void ) public bool valid ( void ) public bool offsetExists ( mixed $index ) public mixed offsetGet ( mixed $index ) public void offsetSet ( mixed $index , mixed $newval ) public void offsetUnset ( mixed $index ) public mixed pop ( void ) public void push ( mixed $value ) public string serialize ( void ) public void unserialize ( string $serialized ) public void setIteratorMode ( int $mode ) public int getIteratorMode ( void ) public mixed shift ( void ) public void unshift ( mixed $value ) }
|
SplDoublyLinkedList使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| $list = new SplDoublyLinkedList(); $list->push('a'); $list->push('b'); $list->push('c'); $list->unshift('top'); $list->shift(); print_r(array( 'pop' => $list->pop(), 'count' => $list->count(), 'isEmpty' => $list->isEmpty(), 'bottom' => $list->bottom(), 'top' => $list->top() )); $list->setIteratorMode(SplDoublyLinkedList::IT_MODE_FIFO); print_r($list->getIteratorMode()); for($list->rewind(); $list->valid(); $list->next()) { echo $list->current().PHP_EOL; } print_r($a = $list->serialize());
$list->offsetSet(0,'new one'); $list->offsetUnset(0); print_r(array( 'offsetExists' => $list->offsetExists(4), 'offsetGet' => $list->offsetGet(0), )); print_r($list);
Array ( [pop] => c [count] => 2 [isEmpty] => [bottom] => a [top] => b ) 0a b i:0;:s:1:"a";:s:1:"b";Array ( [offsetExists] => [offsetGet] => b ) SplDoublyLinkedList Object ( [flags:SplDoublyLinkedList:private] => 0 [dllist:SplDoublyLinkedList:private] => Array ( [0] => b )
)
|