学习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. requirejs源码分析: define 方法

    define = function (name, deps, callback) {     var node, context; //Allow for anonymous modules     ...

  2. POJ 3468 A Simple Problem with Integers 【线段树】

    题目链接 http://poj.org/problem?id=3468 思路 线段树 区间更新 模板题 在赋初始值的时候,按点更新区间就可以 AC代码 #include <cstdio> ...

  3. Nginx负载均衡案例

    nginx负载均衡配置,windows版本和linux版本的nginx除了启动方式其他基本无差异. 1.Niginx安装 参考:https://www.cnblogs.com/zwcry/p/9454 ...

  4. css float 浮动是个混球

    得说,在学习 CSS 的时候就一直在疑惑,横排布局干嘛要用 float 这种属性, 用了还会高度塌陷,怎么不发明些高级点的属性来完成横排布局呢, 甚至所有人都告诉我摒弃表格布局,浮动布局才是 DIV+ ...

  5. 【Head First Servlets and JSP】笔记10:请求分派(RequestDispatcher)

    1.让其它组件接管全部请求. package com.example.web; import com.example.model.BeerExpert; import javax.servlet.*; ...

  6. avaweb学习总结(八)——HttpServletResponse对象(二)

    一.HttpServletResponse常见应用——生成验证码 1.1.生成随机图片用作验证码 生成图片主要用到了一个BufferedImage类,

  7. keepalived检测脚本及注意事项

    keepalived检测脚本的作用及注意事项: 默认每隔3秒钟执行一次检测脚本,检查nginx服务是否启动,如果没启动就把nginx服务启动起来,如果启动不成功,就把keepalived服务down掉 ...

  8. MYSQL数据库字段命名及设计规范

    1.设计原则 1) 标准化和规范化数据的标准化有助于消除数据库中的数据冗余.标准化有好几种形式,但 Third Normal Form(3NF)通常被认为在性能.扩展性和数据完整性方面达到了最好平衡. ...

  9. numpy 往array里添加一个元素

    首先这里p_arr为一个numpy的array,p_为一个元素 p_arr = np.concatenate((p_arr,[p_])) # 先将p_变成list形式进行拼接,注意输入为一个tuple ...

  10. 命令行编译vs10项目工程

    参考网址: http://www.oschina.net/question/234345_42135 1. 1.1.使用的命令行为:开始-->所有程序--> vs2020 --> V ...