SplDoublyLinkedList常用操作

类结构

class SplDoublyLinkedList implements Iterator, 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 () {}

}

先生成一个双向链表

//先生成一个双向链表
$splDoublyLinkedList=new SplDoublyLinkedList();
for ($i=1;$i<=100;$i++)
{
    $splDoublyLinkedList->push($i);
}

add 添加修改链表中的节点的数据

//修改节点标识0的数据为2
$splDoublyLinkedList->add(0,2);
$splDoublyLinkedList->rewind();
//测试下结果  为2
var_dump($splDoublyLinkedList->current());

bottom 返回链表中的节点头

var_dump($splDoublyLinkedList->bottom());

count 返回链表中的节点数量

$count=$splDoublyLinkedList->count();
var_dump($count);

current 返回当前双向链接节点指针指向的节点

$splDoublyLinkedList->rewind();
$current=$splDoublyLinkedList->current();
var_dump($current);

getIteratorMode 获取迭代器模式

//获取迭代器模式
$mode=$splDoublyLinkedList->getIteratorMode();
var_dump($mode);
$splDoublyLinkedList->setIteratorMode(SplDoublyLinkedList::IT_MODE_FIFO | SplDoublyLinkedList::IT_MODE_DELETE);
$mode=$splDoublyLinkedList->getIteratorMode();
var_dump($mode);

isEmpty 返回双向链表是否为空

$emptyDoublyLinedList=new SplDoublyLinkedList();
$isEmpty=$emptyDoublyLinedList->isEmpty();
var_dump($isEmpty);

key 返回双向链表的当前节点key

$key=$splDoublyLinkedList->key();
var_dump($key);//0

$splDoublyLinkedList->next();
$key=$splDoublyLinkedList->key();
var_dump($key);//1

next 节点指针下移

$splDoublyLinkedList->rewind();
$val=$splDoublyLinkedList->current();
var_dump($val);//1
$splDoublyLinkedList->next();
$val=$splDoublyLinkedList->current();
var_dump($val);//2

offsetExists 检查当前双向链表是否存在key

$offsetExists=$splDoublyLinkedList->offsetExists(101);
var_dump($offsetExists);//false  101不存在
$offsetExists=$splDoublyLinkedList->offsetExists(55);
var_dump($offsetExists); //true  55存在

offsetGet 获取当前双向链表中key的数据

$val=$splDoublyLinkedList->offsetGet(50);
var_dump($val);//51
try{
    $val=$splDoublyLinkedList->offsetGet(120);

}
catch (Exception $e)
{
    //抛出异常 超过范围  Offset invalid or out of range
    var_dump($e->getMessage());
}

offsetSet 设置key的数据

$splDoublyLinkedList->offsetSet(50,2222);
var_dump($splDoublyLinkedList->offsetGet(50));//2222
try{
    $val=$splDoublyLinkedList->offsetSet(120,555);

}
catch (Exception $e)
{
    //抛出异常 超过范围  Offset invalid or out of range
    var_dump($e->getMessage());
}

offsetUnset 删除节点

$val=$splDoublyLinkedList->offsetGet(50);
var_dump($val);//51
$splDoublyLinkedList->offsetUnset(50);
$val=$splDoublyLinkedList->offsetGet(50);
var_dump($val);//52  不是51了 51已被删除

pop 从数组结尾取出一个数据 bottom为头 top为尾

$top=$splDoublyLinkedList->top();
var_dump($top);//100

$val=$splDoublyLinkedList->pop();
var_dump($val);//100

$top=$splDoublyLinkedList->top();
var_dump($top);//99

prev 双向链表指针数组上移

$splDoublyLinkedList->rewind();
$val=$splDoublyLinkedList->current();
var_dump($val);//1
$splDoublyLinkedList->next();
$splDoublyLinkedList->next();
$val=$splDoublyLinkedList->current();
var_dump($val);//3
$splDoublyLinkedList->prev();
$val=$splDoublyLinkedList->current();
var_dump($val);//2

push 双向链表尾部添加数据

$top=$splDoublyLinkedList->top();
var_dump($top);//100
$splDoublyLinkedList->push(1024);
$top=$splDoublyLinkedList->top();
var_dump($top);//1024

rewind 节点指针移动到头部

$splDoublyLinkedList->rewind();
$val=$splDoublyLinkedList->current();
var_dump($val);//1
$splDoublyLinkedList->next();
$splDoublyLinkedList->next();
$val=$splDoublyLinkedList->current();
var_dump($val);//3
$splDoublyLinkedList->rewind();
$val=$splDoublyLinkedList->current();
var_dump($val);//1

serialize 返回双向链表序列化字符串

//返回双向链表序列化字符串
$serialize= $splDoublyLinkedList->serialize();
var_dump($serialize);

setIteratorMode 设置迭代器模式

$mode=$splDoublyLinkedList->getIteratorMode();
var_dump($mode);
$splDoublyLinkedList->setIteratorMode(SplDoublyLinkedList::IT_MODE_FIFO | SplDoublyLinkedList::IT_MODE_DELETE);
$mode=$splDoublyLinkedList->getIteratorMode();
var_dump($mode);

shift 从链表头部取出一条数据

$val=$splDoublyLinkedList->shift();
var_dump($val);//1
$val=$splDoublyLinkedList->shift();
var_dump($val);//2

top 返回链表尾部的一个数据

$val=$splDoublyLinkedList->top();
var_dump($val);//100

unshift 加入一条数据到链表头部

$splDoublyLinkedList->rewind();
$splDoublyLinkedList->unshift(111);
var_dump($splDoublyLinkedList->bottom());//111

unserialize 反序列化 字符串

//先生成一个双向链表
$splDoublyLinkedList=new SplDoublyLinkedList();
for ($i=1;$i<=100;$i++)
{
    $splDoublyLinkedList->push($i);
}

//生成一个空的双向链表
$sp1=new SplDoublyLinkedList();
//序列化第一个双向链表
$serialize =$splDoublyLinkedList->serialize();
//用空的双向链表反序列化这个序列化字符串
$sp1->unserialize($serialize);
foreach ($sp1 as $key=>$val)
{
    var_dump($val);
}
//这个空的双向链表反序列化后不为空了

valid 返回双向链表是否挂载更多的节点

$check=$splDoublyLinkedList->valid();
var_dump($check);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值