PHP数据结构之实现单链表
学习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数据结构之实现单链表的更多相关文章
- 数据结构——Java实现单链表
一.分析 单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素.链表中的数据是以结点来表示的,每个结点由元素和指针构成.在Java中,我们可以将单链表定义成一个类,单链表的基 ...
- js数据结构与算法--单链表的实现与应用思考
链表是动态的数据结构,它的每个元素由一个存储元素本身的节点和一个指向下一个元素的引用(也称指针或链接)组成. 现实中,有一些链表的例子. 第一个就是寻宝的游戏.你有一条线索,这条线索是指向寻找下一条线 ...
- C++ 数据结构学习二(单链表)
模板类 //LinkList.h 单链表#ifndef LINK_LIST_HXX#define LINK_LIST_HXX#include <iostream>using namespa ...
- 数据结构:DHUOJ 单链表ADT模板应用算法设计:长整数加法运算(使用单链表存储计算结果)
单链表ADT模板应用算法设计:长整数加法运算(使用单链表存储计算结果) 时间限制: 1S类别: DS:线性表->线性表应用 题目描述: 输入范例: -5345646757684654765867 ...
- 数据结构-多级指针单链表(C语言)
偶尔看到大一时候写了一个多级链表,听起来好有趣,稍微整理一下. 稍微注意一下两点: 1.指针是一个地址,他自己也是有一个地址.一级指针(带一个*号)表示一级地址,他自身地址为二级地址.二级指针(带两个 ...
- TZOJ 数据结构实验:单链表元素插入
描述 实现函数CreateHeader用于创建空链表,实现Insert函数并调用它完成带头节点链表的创建. 部分代码已经给出,请补充完整,提交时请勿包含已经给出的代码. void PrintLinkL ...
- 分离的思想结合单链表实现级联组件:CascadeView
本文介绍自己最近做省市级联的类似的级联功能的实现思路,为了尽可能地做到职责分离跟表现与行为分离,这个功能拆分成了2个组件并用到了单链表来实现关键的级联逻辑,下一段有演示效果的gif图.虽然这是个很常见 ...
- 数据结构图文解析之:数组、单链表、双链表介绍及C++模板实现
0. 数据结构图文解析系列 数据结构系列文章 数据结构图文解析之:数组.单链表.双链表介绍及C++模板实现 数据结构图文解析之:栈的简介及C++模板实现 数据结构图文解析之:队列详解与C++模板实现 ...
- 数据结构与算法分析——C语言描述 第三章的单链表
数据结构与算法分析--C语言描述 第三章的单链表 很基础的东西.走一遍流程.有人说学编程最简单最笨的方法就是把书上的代码敲一遍.这个我是头文件是照抄的..c源文件自己实现. list.h typede ...
随机推荐
- notepad++运行python代码
notepad++运行代码 在菜单栏中点击运行或按F5,在弹出的对话框中输入下面命令 cmd /k E:\py3.6\python.exe "$(FULL_CURRENT_PATH)&quo ...
- centos 6.5 设置屏幕保护
设置屏幕保护:System -> Preferences -> Screensaver.如果需要取消屏幕保护的锁定功能,将Lock screen when screensaver is a ...
- Linux系统资源查询命令(cpu、io、mem)
cpu/mem: 1. 指定pid top -p pid1,pid2,... 2. top排序 先top,然后 输入大写P,则结果按CPU占用降序排序.输入大写M,结果按内存占用降序排序. io: ...
- UI控件之UIPickerView的协议方法
UIPickerView:选择视图,父类是UIView UIPickerView *pickerView=[[UIPickerView alloc]initWithFrame:CGRectMake(1 ...
- IM协议
四种协议英文全称与简称 1->IMPP(Instant Messaging And PresenceProtocol):即时信息和空间协议 2->PRIM(Presence and Ins ...
- UVA11297 Census
题目 UVA11297 Census 做法 二维线段树,单点修改,矩阵查询,树套树(\(x,y\)),维护最大值最小值废话 有一点要注意的是:\(x\)树传到\(y\)树里面修改的时候,如果\(x\) ...
- 关于Pytorch的二维tensor的gather和scatter_操作用法分析
看得不明不白(我在下一篇中写了如何理解gather的用法) gather是一个比较复杂的操作,对一个2维tensor,输出的每个元素如下: out[i][j] = input[index[i][j]] ...
- Shell脚本实现SSH免密登录及批量配置管理
本节索引 场景分析 ssh免密登录 pssh工具批量管理 SHELL自动化脚本 本篇总结 场景分析 作为一个运维工程师,不是每个人工作的环境都想阿里.腾讯那样,动不动就上亿的PV量,上万台服务器.我们 ...
- Struts2获取参数的几种方式
Struts2由于是一个贴心的框架,所以获取参数这种体力活,就无需再通过原生的request来getParameter了,有如下几种方式进行获取 1.Action中属性驱动,必须提供与form表单na ...
- sql server parameter validation of stored procedure
https://stackoverflow.com/questions/41908156/validating-missing-parameter-from-procedure-calls I don ...