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语言的数据结构,下面是对于一个单链表的简单的实现. /** ***************** ...
随机推荐
- 使用JS导出页面内容到Excel
JS代码 <script> $(function(){ // 使用outerHTML属性获取整个table元素的HTML代码(包括<table>标签),然后包装成一个完整的HT ...
- java.sql.SQLException: Access denied for user 'root '@'localhost' (using password: YES) 最蠢
我犯了七年前的错误,一个空格,昨天就想到的,还对比了一下密码有没有空格 问题原因1:多写空格 在datasource.properties 中的username 的值root后面多写了一个空格, jd ...
- SQL Server Profiler小技巧——筛选请求
如果需要转载,请附上本文作者和原文链接:http://www.cnblogs.com/zeusro/p/4016228.html Microsoft SQL Server Profiler 是 SQL ...
- js二分查找树实现
function BinaryTree() { var Node = function(key) { this.key = key; this.left = null; this.right = nu ...
- 阿里云短信服务Java版
短信服务管理平台 https://dysms.console.aliyun.com/dysms.htm java短信发送API https://help.aliyun.com/document_ ...
- 解决ubuntu使用命令sudo apt -get install 安装东西时出现"E: Sub-process /usr/bin/dpkg returned an error code (1) "的错误
问题描述: 今天在使用命令 "sudo apt-get install python3-pip"安装时,总是出现如下图这样的错误,开始以为是以为自己python版本的问题,后来发现 ...
- web百度地图离线开发
公司现在做的一个项目本来用的是google离线地图,但是发现谷歌的地图数据很久没更新数据了,中国的城市发展这么快,好多地方地图和现实都对不上了. 发现百度地图数据更新挺快的(呵呵,毕竟是国产的吗),最 ...
- android IO流操作文件(存储和读取)
存储文件: public class FileOperate extends Activity { private static final String FILENAME = "mydat ...
- android中的textview显示汉字不能自动换行的一个解决办法
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_ ...
- android之画板功能之橡皮擦 画笔大小和画笔颜色
第一展示设置画笔颜色的功能,第二展示设置画笔大小的颜色,而第三则展示橡皮擦的功能,这节将图标颜色设置为了蓝色,并且,增加了最左边的按钮(其实,就是在gridview中多增加了一个item). 下面分别 ...