php - PHP 的 SplDoublyLinkedList 类,更重要的是,一般的链表有什么意义?

标签 php data-structures linked-list spl

为了提高我的编程能力,我深入研究了 The Standard PHP Library .这导致我发现了 SplDoublyLinkedList类(class)。从那里我阅读了 Linked Lists 的描述和 Doubly Linked Lists在维基百科上。

我明白它们是如何工作的……但我想不出我们需要它的原因——或者更好的是 SplDoublyLinkedList 的实际示例,因为我们在 PHP 中有索引和关联数组。

链接列表通常如何在 PHP 中进出使用?

最佳答案

SPL 数据结构减少了内存消耗并提高了性能。很好的解释:

Data structures are inherently language-independent and exist as a set of logical concepts based in mathematics. These containers use different algorithms as appropriate to maximize efficiency.

For example, if you don't need the hash map capabilities of an associative array -- that is, if you aren't using the array key for a specific purpose and only need an enumerated array -- SplFixedArray (formerly SplFastArray, currently undocumented) may be a suitable replacement. The only caveat is that the size of the array is fixed, meaning that you must specify the size when you instantiate the class and an error will occur if you attempt to store more than that number of elements. This is the reason that, on average, it performs better than standard PHP arrays.

http://web.archive.org/web/20130805120049/http://blueparabola.com/blog/spl-deserves-some-reiteration

Within the C code that makes up the PHP interpreter, arrays are implemented as a data structure called a hash table or hash map. When a value contained within an array is referenced by its index, PHP uses a hashing function to convert that index into a unique hash representing the location of the corresponding value within the array.

This hash map implementation enables arrays to store an arbitrary number of elements and provide access to all of those elements simultaneously using either numeric or string keys. Arrays are extremely fast for the capabilities they provide and are an excellent general purpose data structure.

In computer science, a list is defined as an ordered collection of values. A linked list is a data structure in which each element in the list includes a reference to one or both of the elements on either side of it within the list. The term “doubly-linked list” is used to refer to the latter case. In the SPL, this takes the form of the class SplDoublyLinkedList.... It makes sense to use lists when the number of elements to be stored is not known in advance and the elements only need to be accessed by sequential position.

http://matthewturland.com/2010/05/20/new-spl-features-in-php-5-3/

关于php - PHP 的 SplDoublyLinkedList 类,更重要的是,一般的链表有什么意义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3963288/

相关文章:

php - PHP MYSQL覆盖最后一个连接链接

javascript 函数冲突或 css 选择冲突

php - 使用PHP登录系统

c - 在循环链表开头插入

c - 学习C,[1]为什么要在main中获得无限循环? [2]为什么在不触摸文件检查代码的情况下出现段错误

java - 从链表中的方法递归调用

javascript - 将返回值从 php 传递给 js

algorithm - 如何存储和收集最近24小时、最近7天、最近30天、最近365天浏览次数最多的信息来挖掘?

java - 为什么我的 BST 不会写入文件?

algorithm - 什么数据结构或算法用于自动完成?