PHP 的 SplDoublyLinkedList 类,更重要的是,一般的链表有什么意义? [英] What is the point of PHP's SplDoublyLinkedList class, and more importantly, Linked Lists in general?

查看:21
本文介绍了PHP 的 SplDoublyLinkedList 类,更重要的是,一般的链表有什么意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了扩展我的编程能力,我曾非常深入地研究 标准 PHP 库.这导致我发现了 SplDoublyLinkedList班级.从那里我阅读了链接列表维基百科上的双向链表.

我理解它们是如何工作的...但是我想不出我们为什么需要它的原因——或者更好的 SplDoublyLinkedList 的实际例子,因为我们已经在 PHP 中建立了索引和关联数组.

链接列表在 PHP 内外通常是如何使用的?

解决方案

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

<块引用>

数据结构本质上是独立于语言的,并且作为一组基于数学的逻辑概念存在.这些容器根据需要使用不同的算法以最大限度地提高效率.

例如,如果您不需要关联数组的哈希映射功能——也就是说,如果您没有将数组键用于特定目的而只需要一个枚举数组——SplFixedArray(以前称为 SplFastArray,目前未记录)可能是合适的替代品.唯一需要注意的是数组的大小是固定的,这意味着您必须在实例化类时指定大小,如果您尝试存储超过该数量的元素,则会发生错误.这就是平均而言,它的性能优于标准 PHP 数组的原因.

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

<块引用>

在构成 PHP 解释器的 C 代码中,数组被实现为称为哈希表或哈希映射的数据结构.当数组中包含的值通过其索引引用时,PHP 使用散列函数将该索引转换为唯一的散列,表示数组中相应值的位置.

此哈希映射实现使数组能够存储任意数量的元素,并使用数字键或字符串键同时提供对所有这些元素的访问.数组提供的功能非常快,是一种出色的通用数据结构.

在计算机科学中,列表被定义为值的有序集合.链表是一种数据结构,其中列表中的每个元素都包含对列表中其任一侧的一个或两个元素的引用.术语双向链表"用于指后一种情况.在 SPL 中,这采用类 SplDoublyLinkedList 的形式......当要存储的元素数量事先未知并且元素只需要按顺序访问时,使用列表是有意义的.

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

On a quest to expand my programming prowess, I've delved ever-so-slightly into The Standard PHP Library. This led to my discovery of the SplDoublyLinkedList class. From there I read the descriptions of Linked Lists and Doubly Linked Lists on Wikipedia.

I understand how they work... But I cannot conceive of a reason WHY we need it—or better yet a practical example of SplDoublyLinkedList since we have indexed and associative arrays in PHP.

How are Linked Lists normally used in-and-out of PHP?

解决方案

The SPL data structures reduce memory consumption and improve performance. Good explanations:

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 的 SplDoublyLinkedList 类,更重要的是,一般的链表有什么意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