<?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模拟单链表的数据结构的更多相关文章

  1. python算法双指针问题:使用列表和数组模拟单链表

    这个很多基础算法,python已内部实现了. 所以,要想自己实现链表这些功能时, 反而需要自己来构造链表的数据结构. 当然,这是python灵活之处, 也是python性能表达不如意的来源. valu ...

  2. 面试题:Add Two Numbers(模拟单链表)

    题干: You are given two non-empty linked lists representing two non-negative integers. The digits are ...

  3. 数据结构(一) 单链表的实现-JAVA

    数据结构还是很重要的,就算不是那种很牛逼的,但起码得知道基础的东西,这一系列就算是复习一下以前学过的数据结构和填补自己在这一块的知识的空缺.加油.珍惜校园中自由学习的时光.按照链表.栈.队列.排序.数 ...

  4. 用最简单的方式学Python单链表

    Python 实现单链表 在本博客中,我们介绍单链表这种数据结构,链表结构为基于数组的序列提供了另一种选择(例如Python列表). 基于数组的序列和链表都能够对其中的元素保持一定得顺序,但采用的方式 ...

  5. java实现单链表的增删改以及排序

    使用java代码模拟单链表的增删改以及排序功能 代码如下: package com.seizedays.linked_list; public class SingleLinkedListDemo { ...

  6. 用最容易的方式学会单链表(Python实现)

    单链表与数组 在本博客中,我们介绍单链表这种数据结构,链表结构为基于数组的序列提供了另一种选择(例如Python列表). 基于数组的序列也会有如下缺点: 一个动态数组的长度可能超过实际存储数组元素所需 ...

  7. 数据结构与算法——链表 Linked List(单链表、双向链表、单向环形链表-Josephu 问题)

    链表是有序的列表,但是在内存中存储图下图所示 链表是以 节点 的方式来存储,是 链式存储 每个节点包含 data 域.next 域,指向下一个节点 链表的各个节点 不一定是连续存储,如上图所示 链表还 ...

  8. 数据结构图文解析之:数组、单链表、双链表介绍及C++模板实现

    0. 数据结构图文解析系列 数据结构系列文章 数据结构图文解析之:数组.单链表.双链表介绍及C++模板实现 数据结构图文解析之:栈的简介及C++模板实现 数据结构图文解析之:队列详解与C++模板实现 ...

  9. 数据结构与算法分析——C语言描述 第三章的单链表

    数据结构与算法分析--C语言描述 第三章的单链表 很基础的东西.走一遍流程.有人说学编程最简单最笨的方法就是把书上的代码敲一遍.这个我是头文件是照抄的..c源文件自己实现. list.h typede ...

随机推荐

  1. js进阶正则表达式15验证身份证号(|符号的使用:var reg=/^\d{17}[\d|X]$|^\d{15}$/)(str的方法substr)

    js进阶正则表达式15验证身份证号(|符号的使用:var reg=/^\d{17}[\d|X]$|^\d{15}$/)(str的方法substr) 一.总结 1.|符号的使用:var reg=/^\d ...

  2. js字符串转换为数字的三种方法。(转换函数)(强制类型转换)(利用js变量弱类型转换)

    js字符串转换为数字的三种方法.(转换函数)(强制类型转换)(利用js变量弱类型转换) 一.总结 js字符串转换为数字的三种方法(parseInt("1234blue"))(Num ...

  3. 【转载】FormsAuthenticationTicket 对象

    1.使用Forms验证存储用户自定义信息 Forms验证在内部的机制为把用户数据加密后保存在一个基于cookie的票据FormsAuthenticationTicket中,因为是经过特殊加密的,所以应 ...

  4. node转发formdata

    router.post('/keUpload', checkLogin, setAccessControlAllow, (req, res, next) => { const busboy = ...

  5. idea中建立一个OSGI项目

    参考网址:http://chenjingbo.iteye.com/blog/1893597 首先我使用的是equinox作为我的osgi framework 直接在官网上解压下载即可,第一步creta ...

  6. DYNAMIC CONTEXT SWITCHING BETWEEN ARCHITECTURALLY DISTINCT GRAPHICS PROCESSORS

    FIELD OF INVENTION This invention relates to computer graphics processing, and more specifically to ...

  7. 作为电磁波的 Wi-Fi 信号

    常用 WiFi:2.4 GHz,而同样是电磁波的可将光,频率是 WiFi 的十几万倍: 如何让家里的 WiFi 信号变得更好,移动路由器到合适位置: 1. Wi-Fi:穿墙有术还是穿墙无术? 我们都会 ...

  8. Xcode崩溃定位:异常位置Exception的断点

    1.全局Exception断点 在XCode界面中按cmd + 6跳到Breakpoint的tab(或者点击下图左上第7个图标),然后点击左下角的+号,增加一个Exception的断点,如下图所示.这 ...

  9. ANR触发原理

    ANR(Application Not responding),是指应用程序未响应,Android系统对于一些事件需要在一定的时间范围内完成,如果超过预定时间能未能得到有效响应或者响应时间过长,都会造 ...

  10. Mobile Push Notification

    In one embodiment, a method includes sending to a mobile client computing device a first notificatio ...