PHP实现数据结构与算法之线性表

一、数组

class ArrayList
{
    private $data;
    private $capacity;
    private $length;

    public function __construct($capacity)
    {
        $capacity = intval($capacity);
        if ($capacity <= 0) return null;

        $this->data = array();
        $this->capacity = $capacity;
        $this->length = 0;
    }

    public function getLength()
    {
        return $this->length;
    }

    public function insert($index, $value)
    {
        $index = intval($index);
        if ($index < 0) return false;
        if ($index > $this->getLength()) return false;
        if ($this->checkIfFull()) return false;

        for ($i = $this->getLength(); $i > $index; --$i) {
            $this->data[$i] = $this->data[$i - 1];
        }

        $this->data[$index] = $value;
        ++$this->length;
        return true;
    }

    public function delete($index)
    {
        $index = intval($index);
        if ($index < 0) return false;
        if ($this->checkOutOfRange($index)) return false;

        $value = $this->data[$index];
        for ($i = $index; $i < $this->getLength() - 1; ++$i) {
            $this->data[$i] = $this->data[$i + 1];
        }

        --$this->length;
        return $value;
    }

    public function find($index)
    {
        $index = intval($index);
        if ($index < 0) return [0, -1];
        if ($this->checkOutOfRange($index)) return [0, -1];

        return [1, $this->data[$index]];
    }

    public function printData()
    {
        $string = '';
        for ($i = 0; $i < $this->getLength(); ++$i) {
            $string .= " [{$this->data[$i]}] ";
        }

        echo "{$string}\n length={$this->getLength()}\n";
    }

    private function checkOutOfRange($index)
    {
        $index = intval($index);

        return $index >= $this->length;
    }

    private function checkIfFull()
    {
        return $this->length == $this->capacity;
    }
}

二、链表

class SingleLinkedListNode
{
    public $data;

    public $next;

    public function __construct($data = null)
    {
        $this->data = $data;
        $this->next = null;
    }
}

class TestSingleLinkedList
{
    private $header;
    private $length;

    public function __construct(SingleLinkedListNode $header = null)
    {
        $this->header = !is_null($header) ? $header : new SingleLinkedListNode();
        $this->length = 0;
    }

    public function getLength()
    {
        return $this->length;
    }

    public function insert($data)
    {
        return $this->insertDataAfter($this->header, $data);
    }

    public function insertDataBefore(SingleLinkedListNode $node, $data)
    {
        if (is_null($node)) return false;

        $new = new SingleLinkedListNode($data);

        $preNode = $this->getPreNode($node);
        $new->next = $preNode->next;
        $preNode->next = $new;

        ++$this->length;
        return true;
    }

    public function insertDataAfter(SingleLinkedListNode $node, $data)
    {
        if (is_null($node)) return false;

        $new = new SingleLinkedListNode($data);

        $new->next = $node->next;
        $node->next = $new;

        ++$this->length;
        return true;
    }

    public function insertNodeAfter(SingleLinkedListNode $node, SingleLinkedListNode $new)
    {
        if (is_null($node)) return false;
        if (is_null($new)) return false;

        $new->next = $node->next;
        $node->next = $new;

        ++$this->length;
        return true;
    }

    public function delete(SingleLinkedListNode $node)
    {
        if (is_null($node)) return false;

        $preNode = $this->getPreNode($node);
        $preNode->next = $preNode->next->next;

        --$this->length;
        return true;
    }

    /**
     * 删除链表倒数第n个节点
     * @param $last
     * @return bool
     */
    public function deleteLastNode($last)
    {
        $last = intval($last);
        if ($last > $this->getLength()) return false;

        $slow = $fast = $this->header;
        $mark = 0;

        while (!is_null($fast->next)) {
            if ($mark >= $last) {
                $slow = $slow->next;
            }
            $fast = $fast->next;
            ++$mark;
        }

        $slow->next = $slow->next->next;
        --$this->length;
        return true;
    }

    public function getNodeByIndex($index)
    {
        $index = intval($index);
        if ($index < 0) return null;
        if ($index >= $this->length) return null;

        $currNode = $this->header;
        $i = 0;
        while (!is_null($currNode->next) && $index != $i++) {
            $currNode = $currNode->next;
        }

        return $currNode->next;
    }

    public function printData()
    {
        $string = '';

        $currNode = $this->header;
        $len = $this->getLength();
        while (!is_null($currNode->next) && $len--) {
            $currNode = $currNode->next;
            $string .= " [$currNode->data] ";
        }

        echo "{$string}\n length={$this->getLength()}\n";
    }

    public function getPreNode(SingleLinkedListNode $node)
    {
        if (is_null($node)) return null;

        $currNode = $this->header;
        while ($currNode->next !== $node && !is_null($currNode->next)) {
            $currNode = $currNode->next;
        }

        return $currNode;
    }

    /**
     * 单链表反转
     */
    public function reverse()
    {
        if ($this->getLength() <= 1) return;

        $preNode = null;
        $remainNode = null;
        $currNode = $this->header->next;
        while (!is_null($currNode)) {
            $remainNode = $currNode->next;
            $currNode->next = $preNode;
            $preNode = $currNode;
            $currNode = $remainNode;
        }

        $this->header->next = $preNode;
    }
}

三、栈

四、队列

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值