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 Directive] Build a Directive that Tracks User Events in a Service in Angular 2
A @Directive is used to add behavior to elements and components in your application. This makes @Dir ...
- html5-6 Frame框架窗口类型
html5-6 Frame框架窗口类型 一.总结 一句话总结: 1.点左侧的a链接如何打开右侧页面? <a href='user/index.html' target='right'>& ...
- The DOT Language
CSDN新首页上线啦,邀请你来立即体验! 立即体验 博客 学院 下载 更多 登录注册 The DOT Language 翻译 2014年04月15日 11:27:07 标签: EBNF / 语言 / ...
- CSS自己主动换行、强制不换行、强制断行、超出显示省略号
P标签是默认是自己主动换行的,因此设置好宽度之后,可以较好的实现效果,可是近期的项目中发现,使用ajax载入数据之后.p标签内的内容没有换行,导致布局错乱,于是尝试着使用换行样式,尽管攻克了问题.可是 ...
- mui列表跳转到详情页优化方案
原理 因为列表页到详情页是多对一的形式,即列表页的多条数据列表对应的是一个详情页,只是数据不同而:因此,可以在加载列表页时预加载详情页,即创建一个详情页的webview,但是不显示出来,点击列表的时候 ...
- 【hdu 5996】dingyeye loves stone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s) ...
- 神经进化学的简介和一个简单的CPPN(Compositional Pattern Producing Networks)DEMO
近期迷上神经进化(Neuroevolution)这个方向,感觉是Deep Learning之后的一个非常不错的研究领域. 该领域的一个主导就是仿照人的遗传机制来进化网络參数与结构.注意,连网络结构都能 ...
- 使用多target来构建大量相似App
转自 come from : http://devtang.com/blog/2013/10/17/the-tech-detail-of-ape-client-1/ 猿题库iOS客户端的技术细节(一) ...
- WPF 小矢量图 : 主页,返回,加,减,文字按钮,左移,右移
原文:WPF 小矢量图 : 主页,返回,加,减,文字按钮,左移,右移 代码:: <UserControl x:Class="SQ.TestPage" xmlns=" ...
- C# opcode 查询源码
Add|将两个值相加并将结果推送到计算堆栈上.Add.Ovf|将两个整数相加,执行溢出检查,并且将结果推送到计算堆栈上.Add.Ovf.Un|将两个无符号整数值相加,执行溢出检查,并且将结果推送到计算 ...