|
模拟链表:
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 '
';
$curr = $curr->next;
}
}
/**
* 添加新节点
*/
public static function add($head, $id, $name) {
$curr = $head;
$obj = new linkList($id, $name);
while ($curr->next != null) {
// 如果当前ID next->id > $id) {
$obj->next = $curr->next;
$curr->next = $obj;
return true;
} else if ($curr->next->id == $id) {
echo '当前Id:'.$id.'重复了,请不要继续添加了!';
echo '
';
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);
?>
链表的增删查改:
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 '
';
$curr = $curr->next;
}
}
/**
* 添加新节点
*/
public static function add($head, $id, $name) {
$curr = $head;
$obj = new linkList($id, $name);
while ($curr->next != null) {
// 如果当前ID next->id > $id) {
$obj->next = $curr->next;
$curr->next = $obj;
return true;
} else if ($curr->next->id == $id) {
echo '当前Id:'.$id.'重复了,请不要继续添加了!';
echo '
';
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小教程之实现双向链表PHP 双链表(SplDoublyLinkedList)简介和使用实例php链表用法实例分析PHP实现的基于单向链表解决约瑟夫环问题示例php 数据结构之链表队列PHP实现双链表删除与插入节点的方法示例PHP实现单链表翻转操作示例PHP基于双向链表与排序操作实现的会员排名功能示例PHP实现找出链表中环的入口节点PHP获取链表中倒数第K个节点的方法
|
|