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 SingleLinkList
{
private $header;
public function __construct($id=null,$name=null)
{
$this->header=new Node($id,$name);
} /**
* 获取单链表的长度
*/
public function getLinkLength(){
$i = ;
$current = $this->header;
while($current->next !=null){
$i++;
$current = $current->next;
}
return $i;
} /**
* 添加节点到单链表中(原理就是插入当前的节点的位置存储的下一个节点的信息,上一个节点存储当前节点的信息)
* @param $node 节点
*/
public function addLink($node){
$current = $this->header;
while($current->next!=null){
if($current->next->id > $node->id){
break;
}
$current = $current->next;
}
$node->next = $current->next;
$current->next = $node;
} /**
* 删除节点(删除的节点的下一个位置保持在上一个节点中)
* @param $id
*/
public function delLink($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 '未找到该节点'.$id.'的信息';
}
} /**
* 判断单链表是否为空
* @return bool
*/
public function isEmpty(){
return $this->header == null;
} /**
* 清空单链表
*/
public function clear(){
$this->header=null;
} /**
* 获取链表的信息
*/
public function getLinkList(){
$current = $this->header;
if($current->next==null){
echo '链表为空';
return;
}
while($current->next != null){
echo 'id:'.$current->next->id.',节点名称为'.$current->next->name.'<br/>';
if($current->next->next == null){
break;
}
$current=$current->next;
}
} /**
* 获取某个节点信息
* @param $id
* @return string
*/
public function getLinkNameById($id){
$current = $this->header;
if($current->next==null){
return '';
} while($current->next != null){
if($current->id == $id){
return $current->name;
}
$current=$current->next;
}
} /**
* 更新某个节点名称
* @param $id
* @param $name
*/
public function updateLink($id,$name){
$current = $this->header;
if($current->next==null){
return '';
} while($current->next != null){
if($current->id == $id){
return $current->name=$name;
}
$current=$current->next;
}
}
} header('Content-Type:text/html;charset=utf8');
$lists = new SingleLinkList(); $lists->addLink ( new node ( , 'eeeeee' ) );
$lists->addLink ( new node ( , 'aaaaaa' ) );
$lists->addLink ( new node ( , 'ffffff' ) );
$lists->addLink ( new node ( , 'dddddd' ) );
$lists->addLink ( new node ( , 'cccccc' ) );
$lists->addLink ( new node ( , 'bbbbbb' ) );
$lists->getLinkList ();
echo "<br>-----------删除节点--------------<br>";
$lists->delLink ( );
$lists->getLinkList ();
echo "<br>-----------更新节点名称--------------<br>";
$lists->updateLink ( , "" );
$lists->getLinkList ();
echo "<br>-----------获取节点名称--------------<br>";
echo $lists->getLinkNameById ( );
echo $lists->getLinkNameById ( );
echo "<br>-----------获取链表长度--------------<br>";
echo $lists->getLinkLength ();
PHP模拟单链表的数据结构的更多相关文章
- python算法双指针问题:使用列表和数组模拟单链表
这个很多基础算法,python已内部实现了. 所以,要想自己实现链表这些功能时, 反而需要自己来构造链表的数据结构. 当然,这是python灵活之处, 也是python性能表达不如意的来源. valu ...
- 面试题:Add Two Numbers(模拟单链表)
题干: You are given two non-empty linked lists representing two non-negative integers. The digits are ...
- 数据结构(一) 单链表的实现-JAVA
数据结构还是很重要的,就算不是那种很牛逼的,但起码得知道基础的东西,这一系列就算是复习一下以前学过的数据结构和填补自己在这一块的知识的空缺.加油.珍惜校园中自由学习的时光.按照链表.栈.队列.排序.数 ...
- 用最简单的方式学Python单链表
Python 实现单链表 在本博客中,我们介绍单链表这种数据结构,链表结构为基于数组的序列提供了另一种选择(例如Python列表). 基于数组的序列和链表都能够对其中的元素保持一定得顺序,但采用的方式 ...
- java实现单链表的增删改以及排序
使用java代码模拟单链表的增删改以及排序功能 代码如下: package com.seizedays.linked_list; public class SingleLinkedListDemo { ...
- 用最容易的方式学会单链表(Python实现)
单链表与数组 在本博客中,我们介绍单链表这种数据结构,链表结构为基于数组的序列提供了另一种选择(例如Python列表). 基于数组的序列也会有如下缺点: 一个动态数组的长度可能超过实际存储数组元素所需 ...
- 数据结构与算法——链表 Linked List(单链表、双向链表、单向环形链表-Josephu 问题)
链表是有序的列表,但是在内存中存储图下图所示 链表是以 节点 的方式来存储,是 链式存储 每个节点包含 data 域.next 域,指向下一个节点 链表的各个节点 不一定是连续存储,如上图所示 链表还 ...
- 数据结构图文解析之:数组、单链表、双链表介绍及C++模板实现
0. 数据结构图文解析系列 数据结构系列文章 数据结构图文解析之:数组.单链表.双链表介绍及C++模板实现 数据结构图文解析之:栈的简介及C++模板实现 数据结构图文解析之:队列详解与C++模板实现 ...
- 数据结构与算法分析——C语言描述 第三章的单链表
数据结构与算法分析--C语言描述 第三章的单链表 很基础的东西.走一遍流程.有人说学编程最简单最笨的方法就是把书上的代码敲一遍.这个我是头文件是照抄的..c源文件自己实现. list.h typede ...
随机推荐
- ArcSDE 设置
---------------------转载----------------------- a)创建加载路径——st_shapelib.dll 执行创建库脚本:create or r ...
- Android 调整透明度的图片查看器
本文以实例讲解了基于Android的可以调整透明度的图片查看器实现方法,具体如下: main.xml部分代码如下: <?xml version="1.0" encoding ...
- TensorFlow中卷积
CNN中的卷积核及TensorFlow中卷积的各种实现 声明: 1. 我和每一个应该看这篇博文的人一样,都是初学者,都是小菜鸟,我发布博文只是希望加深学习印象并与大家讨论. 2. 我不确定的地方用了“ ...
- 【hdu 1846】Brave Game
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...
- 物联网学生科协第三届H-star现场编程比赛
问题 A: 剪纸片 时间限制: 1 Sec 内存限制: 128 MB 题目描写叙述 这是一道简单的题目,假如你身边有一张纸.一把剪刀.在H-star的比赛现场,你会这么做: 1. 将这张纸剪成两片(平 ...
- matlab 实现 stacked Autoencoder 解决图像分类问题
Train Stacked Autoencoders for Image Classification 1. 加载数据到内存 [train_x, train_y] = digitTrainCellAr ...
- ITFriend创业败局(三):技术人员创业,需要尽可能避免,或者需要解决的5个重要问题
一.插科打诨: 本想给小雷粉,做一个创业"成功案例"的,结果做成了一个"反面教材"~ No zuo,no die~ 二.写作目的:分享自己作为一名技术人员,或者 ...
- 颜色转换:#hhhfff->UIColor (MHHexColoring)
MHHexColoring为开发者快速获取想要的十六进制颜色(Hex Color) 查找16进制色码的网站:http://www.color-hex.com // 版权属于原作者 MHHexColor ...
- NOIP模拟 拆网线 - 贪心策略+dp
题目大意: 给一颗n个节点的树,保留最少的边,使得每个连通块的大小都大于等于2,并且连通块的点数和等于k. 题目分析: 要想留下的边数最少,就要尽量多的选择单独的边,这里就要贪心:尽可能多的选择单独的 ...
- NOIP模拟 Work - 二分 + 树状数组 / ???
题目分析 如果没有最后的注意事项,此题就是二分裸题.有了注意事项,会有两种思路: 在线:二分天数t,并在主席树上求1~t天中大于d(浪费的时间)的时间之和以及数量,答案即为:sum - d * cnt ...