php双向链表+性能,PHP双向链表定义与用法示例

本文实例讲述了PHP双向链表定义与用法。分享给大家供大家参考,具体如下:

由于需要对一组数据多次进行移动操作,所以写个双向链表。但对php实在不熟悉,虽然测试各个方法没啥问题,就是不知道php语言深层的这些指针和unset有什么注意的地方,贴出来让大家教育吧。效率没测试....求谅解~

/**

* **双向链表

* @author zhiyuan12@

*/

/**

* 链表元素结点类

*/

class Node_Element {

public $pre = NULL; // 前驱

public $next = NULL; // 后继

public $key = NULL; // 元素键值

public $data = NULL; // 结点值

function __Construct($key, $data) {

$this->key = $key;

$this->data = $data;

}

}

/**

* 双向链表类

*/

class DoubleLinkedList {

private $head; // 头指针

private $tail; // 尾指针

private $current; // 当前指针

private $len; // 链表长度

function __Construct() {

$this->head = self::_getNode ( null, null );

$this->curelement = $this->head;

$this->tail = $this->head;

$len = 0;

}

/**

* @ desc: 读取链表全部结点

*/

public function readAll() {

$tmp = $this->head;

while ( $tmp->next !== null ) {

$tmp = $tmp->next;

var_dump ( $tmp->key, $tmp->data );

}

}

public function move($pos1, $pos2) {

$pos1Node = $this->findPosition ( $pos1 );

$pos2Node = $this->findPosition ( $pos2 );

if ($pos1Node !== null && $pos2Node !== null) {

$tmpKey = $pos1Node->key;

$tmpData = $pos1Node->data;

$pos1Node->key = $pos2Node->key;

$pos1Node->data = $pos2Node->data;

$pos2Node->key = $tmpKey;

$pos2Node->data = $tmpData;

return true;

}

return false;

}

/**

* @ desc: 在指定关键词删除结点

*

* @param : $key

* 指定位置的链表元素key

*/

public function delete($key) {

$pos = $this->find ( $key );

if ($pos !== null) {

$tmp = $pos;

$last = null;

$first = true;

while ( $tmp->next !== null && $tmp->next->key === $key ) {

$tmp = $tmp->next;

if (! $first) {

$this->delNode ( $last );

} else {

$first = false;

}

$last = $tmp;

}

if ($tmp->next !== null) {

$pos->pre->next = $tmp->next;

$tmp->next->pre = $pos->pre;

} else {

$pos->pre->next = null;

}

$this->delNode ( $pos );

$this->delNode ( $tmp );

}

}

/**

* @ desc: 在指定位置删除结点

*

* @param : $key

* 指定位置的链表元素key

*/

public function deletePosition($pos) {

$tmp = $this->findPosition ( $pos );

if ($tmp === null) {

return true;

}

if ($tmp === $this->getTail ()) {

$tmp->pre->next = null;

$this->delNode ( $tmp );

return true;

}

$tmp->pre->next = $tmp->next;

$tmp->next->pre = $tmp->pre;

$this->delNode ( $tmp );

}

/**

* @ desc: 在指定键值之前插入结点

*

* @param : $key

* //指定位置的链表元素key

* @param : $data

* //要插入的链表元素数据

* @param : $flag

* //是否顺序查找位置进行插入

*/

public function insert($key, $data, $flag = true) {

$newNode = self::_getNode ( $key, $data );

$tmp = $this->find ( $key, $flag );

if ($tmp !== null) {

$newNode->pre = $tmp->pre;

$newNode->next = $tmp;

$tmp->pre = $newNode;

$newNode->pre->next = $newNode;

} else {

$newNode->pre = $this->tail;

$this->tail->next = $newNode;

$this->tail = $newNode;

}

$this->len ++;

}

/**

* @ desc: 在指定位置之前插入结点

*

* @param : $pos

* 指定插入链表的位置

* @param : $key

* 指定位置的链表元素key

* @param : $data

* 要插入的链表元素数据

*/

public function insertPosition($pos, $key, $data) {

$newNode = self::_getNode ( $key, $data );

$tmp = $this->findPosition ( $pos );

if ($tmp !== null) {

$newNode->pre = $tmp->pre;

$newNode->next = $tmp;

$tmp->pre = $newNode;

$newNode->pre->next = $newNode;

} else {

$newNode->pre = $this->tail;

$this->tail->next = $newNode;

$this->tail = $newNode;

}

$this->len ++;

return true;

}

/**

* @ desc: 根据key值查询指定位置数据

*

* @param : $key

* //指定位置的链表元素key

* @param : $flag

* //是否顺序查找

*/

public function find($key, $flag = true) {

if ($flag) {

$tmp = $this->head;

while ( $tmp->next !== null ) {

$tmp = $tmp->next;

if ($tmp->key === $key) {

return $tmp;

}

}

} else {

$tmp = $this->getTail ();

while ( $tmp->pre !== null ) {

if ($tmp->key === $key) {

return $tmp;

}

$tmp = $tmp->pre;

}

}

return null;

}

/**

* @ desc: 根据位置查询指定位置数据

*

* @param : $pos

* //指定位置的链表元素key

*/

public function findPosition($pos) {

if ($pos <= 0 || $pos > $this->len)

return null;

if ($pos < ($this->len / 2 + 1)) {

$tmp = $this->head;

$count = 0;

while ( $tmp->next !== null ) {

$tmp = $tmp->next;

$count ++;

if ($count === $pos) {

return $tmp;

}

}

} else {

$tmp = $this->tail;

$pos = $this->len - $pos + 1;

$count = 1;

while ( $tmp->pre !== null ) {

if ($count === $pos) {

return $tmp;

}

$tmp = $tmp->pre;

$count ++;

}

}

return null;

}

/**

* @ desc: 返回链表头节点

*/

public function getHead() {

return $this->head->next;

}

/**

* @ desc: 返回链表尾节点

*/

public function getTail() {

return $this->tail;

}

/**

* @ desc: 查询链表节点个数

*/

public function getLength() {

return $this->len;

}

private static function _getNode($key, $data) {

$newNode = new Node_Element ( $key, $data );

if ($newNode === null) {

echo "new node fail!";

}

return $newNode;

}

private function delNode($node) {

unset ( $node );

$this->len --;

}

}

