php标准库(SPL)-数据结构(一)-SplDoublyLinkedList详解


SPL是用于解决典型问题(standard problems)的一组接口与类的集合。

1. SplDoublyLinkedList     
原型:


SplDoublyLinkedList implements Iterator , ArrayAccess , Countable {
/* 方法 */
public __construct ( void )
public void add ( mixed $index , mixed $newval )
public mixed bottom ( void )
public int count ( void )
public mixed current ( void )
public int getIteratorMode ( void )
public bool isEmpty ( void )
public mixed key ( void )
public void next ( 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 prev ( void )
public void push ( mixed $value )
public void rewind ( void )
public string serialize ( void )
public void setIteratorMode ( int $mode )
public mixed shift ( void )
public mixed top ( void )
public void unserialize ( string $serialized )
public void unshift ( mixed $value )
public bool valid ( void )
}

示例:


 //Constructs a new doubly linked list
    $list = new SplDoublyLinkedList();

    //Pushes an element at the end of the doubly linked list
    $list -> push('a');
    $list -> push('b');
    $list -> push('c');
    $list -> push('d');
    $list -> push('e');
    $list -> push('f');
    // Counts the number of elements in the doubly linked list.
    $list -> count();
    // Sets the mode of iteration
    $list -> setIteratorMode(SplDoublyLinkedList::IT_MODE_LIFO);
    // Returns the mode of iteration
    $model = $list -> getIteratorMode();
    /*  IT_MODE_LIFO => int(2)
     *  IT_MODE_FIFO => int(0)
     *  IT_MODE_DELETE => int(1)
     *  IT_MODE_KEEP => int(0) 
     */
    // Checks whether the doubly linked list is empty.
    $list -> isEmpty();
    // Return current node index
    $list->key();
    // Move to next entry
    $list -> next();
    // Returns whether the requested $index exists
    $list -> offsetExists(3);  
    // Returns the value at the specified $index
    $list -> offsetGet(3);
    // Sets the value at the specified $index to $newval
    $list -> offsetSet(3,'s');
    // Unsets the value at the specified $index
    $list -> offsetUnset(3);
    // Pops a node from the end of the doubly linked list
    $list -> pop();
    // Peeks at the node from the end of the doubly linked list
    $list -> top();
    // Move to previous entry
    $list -> prev();
    // Rewind iterator back to the start
    $list -> rewind();
    // Serializes the storage
    // $list -> serialize();
    // Shifts a node from the beginning of the doubly linked list
    $list -> shift();
    // Prepends the doubly linked list with an element
    $list -> unshift('s');
    // Check whether the doubly linked list contains more nodes
    $list -> valid();
    for($list -> rewind();$list -> valid();$list -> next()){
        //Return current array entry
        echo $list -> current();
    }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值