php单链表实现
php单链表实现
<?php
//单链表
class Hero{
public $no;
public $name;
public $nickname;
public $next=null; function __construct($no='',$name=''){
$this->no=$no;
$this->name=$name;
} }
function addHero($head,$Hero){
$cur=$head;
$flag=true;
while ($cur->next!=null) {
if($cur->next->no>$Hero->no){
$tmp=$cur->next;
$cur->next=$Hero;
$Hero->next=$tmp;
$flag=false;break;
}else if($cur->next->no==$Hero->no){echo "该位置已有人,不允许占位";$flag=false;break;}
else{ $cur=$cur->next;}
}
if($flag){$cur->next=$Hero;}
}
//增加
function showHero($head){
$cur=$head;
while($cur->next!=null){ echo $cur->next->no.":".$cur->next->name."<br/>";
$cur=$cur->next;
} }
//删除特定编号的
function delHero($head,$no){
$cur=$head;
while($cur->next!=null){
if($cur->next->no==$no)
{if($cur->next->next)$cur->next=$cur->next->next;
else $cur->next=null;
break;
}
$cur=$cur->next;
} }
//查找特定编号的信息
function findHero($head,$no){
$cur=$head;
while($cur->next!=null){
if($cur->next->no==$no){break;}
$cur=$cur->next;
}
echo$cur->next->name; }
//改特定编号的信息
function updateHero($head,$no,$name){
$cur=$head;
while($cur->next!=null){
if($cur->next->no==$no){break;}
$cur=$cur->next;
}
$cur->next->name=$name;
}
$head=new Hero();
$Hero=new Hero(1,"宋江");
addHero($head,$Hero);
$Hero=new Hero(6,"林冲");
addHero($head,$Hero);
$Hero=new Hero(2,"吴用");
addHero($head,$Hero);
$Hero=new Hero(4,"李逵");
addHero($head,$Hero);
showHero($head);
//删除4号
delHero($Hero,4);
//查找6号
findHero($head,6);
// 修改6号
updateHero($Hero,6,"林哥哥");
showHero($head); ?>
php单链表实现的更多相关文章
- 时间复杂度分别为 O(n)和 O(1)的删除单链表结点的方法
有一个单链表,提供了头指针和一个结点指针,设计一个函数,在 O(1)时间内删除该结点指针指向的结点. 众所周知,链表无法随机存储,只能从头到尾去遍历整个链表,遇到目标节点之后删除之,这是最常规的思路和 ...
- 单链表的C++实现(采用模板类)
采用模板类实现的好处是,不用拘泥于特定的数据类型.就像活字印刷术,制定好模板,就可以批量印刷,比手抄要强多少倍! 此处不具体介绍泛型编程,还是着重叙述链表的定义和相关操作. 链表结构定义 定义单链表 ...
- Java实现单链表的各种操作
Java实现单链表的各种操作 主要内容:1.单链表的基本操作 2.删除重复数据 3.找到倒数第k个元素 4.实现链表的反转 5.从尾到头输出链表 6.找到中间节点 7.检测链表是否有环 8.在 ...
- [LeetCode] Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- c++单链表基本功能
head_LinkNode.h /*单链表类的头文件*/#include<assert.h>#include"compare.h"typedef int status; ...
- 单链表、循环链表的JS实现
数据结构系列前言: 数据结构作为程序员的基本知识,需要我们每个人牢牢掌握.近期我也展开了对数据结构的二次学习,来弥补当年挖的坑...... 当时上课的时候也就是跟着听课,没有亲自实现任何一种数据结 ...
- C代码实现非循环单链表
C代码实现非循环单链表, 直接上代码. # include <stdio.h> # include <stdlib.h> # include <malloc.h> ...
- 分离的思想结合单链表实现级联组件:CascadeView
本文介绍自己最近做省市级联的类似的级联功能的实现思路,为了尽可能地做到职责分离跟表现与行为分离,这个功能拆分成了2个组件并用到了单链表来实现关键的级联逻辑,下一段有演示效果的gif图.虽然这是个很常见 ...
- 数据结构:单链表结构字符串(python版)添加了三个新功能
#!/urs/bin/env python # -*- coding:utf-8 -*- #异常类 class stringTypeError(TypeError): pass #节点类 class ...
- 数据结构:单链表结构字符串(python版)改进
此篇文章的replace实现了字符串类的多次匹配,但依然有些不足. 因为python字符串对象为不变对象,所以replace方法并不修改原先的字符串,而是返回修改后的字符串. 而此字符串对象时用单链表 ...
随机推荐
- Maven添加Oracle驱动及依赖
oracle驱动先去官网下载,下载下来后,需要安装到maven本地仓库,然后再pom中添加依赖. 1下载oracle驱动包 ojdbc6-11.2.0.3.jar 2命令行安装到maven仓库 mvn ...
- mybatis引入dtd约束
window->preferences,然后寻找xml catalog,点击add如下所示 将dtd网址复制到key中 key type选择uri,选择dtd的下载路径.
- D3.js学习笔记(三)——创建基于数据的SVG元素
目标 在这一章,你将会使用D3.js,基于我们的数据来把SVG元素添加到网页中.这一过程包括:把数据绑定到元素上,然后在使用这些元素来可视化我们的数据. 注意:不同于前几章,我们从一个完整的代码开始, ...
- 火车头采集器db3导出sql语句
1.通过火狐 sqlite mananger工具,将.db3文件,导出为.sql文件2.右击表面content,属性:Export table 3.不要勾选 include create table. ...
- 【Demo】CSS3 3D转换
3D转换transform rotateX() 方法 rotateX()方法,围绕其在一个给定度数X轴旋转的元素. div { transform: rotateX(120deg); -webkit- ...
- Ceph配置项动态变更机制浅析
转自:https://www.ustack.com/blog/ceph%e9%85%8d%e7%bd%ae%e9%a1%b9%e5%8a%a8%e6%80%81%e5%8f%98%e6%9b%b4%e ...
- Redis 命令,键(key),字符串(String),哈希(Hash),列表(List),集合(Set)(二)
Redis 命令 Redis 命令用于在 redis 服务上执行操作. 要在 redis 服务上执行命令需要一个 redis 客户端.Redis 客户端在我们之前下载的的 redis 的安装包中. ...
- eclipse配置tomcat运行项目访问不加项目名
- LeetCode OJ:Integer to Roman(转换整数到罗马字符)
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- Java基础学习-代码块
/*代码块: * 用{}修饰的代码 * 1.局部代码块:控制变量,存在方法中,控制变量的生命周期(作用域) * 2.构造代码块:提取构造方法中的共性,每次创建对象,都会执行,并且在构造方法执行之前执行 ...