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语言的数据结构,下面是对于一个单链表的简单的实现. /** ***************** ...
随机推荐
- APICloud APP前端框架——手机APP开发、APP制作、APP定制平台
概述 APICloud前端框架,包括api.js和api.css.api.css处理不同平台浏览器的默认样式.api.js是一个JavaScript库.是APICloud为混合移动开发定制的轻量Jav ...
- 用fullPage来实现全屏滚动效果
[注意]所有的page要用div包裹,id为fullpage.不能直接包在body中 [使用fullpage的步骤] 1.导入 JQuery.js,fullpage.js,fullpage.css ...
- git 拉取远程分支报错(fatal: '' is not a commit and a branch '' cannot be created from it)
问题描述从远程git上拉取某一个分支,然后报错,拉取不了这个分支. 拉取分支的命令: git checkout -b xxx-static-19 origin/xxx-static-19 其中xxx- ...
- [javaSE] 数组(排序-选择排序)
两层嵌套循环,外层循环控制次数,内层循环进行比较 for(int x=0;x<arr.length;x++){ for(int y=0;y<arr.length;y++){ if(arr[ ...
- HDU2612(KB1-N)
Find a way Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 【代码笔记】iOS-获取现在的日历时间
一,代码. - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. ...
- Spring Boot—20Zookeeper
https://docs.spring.io/spring-boot/docs/2.0.1.RELEASE/reference/htmlsingle/ pom.xml <dependency&g ...
- Spring Boot—18Redis
pom.xml <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-ja ...
- Atitit.播放系统的选片服务器,包厢记时系统 的说明,教程,维护,故障排查手册p825
Atitit.播放系统的选片服务器,包厢记时系统 的说明,教程,维护,故障排查手册p825 1. 播放系统服务器方面的维护2 1.1. 默认情况下,已经在系统的启动目录下增加了俩个启动项目2 1.2. ...
- Android开发各种Utils收集库
为方便查找,已进行大致归类,其目录如下所示: Activity相关→ActivityUtils.java→Demo isActivityExists : 判断是否存在Activity launchAc ...