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单链表实现的更多相关文章

  1. 时间复杂度分别为 O(n)和 O(1)的删除单链表结点的方法

    有一个单链表,提供了头指针和一个结点指针,设计一个函数,在 O(1)时间内删除该结点指针指向的结点. 众所周知,链表无法随机存储,只能从头到尾去遍历整个链表,遇到目标节点之后删除之,这是最常规的思路和 ...

  2. 单链表的C++实现(采用模板类)

    采用模板类实现的好处是,不用拘泥于特定的数据类型.就像活字印刷术,制定好模板,就可以批量印刷,比手抄要强多少倍! 此处不具体介绍泛型编程,还是着重叙述链表的定义和相关操作.  链表结构定义 定义单链表 ...

  3. Java实现单链表的各种操作

    Java实现单链表的各种操作 主要内容:1.单链表的基本操作 2.删除重复数据 3.找到倒数第k个元素   4.实现链表的反转   5.从尾到头输出链表 6.找到中间节点 7.检测链表是否有环 8.在 ...

  4. [LeetCode] Linked List Cycle II 单链表中的环之二

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  5. c++单链表基本功能

    head_LinkNode.h /*单链表类的头文件*/#include<assert.h>#include"compare.h"typedef int status; ...

  6. 单链表、循环链表的JS实现

    数据结构系列前言: 数据结构作为程序员的基本知识,需要我们每个人牢牢掌握.近期我也展开了对数据结构的二次学习,来弥补当年挖的坑......   当时上课的时候也就是跟着听课,没有亲自实现任何一种数据结 ...

  7. C代码实现非循环单链表

    C代码实现非循环单链表, 直接上代码. # include <stdio.h> # include <stdlib.h> # include <malloc.h> ...

  8. 分离的思想结合单链表实现级联组件:CascadeView

    本文介绍自己最近做省市级联的类似的级联功能的实现思路,为了尽可能地做到职责分离跟表现与行为分离,这个功能拆分成了2个组件并用到了单链表来实现关键的级联逻辑,下一段有演示效果的gif图.虽然这是个很常见 ...

  9. 数据结构:单链表结构字符串(python版)添加了三个新功能

    #!/urs/bin/env python # -*- coding:utf-8 -*- #异常类 class stringTypeError(TypeError): pass #节点类 class ...

  10. 数据结构:单链表结构字符串(python版)改进

    此篇文章的replace实现了字符串类的多次匹配,但依然有些不足. 因为python字符串对象为不变对象,所以replace方法并不修改原先的字符串,而是返回修改后的字符串. 而此字符串对象时用单链表 ...

随机推荐

  1. Remove Duplicates from Sorted List ,除去链表中相邻的重复元素

    Remove Duplicates from Sorted List : Given a sorted linked list, delete all duplicates such that eac ...

  2. No toolchains found in the NDK toolchains folder for ABI with prefix

    通过Android Studio 的Sdk Manager安装NDK,安装完之后编译失败,报错信息如下: No toolchains found in the NDK toolchains folde ...

  3. 使用ZooKeeper实现Java跨JVM的分布式锁

    一.使用ZooKeeper实现Java跨JVM的分布式锁 二.使用ZooKeeper实现Java跨JVM的分布式锁(优化构思) 三.使用ZooKeeper实现Java跨JVM的分布式锁(读写锁) 说明 ...

  4. 【sparkStreaming】kafka作为数据源的生产和消费

    1.建立生产者发送数据 (1)配置zookeeper属性信息props (2)通过 new KafkaProducer[KeyType,ValueType](props) 建立producer (3) ...

  5. 【Hive】自定义函数

    Hive的自定义函数无法满足实际业务的需要,所以为了扩展性,Hive官方提供了自定义函数来实现需要的业务场景. 1.定义 (1)udf(user defined function):  自定义函数,特 ...

  6. WPF控件开源资源

    (转)WPF控件开源资源 Textbox Drag/Drop in WPFhttp://www.codeproject.com/Articles/42696/Textbox-Drag-Drop-in- ...

  7. 【python】没有root权限的时候安装Python package

    下载相关位置的包 1.首先在git上下载对应的包: 搜索package github,找到地址.使用 git clone https://xxx.git 命令 2.使用python setup.py ...

  8. 一个css3 旋转效果 -- 待续

    <div class="container"> <div> <figure></figure> <figure>< ...

  9. 激活函数之ReLU/softplus介绍及C++实现

    softplus函数(softplus function):ζ(x)=ln(1+exp(x)). softplus函数可以用来产生正态分布的β和σ参数,因为它的范围是(0,∞).当处理包含sigmoi ...

  10. Win7系统64位环境下使用Apache——Apache2.4整合Tomcat与mod_jk

    转载请注明出处:http://blog.csdn.net/dongdong9223/article/details/70398091 本文出自[我是干勾鱼的博客] 之前的几篇文章: Win7系统64位 ...