前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >php版链表的实现

php版链表的实现

作者头像
魔王卷子
发布2019-05-31 10:33:26
5510
发布2019-05-31 10:33:26
举报
代码语言:javascript
复制
<?php
class Node {
    private $data;
    private $next;

    public function getData() {
        return $this->data;
    }

    public function setData($data) {
        $this->data = $data;
        return true;
    }

    public function getNext() {
        return $this->next;
    }

    public function setNext($next) {
        $this->next = $next;
        return true;
    }
}

/**
 * 链表类
 */
class Link {
    private $size = 0;
    private $first;
    private $last;

    /**
     * 获取链表长度
     */
    public function getLength() {
        return $this->size;
    }

    /**
     * 链表中插入第一个元素的时候,头和尾部都是同一个元素
     */
    public function oneNode(string $element) {
        $this->first = new Node;
        $this->first->setData($element);
        $this->last = $this->first;
    }

    /**
     * 当只有一个元素的时候,删除$fist和$last
     */
    public function clear() {
        $this->first = $this->last = null;
    }
    
    /**
     * 头节点插入
     */
    public function addHead(string $element) {
        if ($this->size == 0) {
            $this->oneNode($element);
        } else {
            $node = new Node;
            $node->setData($element);
            $node->setNext($this->first);
            $this->first = $node;
        }
        $this->size++;
        return true;
    }
    
    /**
     * 尾节点插入
     */
    public function addTail(string $element) {
        if ($this->size == 0) {
            $this->oneNode($element);
        } else {
            $node = new Node();
            $node->setData($element);
            $this->last->setNext($node);
            $this->last = $node;
        }

        $this->size++;
    }

    /**
     * 中间节点插入
     */
    public function add(int $index, string $element) {
        if ($index <= $this->size) {
            if ($this->size == 0) {
                oneNode($element);
            } elseif ($index == 0) {
                $this->addHead($element);
            } elseif ($index == $this->size) {
                $this->addTail($element);
            } else {
                $tmp = $this->get($index - 1);
                $node = new Node;
                $node->setData($element);
                $node->setNext($tmp->getNext());
                $tmp->setNext($node);
            }
            $this->size++;
        } else {
            throw new \Exception("插入位置无效或超出链表长度");
        }
    }
    
    /**
     * 获取指定位置的元素
     */
    public function get(int $index) {
        $tmp = $this->first;
        for ($i = 0; $i < $index - 1; $i++) {
            $tmp = $tmp->getNext();
        }
        return $tmp;
    }

    /**
     * 删除头节点
     */
    public function deleteFirst() {
        if ($this->size == 0) {
            throw new \Exception("空链表,无元素可删除");
        } elseif ($this->size == 1) {
            $this->clear();
        } else {
            $tmp = $this->first;
            $this->first = $tmp->getNext();
            $this->size--;
        }
    }

    /**
     * 删除尾节点
     */
    public function deleteLast() {
        if ($this->size == 0) {
            throw new \Exception("空链表,无元素可删除");
        } elseif ($this->size == 1) {
            $this->clear();
        } else {
            $tmp = $this->get($this->size - 1);
            $tmp->setNext(null);
            $this->size--;
        }
    }

    /**
     * 删除指定节点
     */
    public function deleteIndex(int $index) {
        if ($this->size == 0) {
            throw new \Exception("空链表,无元素可删除");
        } elseif ($this->size == 1) {
            $this->clear();
        } else {
            $tmp = $this->get($index - 1);
            $tmp->setNext($tmp->getNext()->getNext());
            $this->size--;
        }
    }
    
    /**
     * 反转节点
     */
    public function receve() {
        if ($this->size < 2) {
            return true;
        } else {
            $tmp = $this->first;
            $last = $tmp;
            $next = $this->first->getNext();
            for($i = 0; $i < $this->size - 1; $i++) {
                $nextNext = $next->getNext();
                $next->setNext($tmp);
                $tmp = $next;
                $next = $nextNext;
            }
            $last->setNext(null);
            $this->first = $tmp;
            return true;
        }
    }

    /**
     * 打印现有的所有元素
     */
    public function printLink() {
        $tmp = $this->first;
        if(is_null($tmp)) {
            return false;
        } 
        echo $tmp->getData();
        while(!is_null($tmp->getNext())) {
            $tmp = $tmp->getNext();
            echo "->" . $tmp->getData();
        }
        echo "\n";
    }
}

$link = new Link();
$link->addHead("1");
$link->printLink(); // 1

$link->addHead("5");
$link->printLink(); // 5->1

$link->addTail("9");
$link->printLink(); // 5->1->9

$link->addTail("7");
$link->printLink(); // 5->1->9->7

$link->add(3, "8"); 
$link->printLink(); // 5->1->9->8->7

print_r("链表长度:" . $link->getLength() . "\n");

$link->deleteFirst();
$link->printLink();

$link->deleteLast();
$link->printLink();

$link->deleteIndex(1);
$link->printLink();

print_r("链表长度:" . $link->getLength() . "\n");
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-04-242,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档