php实现单链表的实例代码

 更新时间:2013年03月22日 09:59:26   作者:  
php实现单链表的实例代码,需要的朋友可以参考一下

复制代码 代码如下:

<?php

//链表节点
class node {
    public $id; //节点id
    public $name; //节点名称
    public $next; //下一节点
   

    public function __construct($id, $name) {
        $this->id = $id;
        $this->name = $name;
        $this->next = null;
    }
}

//单链表
class singelLinkList {
    private $header; //链表头节点
   

    //构造方法
    public function __construct($id = null, $name = null) {
        $this->header = new node ( $id, $name, null );
    }

    //获取链表长度
    public function getLinkLength() {
        $i = 0;
        $current = $this->header;
        while ( $current->next != null ) {
            $i ++;
            $current = $current->next;
        }
        return $i;
    }

    //添加节点数据
    public function addLink($node) {
        $current = $this->header;
        while ( $current->next != null ) {
            if ($current->next->id > $node->id) {
                break;
            }
            $current = $current->next;
        }
        $node->next = $current->next;
        $current->next = $node;
    }

    //删除链表节点
    public function delLink($id) {
        $current = $this->header;
        $flag = false;
        while ( $current->next != null ) {
            if ($current->next->id == $id) {
                $flag = true;
                break;
            }
            $current = $current->next;
        }
        if ($flag) {
            $current->next = $current->next->next;
        } else {
            echo "未找到id=" . $id . "的节点!<br>";
        }
    }

    //获取链表
    public function getLinkList() {
        $current = $this->header;
        if ($current->next == null) {
            echo ("链表为空!");
            return;
        }
        while ( $current->next != null ) {
            echo 'id:' . $current->next->id . '   name:' . $current->next->name . "<br>";
            if ($current->next->next == null) {
                break;
            }
            $current = $current->next;
        }
    }

    //获取节点名字
    public function getLinkNameById($id) {
        $current = $this->header;
        if ($current->next == null) {
            echo "链表为空!";
            return;
        }
        while ( $current->next != null ) {
            if ($current->id == $id) {
                break;
            }
            $current = $current->next;
        }
        return $current->name;
    }

    //更新节点名称
    public function updateLink($id, $name) {
        $current = $this->header;
        if ($current->next == null) {
            echo "链表为空!";
            return;
        }
        while ( $current->next != null ) {
            if ($current->id == $id) {
                break;
            }
            $current = $current->next;
        }
        return $current->name = $name;
    }
}

$lists = new singelLinkList ();
$lists->addLink ( new node ( 5, 'eeeeee' ) );
$lists->addLink ( new node ( 1, 'aaaaaa' ) );
$lists->addLink ( new node ( 6, 'ffffff' ) );
$lists->addLink ( new node ( 4, 'dddddd' ) );
$lists->addLink ( new node ( 3, 'cccccc' ) );
$lists->addLink ( new node ( 2, 'bbbbbb' ) );
$lists->getLinkList ();
echo "<br>-----------删除节点--------------<br>";
$lists->delLink ( 5 );
$lists->getLinkList ();

echo "<br>-----------更新节点名称--------------<br>";
$lists->updateLink ( 3, "222222" );
$lists->getLinkList ();

echo "<br>-----------获取节点名称--------------<br>";
echo $lists->getLinkNameById ( 5 );

echo "<br>-----------获取链表长度--------------<br>";
echo $lists->getLinkLength ();
?>

相关文章

  • Symfony控制层深入详解

    Symfony控制层深入详解

    这篇文章主要介绍了Symfony控制层,结合大量实例代码深入分析了Symfony控制器的常见使用技巧与相关注意事项,需要的朋友可以参考下
    2016-03-03
  • C#使用PHP服务端的Web Service通信实例

    C#使用PHP服务端的Web Service通信实例

    这篇文章主要介绍了C#使用PHP服务端的Web Service通信实例,需要的朋友可以参考下
    2014-04-04
  • ThinkPHP之N方法实例详解

    ThinkPHP之N方法实例详解

    ThinkPHP的N方法属于计数器方法,这篇文章主要介绍了ThinkPHP的N方法,需要的朋友可以参考下
    2014-06-06
  • ThinkPHP框架实现的邮箱激活功能示例

    ThinkPHP框架实现的邮箱激活功能示例

    这篇文章主要介绍了ThinkPHP框架实现的邮箱激活功能,结合实例形式分析了thinkPHP使用class.smtp.php及class.phpmailer.php类文件进行邮件发送实现激活功能的具体操作技巧,需要的朋友可以参考下
    2018-06-06
  • PHP中Closure类的使用方法及详解

    PHP中Closure类的使用方法及详解

    Closure类又被大家称之为匿名函数,在php5.3的时候引入的。顾名思义匿名函数就是没有定义名字的函数。本篇文章给大家介绍php中Closure类的使用及详解,需要的朋友可以参考下
    2015-10-10
  • Yii调试SQL的常用方法

    Yii调试SQL的常用方法

    这篇文章主要介绍了Yii调试SQL的常用方法,需要的朋友可以参考下
    2014-07-07
  • PHP laravel中的多对多关系实例详解

    PHP laravel中的多对多关系实例详解

    数据表之间是纵横交叉、相互关联的,laravel的一对一,一对多比较好理解,本文重点通过实例给大家讲解 laravel中的多对多关系,感兴趣的朋友一起看看吧
    2017-06-06
  • php递归函数三种实现方法及如何实现数字累加

    php递归函数三种实现方法及如何实现数字累加

    实现递归函数有哪些方法呢?如何用递归函数实现数字累加?这篇文章就主要介绍php递归函数三种实现方法及如何实现数字累加,需要的朋友可以参考下。
    2015-08-08
  • php简单的留言板与回复功能具体实现

    php简单的留言板与回复功能具体实现

    留言板是在刚接触php时用来学习的一个简单的应用例子了,今天我再给初学php的朋友提供一个完整的php留言板的全部制作过程,希望对你会有帮助
    2014-02-02
  • CentOS 上搭建 PHP7 开发测试环境

    CentOS 上搭建 PHP7 开发测试环境

    本文给大家分享的是作者在centos上搭建部署php7的开发测试环境的全部过程,非常的细致,有需要的小伙伴可以参考下
    2017-02-02

最新评论