5. Data structure (implemented in PHP) — set (implemented with linked list)

Time:2022-1-23

1. Features

  • The elements in the collection will not be repeated, so you need to judge whether there are elements when adding

2. Time complexity analysis

operation Time complexity
add to O(1)
delete O(n)
query O(n)

3. Code

  • Element Nodes

/**
 *Content: element node of the collection
 * create: 2020-11-03
 */
<?php
namespace SetBundle;

class Node
{
    /**
     *Tail pointer
     * @var Node|null
     */
    protected $tail;

    /**
     *The node stores the amount of data
     * @var mixed
     */
    protected $value;

    public function __construct($value = null, ?Node $tail = null)
    {
        $this->setTail($tail)->setValue($value);
    }

    /**
     *Set tail node
     * @param Node|null $tail
     * @return $this
     */
    public function setTail(?Node $tail): self
    {
        $this->tail = $tail;
        return $this;
    }

    /**
     *Get tail node
     * @return Node|null
     */
    public function getTail(): ?Node
    {
        return $this->tail;
    }

    /**
     *Set value
     * @param mixed $value
     * @return $this
     */
    public function setValue($value): self
    {
        $this->value = $value;
        return $this;
    }

    /**
     *Get the value in the node
     * @return mixed
     */
    public function getValue()
    {
        return $this->value;
    }
}
<?php
/**
 *Content: Collection (linked list implementation)
 *Auth: Yu Jiaming
 * create: 2020-11-03
 */
namespace SetBundle;

class LinkSet
{
    /**
     * @var Node|null
     */
    protected $node;

    /**
     *Collection size
     * @var int
     */
    protected $size;

    public function __construct()
    {
        $this->node = null;
        $this->size = 0;
    }

    /**
     *Gets the element in the collection
     * @return int
     */
    public function getSize(): int
    {
        return $this->size;
    }

    /**
     *Query whether there is a node with this value
     * @param $value
     * @return Node|null
     */
    public function contains($value): ?Node
    {
        $node = $this->node;
        while (!is_null($node)) {
            if ($node->getValue() === $value) {
                return $node;
            }
            $node = $node->getTail();
        }
        return null;
    }

    /**
     *Insert
     * @param string|int $value
     * @return Node
     * @throws \Exception
     */
    public function add($value): Node
    {
        $newNode = new Node($value, null);

        if (is_null($this->node)) {
            $this->node = $newNode;
        } else {
            if (!is_null($this->contains($value))) {
                Throw new \ exception ('the inserted element already exists! ');
            }
            $newNode->setTail($this->node);
            $this->node = $newNode;
        }
        $this->size++;
        return $newNode;
    }

    /**
     *Delete
     * @param $value
     */
    public function remove($value)
    {
        //If the element does not exist in the diagram, it is returned
        if (is_null($this->node)) return;
        
        //If the first node is deleted
        if ($this->node->getKey() == $key) {
            $this->node = $this->node->getTail();
            $this->size--;
            return;
        }
        //Delete the second node and later
        $node = $this->node;
        while (!is_null($node->getTail())) {
            if ($node->getTail()->getKey() === $key) {
                $node->setTail($node->getTail()->getTail());
                $this->size--;
                return;
            } 
            $node = $node->getTail();
        }
        return;
    }

    public function varDump()
    {
        $node = $this->node;
        while (!is_null($node)) {
            echo $node->getValue(). PHP_EOL;
            $node = $node->getTail();
        }
    }
}

4. Examples

<?php
require_once __DIR__ . '/../../vendor/autoload.php';
$set = new SetBundleLinkSet();
$set->add(1);
$set->add(2);
$set->add(3);
$set->add(4);
$set->add(5);
$set->add(6);
$set->varDump();
6
5
4
3
2
1