$myList = new DoubleLinkedList ();

$myList->insert ( 1, "test1" );

$myList->insert ( 2, "test2" );

$myList->insert ( "2b", "test2-b" );

$myList->insert ( 2, "test2-c" );

$myList->insert ( 3, "test3" );

$myList->insertPosition ( 5, "t", "testt" );

$myList->readAll ();

echo "+++";

$myList->deletePosition(0);

$myList->readAll ();

echo "..." . $myList->getLength ();

var_dump ( $myList->findPosition ( 3 )->data );

?>

运行结果:

int(1)

string(5) "test1"

int(2)

string(7) "test2-c"

int(2)

string(5) "test2"

string(2) "2b"

string(7) "test2-b"

string(1) "t"

string(5) "testt"

int(3)

string(5) "test3"

+++int(1)

string(5) "test1"

int(2)

string(7) "test2-c"

int(2)

string(5) "test2"

string(2) "2b"

string(7) "test2-b"

string(1) "t"

string(5) "testt"

int(3)

string(5) "test3"

...6string(5) "test2"

希望本文所述对大家PHP程序设计有所帮助。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/539556.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

反击爬虫,前端工程师的脑洞可以有多大?

对于一张网页&#xff0c;我们往往希望它是结构良好&#xff0c;内容清晰的&#xff0c;这样搜索引擎才能准确地认知它。 而反过来&#xff0c;又有一些情景&#xff0c;我们不希望内容能被轻易获取&#xff0c; 前言 比方说电商网站的交易额&#xff0c;教育网站的题目等。因为…

