学习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. JavaScript实现自适应窗口大小的网页

    <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content ...

  2. STM32 ~ CH340在STM32实现一键下载电路

    在做基于STM32的多功能MP3播放器的课题时,在程序下载这部分时借鉴了正点原子开发板上的一键下载电路,采用CH340G这款芯片设计. 在画PCB初期原理图部分,对采用CH340G设计的一键下载电路不 ...

  3. pdoModel封装

    <?php /** * Created by PhpStorm. * User: Administrator * Date: 2017/7/24 * Time: 14:03 */ /** * 数 ...

  4. 【leetcode刷题笔记】Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  5. MongoDB命令语法小用

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using MongoDB; ...

  6. Object.defineProperty小解

    最早认识这个函数,源于对vue双向绑定的探索,vue通过这个函数实现属性挟持并结合发布者-订阅者模式实现双向绑定 先看一个实例: var o= {name: 'a'} Object.definePro ...

  7. Vue.js学习笔记 第五篇 事件处理

    监听事件 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <titl ...

  8. 四月兄弟AprilBeacon

    硬件相关ibeacon https://www.aprbrother.com/

  9. JMeter学习(八)JDBC Request

    [step_1]:“测试计划”--(右键)à添加à线程组: [step_2]:选择step_1中添加的线程组—(右键)à添加à配置元件àJDBC Connection Configuration,添加 ...

  10. CocoaPods安装使用

    $ gem sources --remove https://rubygems.org/ //等有反应之后再敲入以下命令 $ gem sources -a http://ruby.taobao.org ...