码迷,mamicode.com
首页 > 其他好文 > 详细

链表 最灵活的数据结构

时间:2015-05-15 22:47:03      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

链表是一个有序的列表,但是他在内存中时分散存储的,使用链表可以解决类似约瑟夫问题,排序,索引,广义表
<?php

/**
 * Created by PhpStorm.
 * User: qixingyi
 * Date: 2015/5/15
 * Time: 19:20
 * 链表 最灵活的数据结构
 *
 *
 * 链表是一个有序的列表,但是他在内存中时分散存储的,使用链表可以解决类似约瑟夫问题,排序,索引,广义表
 */

/**
 * Class Hero  水浒英雄排行榜管理
 */
class Hero
{
    public $no; //英雄编号
    public $name;
    public $nickname;
    public $next = null;

    public function __construct($no = ‘‘, $name = ‘‘, $nickname = ‘‘)
    {
        $this->no = $no;
        $this->name = $name;
        $this->nickname = $nickname;
    }
}

$head = new Hero();

/**
 * @param $head 好汉
 * @param $hero 英雄
 */
function addHero($head, $hero)
{
    $cur = $head;

    $i=0;
    $flag = false;

    while ($cur->next != null) {
        if ($cur->next->no > $hero->no) {
            break;
        } else if ($cur->next->no == $hero->no) {
            $flag = true;

            echo ‘<br/><br/><b style="color: red">不能抢位置‘ . $hero->no . ‘</b><br/>位置已经有人了<br/>‘;
        }
        $cur=$cur->next;
    }

    if($flag==false){
        $hero->next=$cur->next;
        $cur->next=$hero;
    }
}

/**
 * @param $head 好汉
 */
function showHeros($head){
    $cur=$head;

    while($cur->next!=null){
        echo $cur->next->no.‘ 号英雄: ‘.$cur->next->name.‘ 的外号是: ‘.$cur->next->nickname.‘<br><br>‘;

        $cur=$cur->next;
    }
}

/**
 * @param $head 好汉
 * @param $hero 英雄
 */
function updateHero($head,$hero){
    $cur=$head;
    while($cur->next!=null){
        if($cur->next->no==$hero->no){
            //已找到
            break;
        }
        $cur=$cur->next;
    }

    if($cur->next==null){//还是没找到
        echo ‘<br><br><b style="color: red">XX 【‘.$hero->hero->no.‘】 英雄不存在~</b><br><br>‘;
    }


    //ID不能改
    $cur->next->name=$hero->name;
    $cur->next->nickname=$hero->nickname;

    echo ‘<br><br><b style="color: green">VV修改 【‘.$cur->next->no.‘】 号英雄资料成功</b><br><br>‘;

}


function delHero($head,$hero_no){
    $cur=$head;
    $flag=false;
    while($cur->next!=null){
        if($cur->next->no==$hero_no){
            //已找到
            $flag=true;
            break;
        }

        $cur=$cur->next;
    }

    if($flag){
        $cur->next=$cur->next->next;
        echo ‘<br><br><b style="color: green">VV已删除 【‘.$hero_no.‘】 号英雄</b><br><br>‘;
    }else{
        echo ‘<br><br><b style="color: red">XX 【‘.$hero_no.‘】 英雄不存在~</b><br><br>‘;
    }

}
$hero=new Hero(1,‘宋江‘,‘及时雨‘);
addHero($head,$hero);
$hero=new Hero(4,‘吴用‘,‘智多星‘);
addHero($head,$hero);
$hero=new Hero(2,‘武松‘,‘打虎英雄‘);
addHero($head,$hero);
$hero=new Hero(3,‘秦明‘,‘霹雳火‘);
addHero($head,$hero);
$hero=new Hero(9,‘林冲‘,‘豹子头‘);
addHero($head,$hero);


echo ‘<br/><br/>*******************当前英雄的排行榜是*******************<br/><br/>‘;
showHeros($head);

echo ‘<br/><br/>*******************修改后的英雄的排行榜是*******************<br/><br/>‘;
$hero=new Hero(2,‘林冲的替身‘,‘狮子尾‘);
updateHero($head,$hero);
showHeros($head);

echo ‘<br/><br/>*******************删除后的英雄的排行榜是*******************<br/><br/>‘;
delHero($head,4);
showHeros($head);

  

 技术分享

链表 最灵活的数据结构

标签:

原文地址:http://www.cnblogs.com/singee77/p/4506867.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!