Rumah  >  Artikel  >  pembangunan bahagian belakang  >  php标准库spl之链表,堆栈,队列

php标准库spl之链表,堆栈,队列

WBOY
WBOYasal
2016-08-08 09:18:591368semak imbas

双向链表类:SplDoublyLinkedList

1. 增删节点的方法

push:向链表的尾部插入一个节点
pop:获取链表中的尾部节点,并且从链表中删除这个节点;操作不改变当前指针的位置
unshift:向链表的头部插入一个节点
shift:删除一个链表头部节点

2. 指针操作方法

rewind:使链表的当前指针指向链表的头部(即bottom)
current:获得链表当前节点指针指向的元素,必须在调用之前先调用rewind。当指向的节点被删除之后,会指向一个空节点
next:让链表当前的指针指向下一个节点,curent的返回值随之改变
bottom:获得链表头部元素,当前指针位置不变
top:获得链表尾部元素,当前指针位置不变

3.其他方法(用法见堆栈类)

valid:检查链表中是否还存在节点,循环输出时,可以用作判断
count:统计链表中节点的个数
key:返回当前节点的键值
offsetSet :设置指定键的值,注意:如键为0,在链表中0指向头部即bottom,在堆栈中指向栈顶。
offunset:注销指定键的值

<code><span></span>php
<span>/**
 * Created by 马廷广
 * User: 马廷广
 * Date: 2015/8/5
 * Time: 10:52
 */</span><span>$obj</span><span>=</span><span>new</span> SplDoublyLinkedList();
<span>$obj</span><span>-></span>push(<span>'b'</span>);
<span>$obj</span><span>-></span>push(<span>'c'</span>);
<span>$obj</span><span>-></span>unshift(<span>'a'</span>);
var_dump(<span>$obj</span>);
<span>/*   array(3) {
    [0]=>
    string(1) "a"
    [1]=>
    string(1) "b"
    [2]=>
    string(1) "c"
  }
)
*/</span><span>$obj</span><span>-></span>rewind();
var_dump(<span>$obj</span><span>-></span>current());<span>//string(1) "a"</span><span>$obj</span><span>-></span>next();
var_dump(<span>$obj</span><span>-></span>current());<span>//string(1) "a"</span>
var_dump(<span>$obj</span><span>-></span>bottom());<span>//string(1) "a"</span>
var_dump(<span>$obj</span><span>-></span>top());<span>//string(1) "c"</span>
var_dump(<span>$obj</span><span>-></span>pop());<span>//string(1) "c"</span>
var_dump(<span>$obj</span>);
<span>/*
*  array(2) {
    [0]=>
    string(1) "a"
    [1]=>
    string(1) "b"
  }
*/</span>
var_dump(<span>$obj</span><span>-></span>shift());<span>//string(1) "a"</span>
var_dump(<span>$obj</span>);
<span>/*
* array(1) {
    [0]=>
    string(1) "b"
  }
*/</span></code>

堆栈类:继承自SplDoublyLinkedList类的SplStack类

原理:堆栈类的底层是由栈实现的,栈是一种先进后出的数据结构,所以SplStack类的一些继承自SplDoublyLinkedList类的方法有一些理解上的不同,如rewind方法,spl使用rewind方法后指针将指向图中的栈顶,push和pop操作的是栈顶元素,unshift和shift操作的是栈底元素

<code><span></span>php
<span>/**
 * Created by 马廷广
 * User: 马廷广
 * Date: 2015/8/5
 * Time: 11:47
 */</span><span>$stack</span><span>=</span><span>new</span> SplStack();
<span>$stack</span><span>-></span>push(<span>'a'</span>);
<span>$stack</span><span>-></span>push(<span>'b'</span>);
<span>$stack</span><span>-></span>push(<span>'c'</span>);
echo <span>$stack</span><span>-></span>count();<span>//3</span><span>$stack</span><span>-></span>rewind();
echo <span>$stack</span><span>-></span>current();<span>//c</span><span>$stack</span><span>-></span>offsetSet(<span>0</span>,<span>'d'</span>);<span>//offsetSet中0指向的是图中的栈顶,由栈顶向下递增1,2,3,4</span><span>while</span>(<span>$stack</span><span>-></span>valid()){
    echo <span>$stack</span><span>-></span>key()<span>.</span><span>"->"</span><span>.</span><span>$stack</span><span>-></span>current();
    <span>$stack</span><span>-></span>next();
}
<span>/*2->d
1->b
0->a
*/</span></code>

队列类:继承自SplDoublyLinkedList类的SplQueue类

enqueue:进入队列
dequeue:退出队列
队列类的rewind,offsetSet等方法类似于链表

<code><?php /**
 * Created <span>by 马廷广
 * <span>User</span>: 马廷广
 * <span>Date</span>: <span>2015</span>/<span>8</span>/<span>5</span>
 * <span>Time</span>: <span>12</span>:<span>36</span>
 */

$obj = <span>new</span> SplQueue();
$obj->enqueue(<span>'a'</span>);
$obj->enqueue(<span>'b'</span>);
$obj->enqueue(<span>'c'</span>);
var_dump($obj);
/*    [<span>0</span>]<span>=></span>
    string(<span>1</span>) <span>"a"</span>
    [<span>1</span>]<span>=></span>
    string(<span>1</span>) <span>"b"</span>
    [<span>2</span>]<span>=></span>
    string(<span>1</span>) <span>"c"</span>
  }
*/
$obj->unshift(<span>"d"</span>);
$obj->push(<span>'e'</span>);
var_dump($obj);
/*
*  array(<span>5</span>) {
    [<span>0</span>]<span>=></span>
    string(<span>1</span>) <span>"d"</span>
    [<span>1</span>]<span>=></span>
    string(<span>1</span>) <span>"a"</span>
    [<span>2</span>]<span>=></span>
    string(<span>1</span>) <span>"b"</span>
    [<span>3</span>]<span>=></span>
    string(<span>1</span>) <span>"c"</span>
    [<span>4</span>]<span>=></span>
    string(<span>1</span>) <span>"e"</span>
  }
*/
$obj->rewind();
echo $obj->current();<span>//</span>d
$obj->offsetSet(<span>0</span>,<span>'h'</span>);
var_dump($obj);
/*
* array(<span>5</span>) {
    [<span>0</span>]<span>=></span>
    string(<span>1</span>) <span>"h"</span>
    [<span>1</span>]<span>=></span>
    string(<span>1</span>) <span>"a"</span>
    [<span>2</span>]<span>=></span>
    string(<span>1</span>) <span>"b"</span>
    [<span>3</span>]<span>=></span>
    string(<span>1</span>) <span>"c"</span>
    [<span>4</span>]<span>=></span>
    string(<span>1</span>) <span>"e"</span>
  }
*/</code>

版权声明:本文为博主原创文章,未经博主允许不得转载。

以上就介绍了php标准库spl之链表,堆栈,队列,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:Yii2 自定义日志文件写日志Artikel seterusnya:php的一些原理性