数据结构与算法之PHP实现链表类(单链表/双链表/循环链表)
链表是由一组节点组成的集合。每个节点都使用一个对象的引用指向它的后继。指向另一个节点的引用叫做链表。
插入:链表中插入一个节点的效率很高。向链表中插入一个节点,需要修改它前面的节点(前驱),使其指向新加入的节点,而新加入的节点则指向原来前驱指向的节点(见下图)。
由上图可知,B、C之间插入D,三者之间的关系为
current为插入节点的前驱节点
current->next = new // B节点指向新节点D
new->next = current->next // 新节点D指向B的后继节点C
删除:从链表中删除一个元素,将待删除元素的前驱节点指向待删除元素的后继节点,同时将待删除元素指向 null,元素就删除成功了(见下图)。
由上图可知,A、C之间删除B,三者之间的关系为
current为要删除节点的前驱节点
current->next = current->next->next // A节点指向C节点
具体代码如下:
<?php
// 节点类
class Node {
public $data; // 节点数据
public $next; // 下一节点 public function __construct($data) {
$this->data = $data;
$this->next = NULL;
}
}
// 单链表类
class SingleLinkedList {
private $header; // 头节点 function __construct($data) {
$this->header = new Node($data);
}
// 查找节点
public function find($item) {
$current = $this->header;
while ($current->data != $item) {
$current = $current->next;
}
return $current;
}
// (在节点后)插入新节点
public function insert($item, $new) {
$newNode = new Node($new);
$current = $this->find($item);
$newNode->next = $current->next;
$current->next = $newNode;
return true;
} // 更新节点
public function update($old, $new) {
$current = $this->header;
if ($current->next == null) {
echo "链表为空!";
return;
}
while ($current->next != null) {
if ($current->data == $old) {
break;
}
$current = $current->next;
}
return $current->data = $new;
} // 查找待删除节点的前一个节点
public function findPrevious($item) {
$current = $this->header;
while ($current->next != null && $current->next->data != $item) {
$current = $current->next;
}
return $current;
} // 从链表中删除一个节点
public function delete($item) {
$previous = $this->findPrevious($item);
if ($previous->next != null) {
$previous->next = $previous->next->next;
}
} // findPrevious和delete的整合
public function remove($item) {
$current = $this->header;
while ($current->next != null && $current->next->data != $item) {
$current = $current->next;
}
if ($current->next != null) {
$current->next = $current->next->next;
}
} // 清空链表
public function clear() {
$this->header = null;
} // 显示链表中的元素
public function display() {
$current = $this->header;
if ($current->next == null) {
echo "链表为空!";
return;
}
while ($current->next != null) {
echo $current->next->data . "  ";
$current = $current->next;
}
}
} $linkedList = new SingleLinkedList('header');
$linkedList->insert('header', 'China');
$linkedList->insert('China', 'USA');
$linkedList->insert('USA','England');
$linkedList->insert('England','Australia');
echo '链表为:';
$linkedList->display();
echo "</br>";
echo '-----删除节点USA-----';
echo "</br>";
$linkedList->delete('USA');
echo '链表为:';
$linkedList->display();
echo "</br>";
echo '-----更新节点England为Japan-----';
echo "</br>";
$linkedList->update('England', 'Japan');
echo '链表为:';
$linkedList->display();
//echo "</br>";
//echo "-----清空链表-----";
//echo "</br>";
//$linkedList->clear();
//$linkedList->display(); // 输出:
链表为:China USA England Australia
-----删除节点USA-----
链表为:China England Australia
-----更新节点England为Japan-----
链表为:China Japan Australia PHP实现单链表
二、双链表


current为插入节点的前驱节点
current->next = new // B的next属性指向新节点D
new->next = current->next // 新节点D的next属性指向B的后继节点C
current->next->previous = new // B的后继节点C的previous属性指向新节点D(原先是C的previous属性指向B)

