学习PHP中,学习完语法,开始尝试实现数据结构,今天实现单链表

<?php
class node //节点的数据结构
{
public $id;
public $name;
public $next; public function __construct($id,$name) //构造函数
{
$this->id=$id;
$this->name=$name;
$this->next=null;
} } class linklist //链表的数据结构
{
private $header; public function __construct() //链表构造函数
{
$this->header=new node($id=null,$name=null);
} public function add($node) //向链表中添加节点的函数
{
$current=$this->header;
while($current->next!=null)
{
if($current->id>$node->id)
break;
else if($current->next==$node->id)
{
exit('already exist!');
}
$current=$current->next; }
$node->next=$current->next;
$current->next=$node;
} public function del($id) //在链表中删除一个节点
{
$current=$this->header;
$flag=false;
while($current->next!=null)
{
if($current->next->id==$id)
{
$flag=true;
break;
}
$current=$current->next;
}
if($flag)
$current->next=$current->next->next;
else
echo "can not find the node which id=".$id;
} public function getlength() //获取链表的长度
{
$current=$this->header;
$i=0;
while($current->next!=null)
{
$i++;
$current=$current->next;
}
return $i;
} public function getlist() //获取整个链表
{
$current=$this->header;
if($current->next==null)
{
echo "empty list"; //空表
return;
}
while($current->next!=null)
{
echo "id=".$current->next->id." , "."name=".$current->next->name."<br>";
if($current->next->next==null)
break;
$current=$current->next;
}
} public function update($id,$name) //更新表
{
$current=$this->header;
if($current->next==null)
exit("empty list");
while($current->next!=null)
{
if($current->id==$id)
break;
$current=$current->next; }
return $current->name=$name;
} } $list=new linklist(); //构造一个表
$list->add(new node(1,'aaa'));
$list->add(new node(2,'bbb'));
$list->add(new node(3,'ccc'));
$list->add(new node(4,'ddd'));
$list->add(new node(5,'eee'));
$list->add(new node(6,'fff'));
$list->add(new node(7,'ggg'));
$list->add(new node(8,'hhh'));
$list->add(new node(9,'iii')); $list->getlist();
echo"<br/> the length is ".$list->getlength()."<br/>";
echo"测试删除节点<br/>";
$list->del('8');
$list->getlist();
echo"测试更新节点<br/>";
$list->update('9','AAA');
$list->getlist(); ?>

  


输出结果为:
   
           id=1 , name=aaa
   id=2 , name=bbb
   id=3 , name=ccc
   id=4 , name=ddd
   id=5 , name=eee
   id=6 , name=fff
   id=7 , name=ggg
   id=8 , name=hhh
   id=9 , name=iii    the length is 9   
测试删除节点
    id=1 , name=aaa
    id=2 , name=bbb   
id=3 , name=ccc
   id=4 , name=ddd
   id=5 , name=eee
   id=6 , name=fff
   id=7 , name=ggg
   id=9 , name=iii
 测试更新节点
   id=1 , name=aaa
   id=2 , name=bbb
    id=3 , name=ccc
   id=4 , name=ddd
    id=5 , name=eee
   id=6 , name=fff
   id=7 , name=ggg
    id=9 , name=AAA

  


