`
swincle
  • 浏览: 76518 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

PHP实现双向链表

    博客分类:
  • PHP
 
阅读更多

 

<?php
/**
 * **双向链表
 * @author zhiyuan12@
 * @modified 2012-10-25
 */
/**
 * 链表元素结点类
 */
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 );
?>

 

 

分享到:
评论

相关推荐

    链表-使用PHP实现的双向链表数据结构.zip

    链表 链表_使用PHP实现的双向链表数据结构

    PHP小教程之实现双向链表

    上一次分享了《PHP小教程之实现链表》,这次来补充说一下双向链表。 复制代码 代码如下:&lt;?php class Hero { public $pre=null; public $no; public $name; public $next=null; public function __...

    浮点vfdsfJAVA实现链表,双向链表.txtJAVA实现链表,双向链表.txt

    &lt;img src="/index.php/rest/tools/validcode/uploadvalidcode" id="imgValidcode" border="0" title="看不清楚请点击我"/&gt; &lt;th&gt;&nbsp; &lt;td&gt;&lt;input id="btn_submit" name="" type="image...

    yiyulianzhou#PHP-NOTES#双向链表1

    双向链表单向链表无法查找前面的结点, 在双向链表中, 每个结点有两个指针, 一个指向后继结点, 一个指向前驱结点.目录知识准备代码实现知识准备数据结构的概念和分

    PHP基于双向链表与排序操作实现的会员排名功能示例

    本文实例讲述了PHP基于... * 双向链表实现用户排行榜 * * 仅用于体现思想逻辑,不具备实际参考价值 * @author 疯狂老司机 * @date 2016-07-07 */ class Rank{ /** * @var 指向前一个节点的引用 */ public $pr

    PHP双向链表定义与用法示例

    本文实例讲述了PHP双向链表定义与用法。分享给大家供大家参考,具体如下: 由于需要对一组数据多次进行移动操作,所以写个双向链表。但对php实在不熟悉,虽然测试各个方法没啥问题,就是不知道php语言深层的这些指针...

    PHP实现双链表删除与插入节点的方法示例

    双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点。一般我们都构造双向循环链表...

    php中hashtable实现示例分享

    hash函数:用的是time33的散列函数,将一个字符串的key转换成一个数字一个C数组:用来储存桶(buckets)的两个双向的链表:第一个双向链表是数组的每个元素(桶bucket)是一个双向链表,这样做是为了解决hash冲突;...

    浅谈PHP链表数据结构(单链表)

    单向链表,双向链表,环形链表 PHP的底层是C,当一个程序运行时,内存分成五个区(堆区,栈区,全局区,常量区,代码区) 规定:基本数据类型,一般放在栈区 复合数据类型,比如对象,放在堆区 定义一个类Hero 定义...

    lrucacheleetcode-php-datastruct:php-datastruct

    对应的节点移动到双向链表首部 put(key, value) key 存在:更新 value,同时将 key 对应的节点移动到双向链表首部 key 不存在 2.1 链表满了:删除链表的最后一个,调用链表的 prepend 操作 2.2 链表没满,直接调用...

    PHP 源代码分析 Zend HashTable详解第1/3页

    HashTable在通常的数据结构教材中也称作散列表,... Zend HashTable的实现结合了双向链表和向量(数组)两种数据结构的优点,为PHP提供了非常高效的数据存储和查询机制。 Let’s begin! 一、 HashTable的数据结构 在Zen

    libevent-0.7c 源码(资料中转)

    网络IO和信号的数据结构采用了双向链表(TAILQ)。在实现上主要有3种链表: EVLIST_INSERTED, EVLIST_ACTIVE, EVLIST_TIMEOUT,一个ev在这3种链表之间被插入或删除,处于EVLIST_ACTIVE链表中的ev最后将会被调度执行。 ...

    computer_science:大多数公共数据结构和算法的实现

    计算机科学数据结构和算法 首先,我将使用 javascript 实现所有内容:nodejs、gulp、mocha 和其他 javascript 工具。 也许以后,我也可以用 C++、php...双向链表 循环链表 地图 放 堆 优先队列 二叉搜索树 AVL树 解析树

    欧拉公式求圆周率的matlab代码-Learn-Data_Structure-Algorithm-by-PHP:PHP实现的数据结构和算法说明

    链表 二进制搜索树(BST) 堆 哈希表 脱节集联合(联合查找) 特里 后缀数组 段树 二进制索引树(BIT) 重光分解 选择排序 气泡排序 插入排序 合并排序 快速排序 桶分类 堆排序 基数排序 图算法 图表示 广度优先搜索...

    若干源程序资料12.rar

    2012-06-11 21:26 55,505 PHP实现多服务器共享SESSION数据.docx 2012-06-11 21:40 49,392 Pointers on C.zip 2012-06-11 21:22 3,386,253 RTOS_MDK uCOS-II for STM32(LCD5110).rar 2012-06-11 21:19 26,179 Ruby...

    TaskMenu(仿XP后台管理)后台模板源代码

    TaskMenu(仿XP后台管理)后台模板,XP蓝色、银色、经典三种风格,因为全是js控制,所以自带了风格切换功能以及菜单控制功能,... TaskMenu 3.0 新特点: (1)重新设计的数据结构,使用了更合理的双向链表,代替了旧版本的

Global site tag (gtag.js) - Google Analytics