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 ...
随机推荐
- [Angular] Auxiliary named router outlets
Define a auxilliary router: export const ROUTES: Routes = [ { path: 'folder/:name', component: MailF ...
- matlab 下的集成学习工具箱
matlab 当前支持的弱学习器(weak learners)类型分别为: 'Discriminant' 'knn' 'tree' 可通过 templateTree 定义: 1. fitcensemb ...
- java.lang.ClassNotFoundException: org.codehaus.jackson.JsonProcess******
http://blog.csdn.net/jrainbow/article/details/38764039
- html5-2 html实体和颜色有哪些
html5-2 html实体和颜色有哪些 一.总结 一句话总结:网站配色用安全色. 1.颜色用什么类型的颜色(安全色)? 直接百度搜 安全色 即可 2.html实体常用哪6个,头尾符号是什么? 头是取 ...
- Mybatis中sql语句中的in查询,一定要判断null的情况
不严谨的写法,可能会报错:in (),这种情况不符合mysql的语法. select from loanwhere LOAN_ID in <foreach item="item&quo ...
- 【hdu 3032】Nim or not Nim?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...
- [转]erlang ranch
一. ranch app启动: ranch_sup -> ranch_server % 创建ets, 并提供接口给其他进程读写 二. 启动diy app (监听模块: 用ranch_tcp -& ...
- js取json对象的键和值
//构建一个json对象 var pinpai = { "0":{"美的":49,"三星":35,"海信":25,&qu ...
- Cordova app 检查更新 ----JS进行调用(二)
原文:Cordova app 检查更新 ----JS进行调用(二) 1.获取版本号 需要添加 插件 cordova plugin add https://github.com/whiteoctober ...
- Method and system for providing security policy for linux-based security operating system
A system for providing security policy for a Linux-based security operating system, which includes a ...