php实现简单的单链表
<?php
/**
* 建立一个链表,节点的data为数组,记录一个id,完成链表所以操作
*/ //结点,结点数据data定义为一个数组,id和value
class Node{
public $data;
public $next; public function __construct($data,$next=null){
$this->data = $data;
$this->next = $next;
}
} class Linklist{
public $header; public function __construct(){
$this->header = new Node(null);
} //向尾部添加结点
public function add(Node $node){
$current = $this->header;
while ($current->next !==null) {
$current = $current->next;
}
$current->next = $node;
} //遍历列表,打印结点
public function show(){
$current = $this->header;
while ($current->next !== null){
//print_r($current->next->data);
//echo "<br/>";
echo $current->next->data['value'];
$current = $current->next;
}
} //获取链表长度,不含头结点(空)
public function getLength(){
$len = 0;
$current = $this->header;
while ($current->next !== null) {
$len++;
$current = $current->next;
}
return $len;
} //查找value是否在list中,存在返回id,否则false
public function find($value){
$current = $this->header;
$flag = 0;
while ($current->next!==null){
if($value == $current->next->data['value']){
$flag = 1;
$id = $current->next->data['id'];
break;
}
$current = $current->next;
}
if($flag == 0){
return false;
}else{
return $id;
}
} //插入一个结点,在id之前
public function insert(Node $node,$id){
$current = $this->header;
while ($current->next!==null){
if($id == $current->next->data['id']){
$tmp = $current->next;
$current->next = $node;
$current->next->next = $tmp;
break;
}
$current = $current->next;
}
} //删除一个结点(第一个出现的)
public function del($id){
$current = $this->header;
while ($current->next!=null){
if($current->next->data['id'] == $id){
$current->next = $current->next->next;
break;
}
$current = $current->next;
}
} //更新结点value
public function update($id,$value){
$current = $this->header;
while ($current->next !==null){
if($current->next->data['id'] == $id){
$current->next->data['value'] = $value;
}
$current = $current->next;
}
}
} class Client{
public static function main(){
$list = new LinkList();
$data1 = array(
'id'=>1,
'value'=>'hello ',
);
$data2 = array(
'id'=>2,
'value'=>'world',
);
$data3 = array(
'id'=>3,
'value'=>'!',
); $node1 = new Node($data1);
$node2 = new Node($data2);
$node3 = new Node($data3); $list->add($node1);
$list->add($node2);
$list->add($node3); // $list->show();
// echo "<br/>"; $node4 = new Node($data3);
$list->insert($node4, 2);
//$list->del(3);
$list->update(3, '!!!');
$list->show();
echo "<br/>"; }
} Client::main();
?>
增加结点、删除结点、查找结点、修改结点、获取长度,遍历均实现。
php实现简单的单链表的更多相关文章
- C++实现简单的单链表
下面实现的是一个简单的单链表 功能不多,学习使用 #pragma once #include <iostream> using namespace std; class ListEx { ...
- java实现一个简单的单链表反转
自定义一个单链表,实现链表反转: 1.普通方法实现 2.递归方式实现 package listNode; public class ReverseNode { public static void m ...
- 单链表数据结构 - java简单实现
链表中最简单的一种是单向链表,每个元素包含两个域,值域和指针域,我们把这样的元素称之为节点.每个节点的指针域内有一个指针,指向下一个节点,而最后一个节点则指向一个空值.如图就是一个单向链表 一个单向链 ...
- C++ 单链表基本操作
链表一直是面试的高频题,今天先总结一下单链表的使用,下节再总结双向链表的.本文主要有单链表的创建.插入.删除节点等. 1.概念 单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数 ...
- "《算法导论》之‘线性表’":基于数组实现的单链表
对于单链表,我们大多时候会用指针来实现(可参考基于指针实现的单链表).现在我们就来看看怎么用数组来实现单链表. 1. 定义单链表中结点的数据结构 typedef int ElementType; cl ...
- 简单约瑟夫环的循环单链表实现(C++)
刚刚接触C++以及数据结构,今天做了第一次尝试用C++和数据结构解决问题,问题是基于约瑟夫环问题的简单版. 先来看看约瑟夫环问题的介绍: 约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3.. ...
- 用最简单的方式学Python单链表
Python 实现单链表 在本博客中,我们介绍单链表这种数据结构,链表结构为基于数组的序列提供了另一种选择(例如Python列表). 基于数组的序列和链表都能够对其中的元素保持一定得顺序,但采用的方式 ...
- 超级简单的数组加单链表实现Map
/** * 超级简单的数组加单链表实现Map * @author jlj * */ public class MyHashMap { public MyList[] lists; public int ...
- C:单链表的简单实现
前言 今天整理资料的时候翻出来的文件,发现是以前学习数据结构的时候写的代码,当初是看郝凯老师的视频学习的C语言的数据结构,下面是对于一个单链表的简单的实现. /** ***************** ...
随机推荐
- visual studio 不能进入调试状态
解决Windows操作系统在处理回环地址 1. 第一种解决方案是禁用环回检查. 步骤如下 a) 依次单击“开始”和“运行”,键入 regedit,然后单击“确定” b) 在注册表编辑器中,找到并单击下 ...
- javascript中字符串常用操作总结
String对象属性 (1) length属性 length算是字符串中非常常用的一个属性了,它的功能是获取字符串的长度.当然需要注意的是js中的中文每个汉字也只代表一个字符,这里可能跟其他语言有些不 ...
- C#动态创建Gridview及批量插入到数据库
这里介绍两种动态创建Gridview的方法: (一).有时需要应付上头领导的检查,所以就弄一些静态的Gridview来显示数据,这种方法的优点就是不用连接数据库,比较方便,但是代码灵活性不高,所有数据 ...
- tcpcopy + tcpdump 离线回放
简单来说,就是用tcpdump记录线上请求,用tcpcopy来重放,如下图所示: 有关 tcpdump 的命令详解请参考: http://www.cnblogs.com/ggjucheng/arc ...
- Java新建线程的两种方式
Java新建线程有两种方式,一种是通过继承Thread类,一种是实现Runnable接口,下面是新建线程的两种方式. 我们假设有个竞赛,有一个选手A做俯卧撑,一个选手B做仰卧起坐.分别为两个线程: p ...
- 使用postman测试接口时需要先登录怎么办
原文 1.在浏览器上先登录,登录成功后获取cookie: 2.接着打开postman:
- package.json中devDependencies与dependencies的区别
前言:之前一直不懂既然都是项目的依赖,为什么要分成两个部分,devDependencies和dependencies,有什么区别? 安装方式 我们在通过npm安装插件或库时,有三种方式: npm in ...
- 微信小程序为什么不被看好?
我自认为对新技术还是比较有热情的,可对于小程序这个“新技术”,我却完全是被动的.去年9月份的时候,微信小程序开始内测,瞬间引爆朋友圈.知乎等一众分享平台.当时我大概了解了一下,觉得从技术角度上来说没啥 ...
- Vue 框架-09-初识组件的应用
Vue 框架-09-初识组件的应用 今天的第一个小实例,初步使用组件: 在 app.js 中定义模板组件,在 html 文件中使用自定义标签来显示 js 文件中定义的 html 代码块 比如说,下面定 ...
- Python3 循环语句
Python3 循环语句 转来的 很适合小白 感谢作者 Python中的循环语句有 for 和 while. Python循环语句的控制结构图如下所示: while 循环 Python中wh ...