sergiobelya/data-structures
最新稳定版本:1.0.0
Composer 安装命令:
composer require sergiobelya/data-structures
包简介
PHP library for data structures like Sorted Linked List
README 文档
README
PHP library for data structures like Sorted Linked List
Installation
composer require sergiobelya/data-structures
Sorted Linked List
The data structure that consists of elements, each containing a link to the next element, and all elements are always kept sorted.
One instance of SortedLinkedList contains int or string values but not both.
Available actions
- add
- delete
- shift
- pop
- isValueExists
- count
- reverse
- toArray
- iterate
Usages
Notice: each $value used for one instance of SortedLinkedList should be int or string but not both, otherwise \InvalidArgumentException will be thrown.
Add
use Sergiobelya\DataStructures\SortedLinkedList; // 1 example $list = new SortedLinkedList(); while ($value = /* some function, e.g. fetch record from DB, read row from file, pop value from array, etc. */) { $list->add($value); } // 2 example $sortedListInt = new SortedLinkedList(); $sortedListInt->add(5); $sortedListInt->add(15); $sortedListInt->add(10); // 3 example $sortedListString = new SortedLinkedList(); $sortedListString->add('USA'); $sortedListString->add('Canada'); $sortedListString->add('Mexico');
Delete
// Delete value from SortedLinkedList<int> $sortedListInt->delete(10); // Delete value from SortedLinkedList<string> $sortedListString->delete('Mexico');
Shift & Pop
$firstValue = $list->shift(); $lastValue = $list->pop();
Is Value Exists
if ($list->isValueExists('USA')) { // do something }
Count
$countItems = $list->count(); // or $countItems = count($list);
Reverse
// 1st option - call reverse() before add() $sortedList = new SortedLinkedList(); $sortedList->reverse(); foreach ($originalData as $value) { // add values in descending order $sortedList->add($value); } // 2nd option - call reverse() after add() $sortedList = new SortedLinkedList(); foreach ($originalData as $value) { // add values in ascending order $sortedList->add($value); } // reverse all values in descending order $sortedList->reverse();
To Array
$data = $list->toArray();
Iterations
foreach ($list as $value) { // do something }
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-07