温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

PHP中怎么模拟链表

发布时间:2021-06-23 16:46:38 阅读:162 作者:Leah 栏目:开发技术
PHP开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

这篇文章给大家介绍PHP中怎么模拟链表,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

模拟链表:

<?php 
/**
 * PHP实现链表的基本操作
 */
class linkList {
  /**
   * 姓名
   * @var string
   */
  public $name '';
   
  /**
   * 编号
   * @var int
   */
  public $id 0;
   
  /*
   * 引用下一个对象
   */
  public $next null;
   
  /**
   * 构造函数初始化数据
   * @param int $id
   * @param string $name
   */
  public function __construct($id 0$name '') {
    $this->name = $name;
    $this->id  = $id;
  }
   
  /**
   * 遍历链表
   */
  public static function echo_link_list($head) {
    $curr $head;
    while ($curr->next != null) {
      echo '姓名:'.$curr->next->name, ' 编号:'.$curr->next->id;
      echo '<br>';
      $curr $curr->next;
    }
  }
   
  /**
   * 添加新节点
   */
  public static function add($head$id$name) {
    $curr $head;
    $obj new linkList($id$name);
     
    while ($curr->next != null) {
      // 如果当前ID < 下一个ID,则添加到中间,添加节点到指定顺序位置
      if ($curr->next->id > $id) {
         
        $obj->next = $curr->next;
        $curr->next = $obj;
         
        return true;
      } else if ($curr->next->id == $id) {
         
        echo '当前Id:'.$id.'重复了,请不要继续添加了!';
        echo '<br>';
         
        return false;
      }
      $curr $curr->next;
    }
    // 添加节点到尾部
    if ($curr->next == null) {
      $curr->next = $obj;
    }
  }
   
  /**
   * 删除节点
   */
  public static function del($head$id) {
    $curr $head;
     
    while($curr->next != null) {
      if ($curr->next->id == $id) {
        $curr->next = $curr->next->next;
        return true;
      }
      $curr $curr->next;
    }
  }
   
  /**
   * 修改节点
   */
  public static function edit($head$id$new_name) {
    $curr $head;
     
    while($curr->next != null) {
      if ($curr->next->id == $id) {
        $curr->next->name = $new_name;
      }
      $curr $curr->next;
    }
  }
}
 
$head new linkList();
linkList::add($head1'wangdk');
linkList::add($head2'sunshuzhen');
linkList::add($head8'wanghaha');
linkList::add($head6'wangchufen');
linkList::add($head6'wangchufen');
linkList::add($head3'wangdaye');
 
linkList::del($head1);
linkList::edit($head2'hahaha');
linkList::echo_link_list($head);
 
?>

链表的增删查改:

<?php 
/**
 * PHP实现链表的基本操作
 */
class linkList {
  /**
   * 姓名
   * @var string
   */
  public $name '';
   
  /**
   * 编号
   * @var int
   */
  public $id 0;
   
  /*
   * 引用下一个对象
   */
  public $next null;
   
  /**
   * 构造函数初始化数据
   * @param int $id
   * @param string $name
   */
  public function __construct($id 0$name '') {
    $this->name = $name;
    $this->id  = $id;
  }
   
  /**
   * 遍历链表
   */
  public static function echo_link_list($head) {
    $curr $head;
    while ($curr->next != null) {
      echo '姓名:'.$curr->next->name, ' 编号:'.$curr->next->id;
      echo '<br>';
      $curr $curr->next;
    }
  }
   
  /**
   * 添加新节点
   */
  public static function add($head$id$name) {
    $curr $head;
    $obj new linkList($id$name);
     
    while ($curr->next != null) {
      // 如果当前ID < 下一个ID,则添加到中间,添加节点到指定顺序位置
      if ($curr->next->id > $id) {
         
        $obj->next = $curr->next;
        $curr->next = $obj;
         
        return true;
      } else if ($curr->next->id == $id) {
         
        echo '当前Id:'.$id.'重复了,请不要继续添加了!';
        echo '<br>';
         
        return false;
      }
      $curr $curr->next;
    }
    // 添加节点到尾部
    if ($curr->next == null) {
      $curr->next = $obj;
    }
  }
   
  /**
   * 删除节点
   */
  public static function del($head$id) {
    $curr $head;
     
    while($curr->next != null) {
      if ($curr->next->id == $id) {
        $curr->next = $curr->next->next;
        return true;
      }
      $curr $curr->next;
    }
  }
   
  /**
   * 修改节点
   */
  public static function edit($head$id$new_name) {
    $curr $head;
     
    while($curr->next != null) {
      if ($curr->next->id == $id) {
        $curr->next->name = $new_name;
      }
      $curr $curr->next;
    }
  }
}
 
$head new linkList();
linkList::add($head1'wangdk');
linkList::add($head2'sunshuzhen');
linkList::add($head8'wanghaha');
linkList::add($head6'wangchufen');
linkList::add($head6'wangchufen');
linkList::add($head3'wangdaye');
 
linkList::del($head1);
linkList::edit($head2'hahaha');
linkList::echo_link_list($head);
 
?>

关于PHP中怎么模拟链表就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php
AI

开发者交流群×