PHP标准库(SPL)- SplDoublyLinkedList类(双向链表)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP标准库(SPL)- SplDoublyLinkedList类(双向链表)相关的知识,希望对你有一定的参考价值。

class SplDoublyLinkedList implements Iterator, Traversable, Countable, ArrayAccess {
        const IT_MODE_LIFO = 2;
        const IT_MODE_FIFO = 0;
        const IT_MODE_DELETE = 1;
        const IT_MODE_KEEP = 0;


        /**
* Add/insert a new value at the specified index * @param mixed $index The index where the new value is to be inserted. * @param mixed $newval The new value for the index. * @link http://php.net/spldoublylinkedlist.add * @return void * @since 5.5.0
*/ public function add($index, $newval) {} /** * Pops a node from the end of the doubly linked list * @link http://php.net/manual/en/spldoublylinkedlist.pop.php * @return mixed The value of the popped node. * @since 5.3.0 */ public function pop () {} /** * Shifts a node from the beginning of the doubly linked list * @link http://php.net/manual/en/spldoublylinkedlist.shift.php * @return mixed The value of the shifted node. * @since 5.3.0 */ public function shift () {} /** * Pushes an element at the end of the doubly linked list * @link http://php.net/manual/en/spldoublylinkedlist.push.php * @param mixed $value <p> * The value to push. * </p> * @return void * @since 5.3.0 */ public function push ($value) {} /** * Prepends the doubly linked list with an element * @link http://php.net/manual/en/spldoublylinkedlist.unshift.php * @param mixed $value <p> * The value to unshift. * </p> * @return void * @since 5.3.0 */ public function unshift ($value) {} /** * Peeks at the node from the end of the doubly linked list * @link http://php.net/manual/en/spldoublylinkedlist.top.php * @return mixed The value of the last node. * @since 5.3.0 */ public function top () {} /** * Peeks at the node from the beginning of the doubly linked list * @link http://php.net/manual/en/spldoublylinkedlist.bottom.php * @return mixed The value of the first node. * @since 5.3.0 */ public function bottom () {} /** * Counts the number of elements in the doubly linked list. * @link http://php.net/manual/en/spldoublylinkedlist.count.php * @return int the number of elements in the doubly linked list. * @since 5.3.0 */ public function count () {} /** * Checks whether the doubly linked list is empty. * @link http://php.net/manual/en/spldoublylinkedlist.isempty.php * @return bool whether the doubly linked list is empty. * @since 5.3.0 */ public function isEmpty () {} /** * Sets the mode of iteration * @link http://php.net/manual/en/spldoublylinkedlist.setiteratormode.php * @param int $mode <p> * There are two orthogonal sets of modes that can be set: * </p> * The direction of the iteration (either one or the other): * <b>SplDoublyLinkedList::IT_MODE_LIFO</b> (Stack style) * @return void * @since 5.3.0 */ public function setIteratorMode ($mode) {} /** * Returns the mode of iteration * @link http://php.net/manual/en/spldoublylinkedlist.getiteratormode.php * @return int the different modes and flags that affect the iteration. * @since 5.3.0 */ public function getIteratorMode () {} /** * Returns whether the requested $index exists * @link http://php.net/manual/en/spldoublylinkedlist.offsetexists.php * @param mixed $index <p> * The index being checked. * </p> * @return bool true if the requested <i>index</i> exists, otherwise false * @since 5.3.0 */ public function offsetExists ($index) {} /** * Returns the value at the specified $index * @link http://php.net/manual/en/spldoublylinkedlist.offsetget.php * @param mixed $index <p> * The index with the value. * </p> * @return mixed The value at the specified <i>index</i>. * @since 5.3.0 */ public function offsetGet ($index) {} /** * Sets the value at the specified $index to $newval * @link http://php.net/manual/en/spldoublylinkedlist.offsetset.php * @param mixed $index <p> * The index being set. * </p> * @param mixed $newval <p> * The new value for the <i>index</i>. * </p> * @return void * @since 5.3.0 */ public function offsetSet ($index, $newval) {} /** * Unsets the value at the specified $index * @link http://php.net/manual/en/spldoublylinkedlist.offsetunset.php * @param mixed $index <p> * The index being unset. * </p> * @return void * @since 5.3.0 */ public function offsetUnset ($index) {} /** * Rewind iterator back to the start * @link http://php.net/manual/en/spldoublylinkedlist.rewind.php * @return void * @since 5.3.0 */ public function rewind () {} /** * Return current array entry * @link http://php.net/manual/en/spldoublylinkedlist.current.php * @return mixed The current node value. * @since 5.3.0 */ public function current () {} /** * Return current node index * @link http://php.net/manual/en/spldoublylinkedlist.key.php * @return mixed The current node index. * @since 5.3.0 */ public function key () {} /** * Move to next entry * @link http://php.net/manual/en/spldoublylinkedlist.next.php * @return void * @since 5.3.0 */ public function next () {} /** * Move to previous entry * @link http://php.net/manual/en/spldoublylinkedlist.prev.php * @return void * @since 5.3.0 */ public function prev () {} /** * Check whether the doubly linked list contains more nodes 当前指针处元素是否有效 * @link http://php.net/manual/en/spldoublylinkedlist.valid.php * @return bool true if the doubly linked list contains any more nodes, false otherwise. * @since 5.3.0 */ public function valid () {} /** * Unserializes the storage * @link http://php.net/manual/ru/spldoublylinkedlist.serialize.php * @param string $serialized The serialized string. * @return void * @since 5.4.0 */ public function unserialize($serialized) {} /** * Serializes the storage * @link http://php.net/manual/ru/spldoublylinkedlist.unserialize.php * @return string The serialized string. * @since 5.4.0 */ public function serialize () {} }

用例

<?php
header("Content-type: text/html; charset=gb2312");
$doubly=new SplDoublyLinkedList();
$doubly->push(‘a‘);
$doubly->push(‘b‘);
$doubly->push(‘c‘);
$doubly->push(‘d‘);
$doubly->add(1,‘e‘);

echo ‘Original:‘."\n";
var_dump($doubly);

echo ‘FIFO|KEEP:‘."\n";
$doubly->setIteratorMode(SplDoublyLinkedList::IT_MODE_FIFO | SplDoublyLinkedList::IT_MODE_KEEP);
$doubly->rewind();
foreach($doubly as $key=>$value)
{
    echo $key.‘ ‘.$value."\n";
}

echo ‘LIFO|KEEP:‘."\n";
$doubly->setIteratorMode(SplDoublyLinkedList::IT_MODE_LIFO | SplDoublyLinkedList::IT_MODE_KEEP);
$doubly->rewind();
foreach($doubly as $key=>$value)
{
    echo $key.‘ ‘.$value."\n";
}

echo ‘LIFO|DELETE:‘."\n";
$doubly->setIteratorMode(SplDoublyLinkedList::IT_MODE_LIFO | SplDoublyLinkedList::IT_MODE_DELETE);
$doubly->rewind();
foreach($doubly as $key=>$value)
{
    if($key == 1) break;
    echo $key.‘ ‘.$value."\n";
}
echo ‘List after Delete‘."\n";
var_dump($doubly);
?>

 


以上是关于PHP标准库(SPL)- SplDoublyLinkedList类(双向链表)的主要内容,如果未能解决你的问题,请参考以下文章

夯实PHP基础PHP标准库 SPL

PHP标准库(SPL)- SplDoublyLinkedList类(双向链表)

PHP的SPL扩展库数据结构

php spl库的使用

PHP SPL使用方法 自动加载和迭代器

PHP SPL RecursiveIterator::getChildren() 究竟返回了啥?