Spring与Struts框架整合

Spring&#xff0c;负责对象对象创建 Struts&#xff0c;用Action处理请求 Spring与Struts框架整合&#xff0c;关键点&#xff1a;让struts框架action对象的创建&#xff0c;交给spring完成&#xff01; 1.步骤&#xff1a; 引入jar文件 1&#xff09;引入struts .jar相关文件…

esxi能直通的显卡型号_显卡刷bios教程

一般来说显卡默认的出厂bios就已经很稳定&#xff0c;如果没有特殊情况下建议不要刷显卡bios。一般而言部分网友刷显卡BIOS目的是开核或超频&#xff0c;那么对于一个不会刷显卡bios的网友来说肯定会问显卡怎么刷bios类似的问题&#xff0c;那么本文这里就说一下有关显卡怎么刷…

关于Linux网卡调优之:RPS (Receive Packet Steering)

昨天在查LVS调度均衡性问题时&#xff0c;最终确定是 persistence_timeout 参数会使用IP哈希。目的是为了保证长连接&#xff0c;即一定时间内访问到的是同一台机器。而我们内部系统&#xff0c;由于出口IP相对单一&#xff0c;所以总会被哈希到相同的RealServer。 过去使用LVS…

footer.php置底,CSS五种方式实现Footer置底

页脚置底(Sticky footer)就是让网页的footer部分始终在浏览器窗口的底部。当网页内容足够长以至超出浏览器可视高度时&#xff0c;页脚会随着内容被推到网页底部&#xff1b;但如果网页内容不够长&#xff0c;置底的页脚就会保持在浏览器窗口底部。方法一&#xff1a;将内容部分…

安卓adapter适配器作用_自带安卓系统的便携屏,能玩出什么花样?

之前说到去年出差太多&#xff0c;平常就把便携屏带上了。之前也说了如果是像笔者这样的出差狗也知道&#xff0c;托运需要提前去机场一路着急忙慌&#xff0c;不托运只需要打印登机牌(纸质才给报销)排队安检登机就完了。有的时候可以把标准显示器来回寄&#xff0c;只要包装强…

Gradle插件学习笔记(二)

之前介绍了Gradle插件的开发&#xff0c;这次会对功能进行一部分拓展&#xff0c;建议没有读过第一篇文章的朋友&#xff0c;先看一下Gradle插件学习笔记&#xff08;一&#xff09; Extension 之前的文章提到过&#xff0c;如何编写一个插件&#xff0c;但是并不能通过外面传递…

php抽象类继承抽象类,PHP面向对象程序设计高级特性详解(接口,继承,抽象类,析构,克隆等)...

本文实例讲述了PHP面向对象程序设计高级特性。分享给大家供大家参考&#xff0c;具体如下&#xff1a;静态属性class StaticExample {static public $aNum 0; // 静态共有属性static public function sayHello() { // 静态共有方法print "hello";}}print StaticExam…

Typora markdown公式换行等号对齐_Typora编写博客格式化文档的最佳软件

Typora-编写博客格式化文档的最佳软件Typora 不仅是一款支持实时预览的 Markdown 文本编辑器&#xff0c;而且还支持数学公式、代码块、思维导图等功能。它有 OS X、Windows、Linux 三个平台的版本&#xff0c;是完全免费的。作为技术人员或者专业人员&#xff0c;使用Markdown…

Bootstrap静态cdn

百度的静态资源库的 CDN 服务http://cdn.code.baidu.com/ &#xff0c;访问速度更快、加速效果更明显、没有速度和带宽限制、永久免费,引入代码如下&#xff1a; <!-- 新 Bootstrap 核心 CSS 文件 --> <link href"http://apps.bdimg.com/libs/bootstrap/3.3.0/…

