为了账号安全,请及时绑定邮箱和手机立即绑定

php 双向链表

标签:
PHP

<?php     

class student
{
    public $name;                    //姓名
    public $id;                      
    public $gender;                  // 性别
    public $chinese;                               
    public $math;                                  
    public $english;                            
    public $computer;                            
    public $prev = null;            //前节点
    public $next = null;            //后节点
 
    function __construct($name="",$id="",$gender="",$chinese="",$math="",$english="",$computer="") {
        $this->name = $name;
        $this->id = $id;
        $this->gender = $gender;                          
        $this->chinese = $chinese;
        $this->math = $math;
        $this->english = $english;
        $this->computer = $computer;
    }
     
    public static function findStudent($head,$student,$type='id')
    {
        $temp = $head;
        $isFind = false;
        while (!is_null($temp)) {
            if ($temp->$type == $student->$type) {
                $isFind = true;
                break;
            }
            $temp=$temp->next;
        }
        if ($isFind) {
             echo "\n找到:学生姓名:".$temp->name."  学号:".$temp->id."  性别:".$temp->gender."  语文:".$temp->chinese."  数学:".$temp->math."  英语:".$temp->english."  计算机:".$temp->computer."";
        }else {
            echo "没有找到该学生信息";
        }
    }
     
 
    /**
     * 添加学生信息
     *
     * @param unknown_type $head
     * @param unknown_type $student
     */
    public static function insertStudent($head,$student)
    {
        $temp = $head;
        if(is_null($temp->next)){
            $temp->next = $student;
            $student->prev = $temp;
        }else{
            $isAdd = true;
            while ( !is_null($temp->next)) {
                if ($temp->next->id > $student->id) {
                    break;
                }elseif ($temp->next->id == $student->id)
                {
                    echo "学号相同";
                    $isAdd = false;
                }
                $temp = $temp->next;
            }
            if ($isAdd) {
                $student->next = $temp->next;
                $student->prev = $temp;
                //$temp->next->prev = $student;
                $temp->next = $student;
            }
        }
    }
 
    /**
     * 删除学生信息
     *
     * @param unknown_type $head
     * @param unknown_type $student
     */
    public static  function delStudent($head,$student)
    {
        $temp = $head->next;
        $isFind = false;
        while (!is_null($temp)) {
            if ($temp->id == $student->id) {
                $isFind = true;
                break;
            }
            $temp=$temp->next;
        }
        if ($isFind) {
            if (!is_null($temp->next)) {
                $temp->next->prev=$temp->prev;
            }
            $temp->prev->next=$temp->next;
 
            echo "删除的学生为".$temp->name."  学号:".$temp->id."  性别:".$temp->gender."  语文:".$temp->chinese."  数学:".$temp->math."  英语:".$temp->english."  计算机:".$temp->computer."";
        }else {
            echo "没有找到该学生信息";
        }
    }
 
    /**
     * 展示全部学生信息
     *
     * @param unknown_type $head
     */
    public static function showAllStudent($head)
    {
        $temp = $head->next;
        while (!is_null($temp)) {
            echo "\n学生姓名:".$temp->name."  学号:".$temp->id."  性别:".$temp->gender."  语文:".$temp->chinese."  数学:".$temp->math."  英语:".$temp->english."  计算机:".$temp->computer."";
            $temp=$temp->next;
        }
    }
}
 
$head = new student();
$student1 = new student("张永伟","98","男","100","100","100","100");
$student2 = new student("魏海龙","99","男","100","100","100","100");
$student3 = new student("宋老师","100","男","100","100","100","100");
$student4 = new student("杨老师","97","男","100","100","100","100");
$student5 = new student("李杰","96","男","100","100","100","100");
 
 
student::insertStudent($head,$student1);
student::insertStudent($head,$student2);
student::insertStudent($head,$student3);
student::insertStudent($head,$student4);
student::insertStudent($head,$student5);
echo "\n删除宋老师:\n";
student::delStudent($head,$student3);
echo "\n全部学生信息遍历:";
student::showAllStudent($head);
 
echo "\n查找学生张永伟:";
student::findStudent($head,$student1);

?>

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消