PHP数据结构之实现单链表的更多相关文章

  1. 数据结构——Java实现单链表

    一.分析 单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素.链表中的数据是以结点来表示的,每个结点由元素和指针构成.在Java中,我们可以将单链表定义成一个类,单链表的基 ...

  2. js数据结构与算法--单链表的实现与应用思考

    链表是动态的数据结构,它的每个元素由一个存储元素本身的节点和一个指向下一个元素的引用(也称指针或链接)组成. 现实中,有一些链表的例子. 第一个就是寻宝的游戏.你有一条线索,这条线索是指向寻找下一条线 ...

  3. C++ 数据结构学习二(单链表)

    模板类 //LinkList.h 单链表#ifndef LINK_LIST_HXX#define LINK_LIST_HXX#include <iostream>using namespa ...

  4. 数据结构:DHUOJ 单链表ADT模板应用算法设计:长整数加法运算(使用单链表存储计算结果)

    单链表ADT模板应用算法设计:长整数加法运算(使用单链表存储计算结果) 时间限制: 1S类别: DS:线性表->线性表应用 题目描述: 输入范例: -5345646757684654765867 ...

  5. 数据结构-多级指针单链表(C语言)

    偶尔看到大一时候写了一个多级链表,听起来好有趣,稍微整理一下. 稍微注意一下两点: 1.指针是一个地址,他自己也是有一个地址.一级指针(带一个*号)表示一级地址,他自身地址为二级地址.二级指针(带两个 ...

  6. TZOJ 数据结构实验:单链表元素插入

    描述 实现函数CreateHeader用于创建空链表,实现Insert函数并调用它完成带头节点链表的创建. 部分代码已经给出,请补充完整,提交时请勿包含已经给出的代码. void PrintLinkL ...

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

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

  8. 数据结构图文解析之:数组、单链表、双链表介绍及C++模板实现

    0. 数据结构图文解析系列 数据结构系列文章 数据结构图文解析之:数组.单链表.双链表介绍及C++模板实现 数据结构图文解析之:栈的简介及C++模板实现 数据结构图文解析之:队列详解与C++模板实现 ...

  9. 数据结构与算法分析——C语言描述 第三章的单链表

    数据结构与算法分析--C语言描述 第三章的单链表 很基础的东西.走一遍流程.有人说学编程最简单最笨的方法就是把书上的代码敲一遍.这个我是头文件是照抄的..c源文件自己实现. list.h typede ...

随机推荐

  1. Python之函数2 嵌套,作用域和闭包(Day12)

    一.函数对象 1.函数是第一类对象,即函数可以当做数据传递 1.1 可以被引用 1.2 可以当做参数传递 1.3 返回值可以是函数 1.4 可以当做容器类型的元素 二.函数的嵌套 1.函数嵌套的调用: ...

  2. Boostrap常用组件英文名

    dropdownlisttabsearchVertical TabSidebar with tabssidebarExpandable Panel ListFiltered Attendees Lis ...

  3. extern "C" 有关问题

    之前帮老板编译一个库的代码,遇到了一些问题,后来发现问题出现在extern "C"语法上. 1. C/C++语法extern 关键字 extern是C/C++语言中表明函数和全局变 ...

  4. Zuul

    一.zuul是什么 zuul 是netflix开源的一个API Gateway 服务器, 本质上是一个web servlet应用. Zuul 在云平台上提供动态路由,监控,弹性,安全等边缘服务的框架. ...

  5. hadoop01

    RPC:异步系统的调用,webservice是RPC的一种.webservice用于不在同一个公司的系统调用,同一个公司用socket调用.就是RPC. Dubbo淘宝的RPC框架. Hadoop r ...

  6. TIJ读书笔记03-初始化和构造器

      TIJ读书笔记03-初始化和构造器 初始化和清理是涉及安全的两个问题,如果对象不能正确的初始化会引起很多错误,比如空指针异常等,如果不能恰当及时的清理,会占用过多资源. 构造器在创建一个类的实例的 ...

  7. 嵌入式boa服务器移植

    开发板:EDUKIT-III实验箱,S3C2410+LINUX2.4,实验箱随箱光盘提供的Zimage,nor flash启动. 主机:ubnutn10.4LTS,arm-linux-gcc 2.95 ...

  8. iOS_AutoLayout自动布局

    目录: 一.什么是AutoLayout? 二.创建autoLayout的方法 三.VFL语言     一.什么是AutoLayout? Autolayout是一种“自动布局”技术,专门用来布局UI界面 ...

  9. Go sync模块

    // A WaitGroup waits for a collection of goroutines to finish.// The main goroutine calls Add to set ...

  10. flume 使用遇到问题及解决

    1. ../flume/fchannel/spool/data/ 目录下发生缓存文件积压 可能原因:同一时间同一客户端下向两个监控目录mv文件:或同一时间多个客户端向服务端上传文件 2.清空../fl ...