Download the PHP package obernard/linkedlist without Composer
On this page you can find all versions of the php package obernard/linkedlist. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download obernard/linkedlist
More information about obernard/linkedlist
Files in obernard/linkedlist
Informations about the package linkedlist
🖇 LinkedList
Linked List implementation in PHP.
Installation
Usage of final class LifoList
Final classes like LifoList
are given as examples of implementation of abstract linked-list classes but extending AbstractSinglyLinkedList
or AbstractDoublyLinkedList
offers much more coding potentials.
Create an empty list and add nodes.
Usage of Abstract classes
Abstract singly-linked list supports the use of abstract doubly-linked nodes but as a good practice use singly linked nodes inside singly-linked lists.
Your concrete list class links instances of your concrete node classe. Concrete list classes may create node objects - like LifoList
class does - or may not.
In this example, MyList
class does not create nodes by itself:
A Node classe implements IterableNodeInterface
getKey
and getValue
methods through AbstractNode
. You may need to replace one or the other.
getValue
method determines what is returned as value when iterating the list.
In above example, we decide that foreach
statement iterate over $data
node property.
If you want to iterate over Node objects, do not replace getValue
because AbstractNode->getValue()
already returns $this
.
getKey
method determines what is returned as key when iterating the list.AbstractNode->getkey()
argument$index
is binded with the iterator position index. But you can replace the method and make it return whatever suites your needs.
@see AbstractCommonList
key()
and current()
methods to see how the magic works.
Dialogue between nodes
An interesting design pattern is to make nodes communicate through the list.
See AbstractNode
distanceToLastNode()
method as an example of inter-nodes communication:
This design pattern is based on recursivity.
The time complexity of such recursive methods is 0(n)
where n
is the number of nodes.
If your configuration has xdebug
module enabled, the use of such recursive function calls may raise the following error if your list length get close to 256:
If you don't want to modify your xdebug
config by increasing the xdebug.max_nesting_level
setting, just don't use that pattern design for long lists.
Be carefull not to use recursive communication methods between nodes when iterating over nodes because the time complexity would be O(n2) leading to very poor performance.
Tests
Run composer test
.
Contributing
Improvements are welcome! Feel free to submit pull requests.
Licence
MIT
Copyright (c) 2021 Olivier BERNARD