PHP模拟单链表的数据结构

<?php

/***
 * 单链表
 */

//节点,下标,节点名称,下一个节点的地址
class Node
{
    public $id;
    public $name;
    public $next;

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

class SingleLinkList
{
    private $header;
    public function __construct($id=null,$name=null)
    {
        $this->header=new Node($id,$name);
    }

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

    /**
     * 添加节点到单链表中(原理就是插入当前的节点的位置存储的下一个节点的信息,上一个节点存储当前节点的信息)
     * @param $node 节点
     */
    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;
    }

    /**
     * 删除节点(删除的节点的下一个位置保持在上一个节点中)
     * @param $id
     */
    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.'的信息';
        }
    }

    /**
     * 判断单链表是否为空
     * @return bool
     */
    public function isEmpty(){
        return $this->header == null;
    }

    /**
     * 清空单链表
     */
    public function clear(){
        $this->header=null;
    }

    /**
     * 获取链表的信息
     */
    public function getLinkList(){
        $current = $this->header;
        if($current->next==null){
            echo '链表为空';
            return;
        }
        while($current->next != null){
            echo 'id:'.$current->next->id.',节点名称为'.$current->next->name.'<br/>';
            if($current->next->next == null){
                break;
            }
            $current=$current->next;
        }
    }

    /**
     * 获取某个节点信息
     * @param $id
     * @return string
     */
    public function getLinkNameById($id){
        $current = $this->header;
        if($current->next==null){
            return '';
        }

        while($current->next != null){
            if($current->id == $id){
                return $current->name;
            }
            $current=$current->next;
        }
    }

    /**
     * 更新某个节点名称
     * @param $id
     * @param $name
     */
    public function updateLink($id,$name){
        $current = $this->header;
        if($current->next==null){
            return '';
        }

        while($current->next != null){
            if($current->id == $id){
                return $current->name=$name;
            }
            $current=$current->next;
        }
    }
}

header('Content-Type:text/html;charset=utf8');
$lists = new SingleLinkList();

$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 $lists->getLinkNameById ( 4 );
echo "<br>-----------获取链表长度--------------<br>";
echo $lists->getLinkLength ();

 

posted @ 2019-02-15 10:24  前度天下  阅读(218)  评论(0编辑  收藏  举报