2019年2月25日17:24:34

final class BasicNode {

    public $index;
public $data;
public $next = null;
public $pre = null; public function __construct($index, $data) {
$this->index = $index;
$this->data = $data;
} }
<?php

/*
* 双向链表
*/ final class DoublyLinkedList { //从链表尾部压入一个节点,节点自动维护,不需要要像main方法那样自己维护
public function add(BasicNode $head, BasicNode $Node) {
$current = $head; //让$current指向$head;
//顺序联表根据index排序
while ($current->next != null) {
//head元素为空,从第一个有数据元素开始检测
if ($current->next->index > $Node->index) {//如果有相同的index就不能插入
break;
//$current没有 next元素
} elseif ($current->next->index == $Node->index) {
throw new \Exception('index重复');
}
$current = $current->next;
}
// p($current);
//没有元素,和尾部插入元素
//中间插入情况
if ($current->next != null) {
$Node->next = $current->next;
}
$Node->pre = $current;
if ($current->next != null) {
$current->next->pre = $Node;
}
$current->next = $Node;
} //从链表尾压出一个节点
public function delete(BasicNode $head, $index) {
$current = $head; //让$current指向$head;
$has = false;
while ($current->next != null) {
//提前查找链表尾部是否为空,为空就是尾部,吧当前节点的next复制问NULL,就是尾部元素干掉
if ($current->next->index == $index) {
$has = true;
break;
}
$current = $current->next;
}
if (!$has) {
throw new \Exception('index没有找到');
}
if ($current->next != null) {
$current->next->pre = $current;
}
$current->next = $current->next->next;
} //修改数据
public function update(BasicNode $head, $index, $data) {
$current = $head; //让$current指向$head;
$has = false;
while ($current->next != null) {
if ($current->next->index == $index) {
$has = true;
break;
}
$current = $current->next;
}
if (!$has) {
throw new \Exception('index没有找到');
}
$current->next->data = $data;
} }

测试代码

$head = new BasicNode(null, []);
$a = new BasicNode(1, ['a']);
$b = new BasicNode(5, ['b']);
$c = new BasicNode(8, ['c']);
$d = new BasicNode(99, ['d']);
$e = new BasicNode(66, ['e']); //if ($head->next->index > 1) {
// pp('大于');
//} else {
// pp('小于');
//} $DoublyLinkedList = new DoublyLinkedList();
$DoublyLinkedList->add($head, $b);
//pp($head);
$DoublyLinkedList->add($head, $a);
//pp($head);
$DoublyLinkedList->add($head, $d);
$DoublyLinkedList->add($head, $e); $DoublyLinkedList->add($head, $c); //$DoublyLinkedList->update($head, 5, ['2312321']);
$DoublyLinkedList->delete($head, 99);
pp($head);

最麻烦的是新增的时候去维护互相连接的中间节点