php复习,PHP排序算法的复习和总结

直接上代码吧&#xff01;/** 插入排序(一维数组)* 每次将一个待排序的数据元素&#xff0c;插入到前面已经排好序的数列中的适当的位置&#xff0c;使数列依然有序&#xff1b;直到待排序的数据元素全部插入完成为止。*/function insertSort($arr){if(!is_array($arr) || coun…

docker-machine

vbox安装 sudo /sbin/vboxconfig &#xfffc; yum install gcc make yum install kernel-devel-3.10.0-514.26.2.el7.x86_64 转载于:https://www.cnblogs.com/yixiaoyi/p/dockermachine.html

intention lock_写作技巧:你写出来的情节有用吗?好情节的原则——LOCK系统

读者喜欢一本小说的原因只有一个&#xff1a;很棒的故事。——Donald Maass来&#xff0c;话筒对准这位小作家&#xff0c;请问你是如何构思故事的&#xff1f;是习惯于现在脑海中把故事都想好了&#xff0c;才开始写作&#xff1f;还是习惯于临场发挥&#xff0c;喜欢一屁股坐…

zookeeper基本操作

1.客户端连接 [txtest1 bin]$ jps 23433 Jps 23370 QuorumPeerMain #zookeeper进程[txtest1 bin]$ ./zkCli.sh -server test1:2182 Connecting to test1:2182 2018-01-24 23:42:09,024 [myid:] - INFO [main:Environment100] - Client environment:zookeeper.version3.4.5-…

sqllite java 密码,SQLite登录检查用户名和密码

我正在创建一个应用程序(使用Java和SQLite)(JFrame&#xff0c;使用Netbeans)我有我想要登录的用户 . (我有所有正确的包JDBC&#xff0c;SQLite等)我遇到的问题似乎是获取用户名/密码来检查我的users.db文件..我正在使用Java和SQLite . 我也在使用JDBC .我的一些代码作为一个例…

springmvc与struts2的区别

1&#xff09;springmvc的入口是一个servlet&#xff0c;即前端控制器&#xff0c;例如&#xff1a;*.action struts2入口是一个filter过虑器&#xff0c;即前端过滤器&#xff0c;例如&#xff1a;/* 2&#xff09;springmvc是基于方法开发&#xff0c;传递参数是通过方法形…

power designer数据流图_鲲云公开课 | 三分钟带你了解数据流架构

目前&#xff0c;市场上的芯片主要包括指令集架构和数据流架构两种实现方式。指令集架构主要包括X86架构、ARM架构、精简指令集运算RISC-V开源架构&#xff0c;以及SIMD架构。总体来说&#xff0c;四者都属于传统的通用指令集架构。传统的指令集架构采用冯诺依曼计算方式&#…

onCreate源码分析

原文地址Android面试题-onCreate源码都没看过&#xff0c;怎好意思说自己做android Activity扮演了一个界面展示的角色&#xff0c;堪称四大组件之首&#xff0c;onCreate是Activity的执行入口&#xff0c;都不知道入口到底干了嘛&#xff0c;还学什么android,所以本文会从源码…

linux php环境搭建教程,linux php环境搭建教程

linux php环境搭建的方法&#xff1a;首先获取相关安装包&#xff1b;然后安装Apache以及mysql&#xff1b;接着修改配置文件“httpd.conf”&#xff1b;最后设置环境变量和开机自启&#xff0c;并编译安装PHP即可。一、获取安装包PHP下载地址&#xff1a;http://cn.php.net/di…

Apache2.2与Tomcat7集成方案详解

原文地址&#xff1a;http://my.oschina.net/u/919173/blog/159206 ------------------------------------ 首先谈一下为什么要集成Apache和tomcat7&#xff1f; Apache是当前使用最为广泛的WWW服务器软件&#xff0c;具有相当强大的静态HTML处理的能力。 Tomcat服务器是一个…