在php中实现链表
我应该如何在 PHP 中实现链表? PHP 中有内置的实现吗?
我需要做很多插入和删除操作,同时我需要保持顺序。
我想只使用 PHP,而不使用任何特殊扩展。
How should I implement a linked list in PHP? Is there a implementation built in into PHP?
I need to do a lot of insert and delete operations, and at same time I need to preserve order.
I'd like to use only PHP without any special extensions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如果不认为大多数人都了解链表是什么。基本思想是您希望保持数据组织有序,以便您可以使用当前节点访问上一个和下一个节点。其他功能,如添加、删除、插入、头部等,尽管是必要的,但都是糖。我认为 SPL 包确实涵盖了很多内容。问题是我需要一个 PHP 5.2.9 类。我想我必须自己实现它。
If don't think most people understand what linked lists are. The basic idea is you want to keep data organised is such a way that you can access the previous and next node using the current node. The other features like add, delete, insert, head etc are sugar, though necessary. I think the SPL package does cover a lot. Problem is I need a PHP 5.2.9 class. Guess I've to implement it myself.
澄清一下,使用 PHP 数组在 PHP 中实现链表可能不是一个好主意,因为 PHP 数组在底层是哈希表(不是简单的低级数组)。同时,您也无法获得指针的优势。
相反,您可以使用扩展为 PHP 实现链接列表等数据结构,这意味着您正在用 C 语言实现数据结构到 PHP。
Spl 数据结构是一个例子,另一个例子是 php-ds 扩展,特别是在链表的情况下,你可以使用这个: https://www.php.net/manual/en/class.ds-sequence.php
序列ADT是List ADT和Vector ADT的统一,所以你可以使用 Sequence ADT 实现的数据结构作为列表。
希望这可以帮助人们做出明智的选择。
Just to clarify, implementing linked list in PHP using PHP arrays probably is not a good idea, because PHP array is hash-table under the hood (not simple low-level arrays). Simultaneously, you don't get advantages of pointers.
Instead, you can implement data structures like linked list for PHP using extensions, that means you are implementing a data structure in C to PHP.
Spl data structures are an example, another example is php-ds extension, specially in case of linked lists, you can use this: https://www.php.net/manual/en/class.ds-sequence.php
Sequence ADT is the unification of List ADT and Vector ADT, so you can use Sequence ADT implemented data structures as lists.
Hope this could help someone choose wisely.
链接列表 MVC 示例 - PHP
模型代码:
控制器代码:
index.php 代码
Link list MVC Example - PHP
Model Code:
Controller Code:
index.php code
以下是 PHP 中的链表实现,取自: http://www. codediesel.com/php/linked-list-in-php/ 可以在 PHP 中添加、删除、反转和清空链表。
请注意,本文修复了一些错误。见评论。
Here is a linked list implementation in PHP pulled from: http://www.codediesel.com/php/linked-list-in-php/ which can add, delete, reverse and empty a linkedlist in PHP.
Note that some bugs have been fixed in this post. See comments.
这是php中实现链表的代码,仅使用头节点即第一个节点的引用,然后在第一个、最后一个添加和删除一个键,并维护列表中键的代码。
代码输出为:
Here is the code in php which will implement Linked List, only with the reference of head node i.e first node and then you add at first, last and delete a key, and also maintain the code of the keys in list.
Code out put as:
这是另一个使用元素数组的链表实现。 add 函数使元素保持排序。
输出为:
-0-2-3-4-5-7-10
-0-2-4-5-7-10
-2-4-5-7-10
Here is another Linked list implementation using an array of elements. The add function keeps the elements sorted.
The output is:
-0-2-3-4-5-7-10
-0-2-4-5-7-10
-2-4-5-7-10
我还尝试编写一个程序来用 PHP 创建链表。这是我写的,它对我有用。希望对回答问题有所帮助。
I was also trying to write a program to create a linked list in PHP. Here is what I have written and it worked for me. I hope it helps to answer the question.
有
SplDoublyLinkedList
。这也可以吗?There is
SplDoublyLinkedList
. Is this okay, too?