PHP算法学习(7) 双向链表 实现栈的更多相关文章

  1. C语言算法系列---1.队列和栈

    写在前面:在家玩了好久,实在是不知道干嘛了,突然想找些事做,现在是时候做些什么了.这些东西不见得多高深,也可能很简单,但很基础,也无法忽视.同时,也是自己学习走过的一条路. 这是开头,就写写C的队列和 ...

  2. Kosaraju算法学习

    Kosaraju 算法学习 序 这星期捣鼓了一个新的算法--Kosaraju算法 今天分享给大家 简介 Kosaraju算法,其实与tarjan算法差不多.但是码量较小,容易记忆.其时间复杂度与tar ...

  3. 学习javascript数据结构(一)——栈和队列

    前言 只要你不计较得失,人生还有什么不能想法子克服的. 原文地址:学习javascript数据结构(一)--栈和队列 博主博客地址:Damonare的个人博客 几乎所有的编程语言都原生支持数组类型,因 ...

  4. DSP算法学习-过采样技术

    DSP算法学习-过采样技术 彭会锋 2015-04-27 23:23:47 参考论文: 1 http://wr.lib.tsinghua.edu.cn/sites/default/files/1207 ...

  5. 算法学习之C语言基础

    算法学习,先熟悉一下C语言哈!!! #include <conio.h> #include<stdio.h> int main(){ printf(+); getch(); ; ...

  6. Python之路,Day21 - 常用算法学习

    Python之路,Day21 - 常用算法学习   本节内容 算法定义 时间复杂度 空间复杂度 常用算法实例 1.算法定义 算法(Algorithm)是指解题方案的准确而完整的描述,是一系列解决问题的 ...

  7. C / C++算法学习笔记(8)-SHELL排序

    原始地址:C / C++算法学习笔记(8)-SHELL排序 基本思想 先取一个小于n的整数d1作为第一个增量(gap),把文件的全部记录分成d1个组.所有距离为dl的倍数的记录放在同一个组中.先在各组 ...

  8. 算法学习之BFS、DFS入门

    算法学习之BFS.DFS入门 0x1 问题描述 迷宫的最短路径 给定一个大小为N*M的迷宫.迷宫由通道和墙壁组成,每一步可以向相邻的上下左右四格的通道移动.请求出从起点到终点所需的最小步数.如果不能到 ...

  9. 二次剩余Cipolla算法学习笔记

    对于同余式 \[x^2 \equiv n \pmod p\] 若对于给定的\(n, P\),存在\(x\)满足上面的式子,则乘\(n\)在模\(p\)意义下是二次剩余,否则为非二次剩余 我们需要计算的 ...

  10. Manacher算法学习笔记 | LeetCode#5

    Manacher算法学习笔记 DECLARATION 引用来源:https://www.cnblogs.com/grandyang/p/4475985.html CONTENT 用途:寻找一个字符串的 ...

随机推荐

  1. cpp智能指针

    weak_ptr<Cls1> wp1; { shared_ptr<Cls1> ptr1(new Cls1);//共享指针 wp1 = ptr1;//临时共享指针 std::co ...

  2. Java中谈尾递归--尾递归和垃圾回收的比较

    一.首先我们讲讲递归 1.递归的本质是,某个方法中调用了自身,本质还是调用了一个方法,只是这个方法正好是自身而已 2.递归因为是在自身中调用自身,所以会带来以下三个显著特点:    1.调用的是同一个 ...

  3. Xpath初了解

    如下一段html: <html> <body> <form id="loginForm"> <input name="usern ...

  4. tomcat 优化建议

    下面给出的是tomcat的优化建议,如果不同意见请留言. 上配置: tomcat jmx配置访问:修改catalina.sh CATALINA_OPTS="$CATALINA_OPTS -D ...

  5. 在deepin 15.5中安装vs code并配置c/c++环境

    原文地址:https://blog.csdn.net/DefetC/article/details/79946100 参考了以下几篇文章: https://www.zhihu.com/question ...

  6. 转-filebeat 源码分析

    背景 在基于elk的日志系统中,filebeat几乎是其中必不可少的一个组件,例外是使用性能较差的logstash file input插件或自己造个功能类似的轮子:). 在使用和了解filebeat ...

  7. mysql join on and

    2018-6-4 10:28:50 星期一 开发中一直在用 left join, 心中只有一丝丝的了解, 还都是学校里学的, 今天看了几遍文章这里记录一下 sql的left join .right j ...

  8. js 禁止f12、Ctrl +S 、右键

    <script language=javascript> window.onload=function(){ document.onkeydown=function(){ ]; ){ re ...

  9. js的事件冒泡,事件捕获

      addEventListener() 方法可以指定 "useCapture" 参数来设置传递事件类型:false→冒泡       true→捕获       默认false. ...

  10. 什么是内部类? Static Nested Class 和 Inner Class 的不同。

    什么是内部类? Static Nested Class 和 Inner Class 的不同.        内部类就是在一个类的内部定义的类,内部类中不能定义静态成员(静态成员不是对象的特性,只是为了 ...