current为要删除的节点
current->previous->next = current->next // B的前驱节点A的next属性指向B的后继节点C
current->next->previous = current->previous // B的后继节点C的previous属性指向B的前驱节点A
current->previous = null // B的previous属性指向null
current->next = null // B的next属性指向null
<?php
// 节点类
class Node {
public $data; // 节点数据
public $previous = NULL; // 前驱
public $next = NULL; // 后继 public function __construct($data) {
$this->data = $data;
$this->previous = NULL;
$this->next = NULL;
}
}
// 双链表类
class DoubleLinkedList {
private $header; // 头节点 function __construct($data) {
$this->header = new Node($data);
}
// 查找节点
public function find($item) {
$current = $this->header;
while ($current->data != $item) {
$current = $current->next;
}
return $current;
}
// 查找链表最后一个节点
public function findLast() {
$current = $this->header;
while ($current->next != null) {
$current = $current->next;
}
return $current;
}
//(在节点后)插入新节点
public function insert($item, $new) {
$newNode = new Node($new);
$current = $this->find($item);
$newNode->next = $current->next;
$newNode->previous = $current;
$current->next = $newNode;
return true;
}
// 从链表中删除一个节点
public function delete($item) {
$current = $this->find($item);
if ($current->next != null) {
$current->previous->next = $current->next;
$current->next->previous = $current->previous;
$current->next = null;
$current->previous = null;
return true;
}
}
// 显示链表中的元素
public function display() {
$current = $this->header;
if ($current->next == null) {
echo "链表为空!";
return;
}
while ($current->next != null) {
echo $current->next->data . "  ";
$current = $current->next;
}
}
// 反序显示双向链表中的元素
public function dispReverse() {
$current = $this->findLast();
while ($current->previous != null) {
echo $current->data . "  ";
$current = $current->previous;
}
}
} // 测试
$linkedList = new DoubleLinkedList('header');
$linkedList->insert('header', 'China');
$linkedList->insert('China', 'USA');
$linkedList->insert('USA','England');
$linkedList->insert('England','Australia');
echo '链表为:';
$linkedList->display();
echo "</br>";
echo '-----删除节点USA-----';
echo "</br>";
$linkedList->delete('USA');
echo '链表为:';
$linkedList->display(); // 输出:
链表为:China USA England Australia
-----删除节点USA-----
链表为:China England Australia PHP实现双链表
三、循环链表


current为插入节点的前驱节点
// 中间
current->next = new // B节点指向新节点D
new->next = current->next // 新节点D指向B的后继节点C
// 尾端
current->next = new // C节点指向新节点D
new->next = header // 新节点D指向头节点Header

