发布于 2016-03-16 06:46:38 | 118 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的PHP设计模式,程序狗速度看过来!

PHP开源脚本语言

PHP(外文名: Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通用开源脚本语言。语法吸收了C语言、Java和Perl的特点,入门门槛较低,易于学习,使用广泛,主要适用于Web开发领域。PHP的文件后缀名为php。


这篇文章主要介绍了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($head, 1, 'wangdk');
linkList::add($head, 2, 'sunshuzhen');
linkList::add($head, 8, 'wanghaha');
linkList::add($head, 6, 'wangchufen');
linkList::add($head, 6, 'wangchufen');
linkList::add($head, 3, 'wangdaye');
 
linkList::del($head, 1);
linkList::edit($head, 2, '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($head, 1, 'wangdk');
linkList::add($head, 2, 'sunshuzhen');
linkList::add($head, 8, 'wanghaha');
linkList::add($head, 6, 'wangchufen');
linkList::add($head, 6, 'wangchufen');
linkList::add($head, 3, 'wangdaye');
 
linkList::del($head, 1);
linkList::edit($head, 2, 'hahaha');
linkList::echo_link_list($head);
 
?>



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务