美文网首页
PHP标准库 (SPL)实现常用数据结构

PHP标准库 (SPL)实现常用数据结构

作者: 小伟_be27 | 来源:发表于2019-02-10 09:25 被阅读44次

php标准库(spl)

栈:先进后出,后进先出

$q = new SplStack();

$q[] = 1;

$q[] = 2;

$q[] = 3;

$q->push(4);

$q->add(4,5);

$q->rewind();

while(

$q->valid()){

echo

$q->current(),"\n";

$q->next();

}

?>

Output

5

4

3

2

1


队列:先进先出,后进后出

$queue = new SplQueue();

$queue->enqueue('A');

$queue->enqueue('B');

$queue->enqueue('C');

$queue->rewind();

while(

$queue->valid()){

echo

$queue->current(),"\n";

$queue->next();

}

print_r($queue);

$queue->dequeue(); //remove first one

print_r($queue);

?>

Output

A

B

C

SplQueue Object

(

[flags:SplDoublyLinkedList:private] => 4

[dllist:SplDoublyLinkedList:private] => Array

(

[0] => A

[1] => B

[2] => C

)

)

SplQueue Object

(

[flags:SplDoublyLinkedList:private] => 4

[dllist:SplDoublyLinkedList:private] => Array

(

[0] => B

[1] => C

)

)


小顶堆: 堆的每个父节点都小于孩子节点

$h = new SplMinHeap();

// [parent, child]

$h->insert([9, 11]);

$h->insert([0, 1]);

$h->insert([1, 2]);

$h->insert([1, 3]);

$h->insert([1, 4]);

$h->insert([1, 5]);

$h->insert([3, 6]);

$h->insert([2, 7]);

$h->insert([3, 8]);

$h->insert([5, 9]);

$h->insert([9, 10]);

for (

$h->top(); $h->valid(); $h->next()) {

list(

$parentId, $myId) = $h->current();

echo

"$myId ($parentId)\n";

}


固定长度数组:

$array = new SplFixedArray(5);

$array[1] = 2;

$array[4] = "foo";

var_dump($array[0]); // NULL

var_dump($array[1]); // int(2)

var_dump($array["4"]); // string(3) "foo"

相关文章

  • php SPL(PHP标准库讲解)

    PHP SPL标准库 官方解释: SPL 提供了一套标准的数据结构。它们按底层实现进行分组, 通常定义了它们的一般...

  • 章节八:基本数据结构二

    SPL(Standard PHP Library,PHP标准库)中并无树和图数据结构的实现,考虑到实用性,同时呼应...

  • PHP标准库 (SPL)实现常用数据结构

    php标准库(spl) 栈:先进后出,后进先出 $q = new SplStack();$q[] = 1;$q[]...

  • PHP的SPL标准库

    SPL标准PHP类库。是php内置的一些拓展类和拓展接口,其内容包含数据结构、迭代器、接口、异常、SPL函数,文件...

  • PHP中的一些标准库

    很多PHPer都不知道PHP有着自己的一些标准库,官网已经列出了SPL的PHP标准库 标准库中主要的一些数据结构 ...

  • php常用数据结构

    php有着非常强大的spl标准库,其中有对一些基本数据结构的支持,在使用php的时候基本不需要我们去手动实现这些基...

  • PHP面试题

    1,PHP SPL(PHP标准库) SPL是用于解决典型问题(standard problems)的一组接口与类的...

  • PHP SPL

    SPL就是标准库包括:迭代器,算法数据结构,堆。

  • PHP基础 -- 类自动载入

    使用PHP标准库SPL,中的自动载入功能,自动require类文件 创建4个文件 index.php主入口文件 C...

  • PHP SPL标准库之堆

    简介 堆(英语:heap)是计算机科学中一类特殊的数据结构的统称。堆通常是一个可以被看做一棵树的数组对象。堆总是满...

网友评论

      本文标题:PHP标准库 (SPL)实现常用数据结构

      本文链接:https://www.haomeiwen.com/subject/psyteqtx.html