current为要删除节点的前驱节点
// 中间
current->next = current->next->next // A节点指向C节点
// 尾端
current->next = header // B节点指向头节点Header
<?php
// 节点类
class Node {
public $data; // 节点数据
public $previous;
public $next; // 下一节点 public function __construct($data) {
$this->data = $data;
$this->next = NULL;
}
}
// 循环链表类
class CircularLinkedList {
private $header; // 头节点 function __construct($data) {
$this->header = new Node($data);
$this->header->next = $this->header;
}
// 查找节点
public function find($item) {
$current = $this->header;
while ($current->data != $item) {
$current = $current->next;
}
return $current;
}
// 插入新节点
public function insert($item, $new) {
$newNode = new Node($new);
$current = $this->find($item);
if ($current->next != $this->header) { // 链表中间
$current->next = $newNode;
$newNode->next = $current->next;
} else { // 链表尾端
$current->next = $newNode;
$newNode->next = $this->header;
}
return true;
}
// 删除节点
public function delete($item) {
$current = $this->header;
while ($current->next != null && $current->next->data != $item) {
$current = $current->next;
}
if ($current->next != $this->header) { // 链表中间
$current->next = $current->next->next;
} else { // 链表尾端
$current->next = $this->header;
}
}
// 显示链表中的元素
public function display() {
$current = $this->header;
while ($current->next != $this->header) {
echo $current->next->data . "  ";
$current = $current->next;
}
}
} // 测试
$linkedList = new CircularLinkedList('header');
$linkedList->insert('header', 'China');
$linkedList->insert('China', 'USA');
$linkedList->insert('USA', 'England');
$linkedList->insert('England', 'Australia');
echo '链表为:';
$linkedList->display();
echo "</br>";
echo '-----删除节点USA-----';
echo "</br>";
$linkedList->delete('USA');
echo '链表为:';
$linkedList->display();
// 输出:
链表为:China USA England Australia
-----删除节点USA-----
链表为:China England Australia PHP实现循环链表
数据结构与算法之PHP实现链表类(单链表/双链表/循环链表)的更多相关文章
- 《Java数据结构与算法》笔记-CH5-链表-2单链表,增加根据关键字查找和删除
/** * Link节点 有数据项和next指向下一个Link引用 */ class Link { private int iData;// 数据 private double dData;// 数据 ...
- 《Java数据结构与算法》笔记-CH5-链表-1单链表
/** * Link节点 * 有数据项和next指向下一个Link引用 */ class Link { private int iData;//数据 private double dData;//数据 ...
- 数据结构和算法 – 4.字符串、 String 类和 StringBuilder 类
4.1.String类的应用 class String类应用 { static void Main(string[] args) { string astring = "Now is The ...
- JavaScript 数据结构与算法之美 - 线性表(数组、栈、队列、链表)
前言 基础知识就像是一座大楼的地基,它决定了我们的技术高度. 我们应该多掌握一些可移值的技术或者再过十几年应该都不会过时的技术,数据结构与算法就是其中之一. 栈.队列.链表.堆 是数据结构与算法中的基 ...
- C# 数据结构 - 单链表 双链表 环形链表
链表特点(单链表 双链表) 优点:插入和删除非常快.因为单链表只需要修改Next指向的节点,双链表只需要指向Next和Prev的节点就可以完成插入和删除操作. 缺点:当需要查找某一个节点的时候就需要一 ...
- 数据结构与算法(C/C++版)【绪论/线性表】
声明:数据结构与算法系列博文参考了<天勤高分笔记>.<王道复习指导>.C语言中文网.非商业用途,仅为学习笔记总结! 第一章<绪论> 一.基本概念及入门常识 /// ...
- Linux内核中常用的数据结构和算法(转)
知乎链接:https://zhuanlan.zhihu.com/p/58087261 Linux内核代码中广泛使用了数据结构和算法,其中最常用的两个是链表和红黑树. 链表 Linux内核代码大量使用了 ...
- 双链表算法原理【Java实现】(八)
前言 前面两节内容我们详细介绍了ArrayList,一是手写实现ArrayList数据结构,而是通过分析ArrayList源码看看内置实现,关于集合内容一如既往,本节课我们继续学习集合LinkedLi ...
- Python与数据结构[0] -> 链表/LinkedList[1] -> 双链表与循环双链表的 Python 实现
双链表 / Doubly Linked List 目录 双链表 循环双链表 1 双链表 双链表和单链表的不同之处在于,双链表需要多增加一个域(C语言),即在Python中需要多增加一个属性,用于存储指 ...
- 数组、单链表和双链表介绍 以及 双向链表的C/C++/Java实现
概要 线性表是一种线性结构,它是具有相同类型的n(n≥0)个数据元素组成的有限序列.本章先介绍线性表的几个基本组成部分:数组.单向链表.双向链表:随后给出双向链表的C.C++和Java三种语言的实现. ...
随机推荐
- 蚂蚁金服缘何自研Service Mesh?
2018年,微服务方兴未艾,Service Mesh(服务网格)又快速崛起.有观点认为,2018年可被称之为“Service Mesh元年”,在未来两年中,Service Mesh将迎来爆发式增长,成 ...
- POJ 1488 - TEX Quotes
Description TEX is a typesetting language developed by Donald Knuth. It takes source text together w ...
- EM学习-思想和代码
EM算法的简明实现 当然是教学用的简明实现了,这份实现是针对双硬币模型的. 双硬币模型 假设有两枚硬币A.B,以相同的概率随机选择一个硬币,进行如下的抛硬币实验:共做5次实验,每次实验独立的抛十次,结 ...
- vue中导入外面文件(css,js)方式
有时我们需要导入外面的css文件(例如reset.css文件,bootstrap.css,jQuery.js文件),通常可通过import "name.css"的形式 对于rese ...
- OCR技术(光学字符识别)
什么是OCR? OCR英文全称是optical character recognition,中文叫光学字符识别.它是利用光学技术和计算机技术把印在或者写在纸上的 文字读取出来,并转换成一种计算机能够接 ...
- Java 输入/输出——Java虚拟机读写其它进程的数据
使用 Runtime对象的exec()方法可以运行平台上的其它程序,该方法产生一个Process对象,Process对象代表由该Java程序启动的子进程.Process类提供了如下三个方法,用于让程序 ...
- iOS中类、元类、isa详解
类相信大家都知道是什么,如果看过runtime的源码或者看过相关的文章对isa肯定也不陌生,不过元类(meta class)大家可能就比较陌生了.不过大家也不要担心,我会细细道来,让大家明白它到底是个 ...
- [apr] Apache Portable Runtime
接手新项目,前任大量的使用了APR,虽然不影响理解主逻辑,但是看见一行不知道干嘛的代码,总是特别炸眼. 况且,虽然我从来不跨平台.但是APR如此优秀,学习理解它都不是浪费时间. 可是让人崩溃的是,竟然 ...
- 转:Eclipse快捷键和实用技巧
原文地址:Eclipse快捷键和实用技巧 工欲善其事,必先利其器.对于程序员来说,Eclipse便是其中的一个“器”.本文会从Eclipse快捷键和实用技巧这两个篇章展开介绍.Eclipse快捷键用熟 ...
- day3_列表
一.列表 列表也通常被称为list 数组 array 1.列表定义 使用方括号([])即可 alist = [123,'abc','def',666,True] 空列表的定义:alist = [] 或 ...