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 ...
随机推荐
- window对象属性alert、confirm、prompt怎么使用?
window对象属性alert.confirm.prompt怎么使用? 一.总结 1.参数依次复杂,返回值依次复杂,但是感觉都是一一继承的,所以很好想也很好写. 二.window对象alert.con ...
- BigDecimal 舍入模式(Rounding mode)介绍
BigDecimal 舍入模式(Rounding mode)介绍 什么样的经历,才能领悟成为架构师? >>> 1 RoundingMode介绍 package java.math ...
- iOS中js与objective-c的简单交互
1.首先是objective-c调用js中的代码,可以用UIWebview中的一个方法 stringByEvaluatingJavaScriptFromString:后面接的是js中的方法名.这个函数 ...
- javascript 验证附件大小
///验证单个文件不能超过30M function onChangeFile() { ///定义布尔类型的返回结果,初始值为false(默认不超过30M) var sResult = false; / ...
- Centos 6.x 配置hadoop的环境变量
1.安装jdk 原来是用的rpm安装的1.7,所以先使用rpm -qa|grep jdk,找到安装的1.7后 rpm -e --nodeps xxx.使用securecrt把官网下载的jdk-8u18 ...
- 【87.65%】【codeforces 731A】Night at the Museum
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【44.19%】【codeforces 727C】Guess the Array
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 支付(异步通知notify_url 与 同步通知return_url的区别)
同步通知和异步通知发送的数据没有本质的区别:同步通知有2个作用:第一是从支付宝的页面上返回自己的网站继续后续操作:第二是携带支付状态的get参数:让自己的网站用于验证: 同步通知后:还需要异步通知主要 ...
- android 流量统计
1 android通过架构流量统计TrafficStats类可以直接获得 获得总流量受理TrafficStats.getTotalRxBytes(), 获得总传出流量TrafficSt ...
- 相关ubuntu有几个细节有用的工具系列
前言 于Linux制,FTPserver有许多软件,我们已经成熟,像vsftpd, wu-ftp, Pure-FTPd等一下.不过该软件的安装一切,配置比较麻烦,建立个人FTPserver,仍是Pro ...