-
ArrayList数组和LinkedList双向链表效率问题的比较--两个方面:访问元素和插入元素
2020-07-27 13:25:35ArrayList数组 物理结构连续,并且有自己的下标,因而访问起来效率高,但也因其物理结构连续,插入元素...—补充:LinkedList双向链表的特点是端口和尾端都能进能出,所以从端口和尾端无论是访问还是添加/删除,效率都比从中间ArrayList数组 物理结构连续,并且有自己的下标,因而访问起来效率高,但也因其物理结构连续,插入元素时插入位置后的元素都要后移一位,删除同理,故而添加/删除元素的效率不高
LinkedList双向链表,列表中的每个节点都包含了对前一个和后一个元素的引用.访问效率低于ArrayList数组,但添加/删除元素只需要改变前后两个节点,故添加/删除元素的效率高于ArrayList数组.
—补充:LinkedList双向链表的特点是端口和尾端都能进能出,所以从端口和尾端无论是访问还是添加/删除,效率都比从中间开始的高总结:查询用ArrayList,添加/删除用LinkedList
代码验证:
public class Num1ArrayVSLinked { public static void main(String[] args) { //ArrayList数组和LinkedList双向链表效率问题的比较--两个方面:取元素,插入元素 //1.取元素 ArrayList<String> arr = new ArrayList<>(); LinkedList<String> lin = new LinkedList<>(); for (int i = 0; i < 10000000; i++) { arr.add(i, ""); lin.add(i, ""); } //1.1 从列表的头部开始访问 long time1 = System.currentTimeMillis(); arr.get(10); long time2 = System.currentTimeMillis(); System.out.println("ArrayList从列表的头部开始访问的时间:" + (time2 - time1));//0 long time3 = System.currentTimeMillis(); lin.get(10); long time4 = System.currentTimeMillis(); System.out.println("LinkedList从列表的头部开始访问的时间:" + (time4 - time3));//0 //1.2 从列表的中间开始访问 long time5 = System.currentTimeMillis(); arr.get(5000000); long time6 = System.currentTimeMillis(); System.out.println("ArrayList从列表的中间开始访问的时间:" + (time6 - time5));//0 long time7 = System.currentTimeMillis(); lin.get(5000000); long time8 = System.currentTimeMillis(); System.out.println("LinkedList从列表的中间开始访问的时间:" + (time8 - time7));//31 //1.3 从列表的尾部开始访问 long time9 = System.currentTimeMillis(); arr.get(9999999); long time10 = System.currentTimeMillis(); System.out.println("ArrayList从列表的尾部开始访问的时间:" + (time10 - time9));//0 long time11 = System.currentTimeMillis(); lin.get(9999999); long time12 = System.currentTimeMillis(); System.out.println("LinkedList从列表的尾部开始访问的时间:" + (time12 - time11));//0 //2.插入元素 //2.1 从列表的头部开始插入 long time01 = System.currentTimeMillis(); arr.add(10,"3"); long time02 = System.currentTimeMillis(); System.out.println("ArrayList从列表的头部开始插入的时间:" + (time02 - time01));//547 long time03 = System.currentTimeMillis(); lin.add(10,"3"); long time04 = System.currentTimeMillis(); System.out.println("LinkedList从列表的头部开始插入的时间:" + (time04 - time03));//0 //2.2 从列表的中间开始插入 long time05 = System.currentTimeMillis(); arr.add(5000000,"3"); long time06 = System.currentTimeMillis(); System.out.println("ArrayList从列表的中间开始插入的时间:" + (time06 - time05));//46 long time07 = System.currentTimeMillis(); lin.add(5000000,"3"); long time08 = System.currentTimeMillis(); System.out.println("LinkedList从列表的中间开始插入的时间:" + (time08 - time07));//47 //2.3 从列表的尾部开始插入 long time09 = System.currentTimeMillis(); arr.add(9999999,"3"); long time010 = System.currentTimeMillis(); System.out.println("ArrayList从列表的尾部开始插入的时间:" + (time010 - time09));//0 long time011 = System.currentTimeMillis(); lin.add(9999999,"3"); long time012 = System.currentTimeMillis(); System.out.println("LinkedList从列表的尾部开始插入的时间:" + (time012 - time011));//0 } }
-
php双向链表,PHP双向链表使用详解
2021-04-08 08:30:19由于需要对一组数据多次进行移动操作,所以写个双向链表。但对php实在不熟悉,虽然测试各个方法没啥问题,就是不知道php语言深层的这些指针和unset有什么注意的地方,贴出来让大家教育吧。效率没测试....求谅解~...这次给大家带来PHP双向链表使用详解,PHP双向链表使用的注意事项有哪些,下面就是实战案例,一起来看一下。
由于需要对一组数据多次进行移动操作,所以写个双向链表。但对php实在不熟悉,虽然测试各个方法没啥问题,就是不知道php语言深层的这些指针和unset有什么注意的地方,贴出来让大家教育吧。效率没测试....求谅解~<?php
/**
* **双向链表
* @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中文网其它相关文章!
推荐阅读:
-
php双向链表+性能,PHP双向链表基础详解
2021-03-25 10:00:43本文主要和大家介绍PHP双向链表定义与用法,涉及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双向链表+性能,PHP双向链表定义与用法示例
2021-03-25 10:01:54分享给大家供大家参考,具体如下:由于需要对一组数据多次进行移动操作,所以写个双向链表。但对php实在不熟悉,虽然测试各个方法没啥问题,就是不知道php语言深层的这些指针和unset有什么注意的地方,贴出来让大家...本文实例讲述了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相关内容感兴趣的读者可查看本站专题:《PHP数据结构与算法教程》、《php程序设计算法总结》、《php字符串(string)用法总结》、《PHP数组(Array)操作技巧大全》、《PHP常用遍历算法与技巧总结》及《PHP数学运算技巧总结》
希望本文所述对大家PHP程序设计有所帮助。
-
双向链表
2018-09-27 02:44:00链表是实现了数据之间保持逻辑顺序,但存储空间不连续的数据结构。...双向链表新增和删除元素效率较高,因为链表会记录前一个节点和后一个节点。 class Node: def __init__(self, item, next=None, prev=None): ... -
双向链表怎么提高插队效率及实现倒排
2018-04-12 15:08:09碰到个面试题,是关于双向链表插队效率的 具体题目要求是: 实现一个双向链表的倒置功能(1->2->3 变成 3->2->1) ,请勿直接使用jdk的linkedlist 请问大神们,有什么好的java实现? -
双向链表链表
2017-08-15 10:42:51优点:使用方便 ,查询效率 比链表高,内存为一连续的区域 缺点:大小固定,不适合动态存储,不方便动态添加 链表: 优点:可动态添加删除 大小可变 缺点:只能通过顺次指针访问,查询效率低 而... -
双向链表实现
2018-04-08 23:17:34双向链表:对比单向链表,双向链表中,一个结点不仅知道他的后继,还知道他的前驱,这就使得双向链表在操作上比单项链表更为简单,效率更高注:头结点的data无任何意义,所以填0即可空的双向链表(head 的 prev 和 ... -
双向链表示意图_双向链表及其创建(C语言)详解
2021-01-14 15:00:08目前我们所学到的单向链表(或单链表)。虽然使用单链表能 100% 解决逻辑关系为 "一对一" 数据的存储问题,但在解决某些特殊...对于逆向查找(从后往前)相关的问题,使用本节讲解的双向链表,会更加事半功倍。双向链... -
C++双向链表
2019-11-30 11:25:10双向链表顾名思义就是每个结点具有两个指针,一个指向前一个结点,一个指向后一个结点,我们不必拘束与单链表的创建遍历操作等,这样大大减少了使用中存在的效率问题。 在单链表中,我们有一个数据域还有一个指针... -
Go 双向链表
2019-06-01 17:45:47双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点,相对于单链表来讲:往前往后... -
delphi 双向链表,单向链表,可以替换TList,添加删除效率更高
2020-05-07 17:20:111、TList插入删除内存操作过于频繁,效率不高,可用以于...(包括:单线链表,双向链表,队列) //链表节点 pTSingleLinkedNode = ^TSingleLinkedNode; TSingleLinkedNode = record private Next: pTSingl... -
双向链表和环形链表(单向和双向)约瑟夫环实例
2021-03-05 16:11:23双向链表 数组优缺点:根据下标直接查找和修改,增删需要移动后续数据,效率低。 单向链表的缺点:增删快速,查找需要从头遍历,效率低。 双线链表可以向前或向后查找。 单向链表查找的方向只能是一个方向(双向... -
Python单向链表和双向链表原理与用法实例详解
2021-01-20 04:33:42本文实例讲述了Python单向链表和双向链表原理与用法。分享给大家供大家参考,具体如下: 链表是一种数据结构,链表在循环遍历的时候效率不高,但是在插入和删除时优势比较大。 链表由一个个节点组成。 单向链表的... -
C语言实现双向链表
2016-02-12 14:05:22使用双向链表实现,双向链表具有的单向链表所不具备的优势,那就是可以双向遍历,这对增、删操作的效率都有显著的提高。而弊端则是造成空间效率的下降,即需要额外的指针来维持双向链表之间的连接关系。链表的实现中... -
Iterator的作用以及遍历LinkedList双向链表的效率
2014-12-23 16:44:02Iterator模式是用于遍历集合类的标准访问方法。它可以把访问逻辑从不同类型的集合类中抽象出来,从而避免向客户端...而访问一个链表(LinkedList)又必须使用while循环: while((e=e.next())!=null) { ... e.data -
C语言数据结构之双向链表
2021-02-18 20:15:23为了高效率的解决类似问题,便有了双向链表。 它由前驱指针域和后驱指针域还有数据域构成 typedef struct line{ struct line *prior; int date; struct line *next; }NODE; 二、双向链表的创建 与单向链表相比... -
链表的java实现(单向、双向链表,单向链表的反转)
2021-02-22 21:10:02链表链表单向链表单向链表API设计单向链表代码实现双向链表结点API设计双向链表API设计双向链表代码实现链表的复杂度分析链表反转快慢指针中间值问题有向链表是否有环问题有环链表入口问题循环链表约瑟夫问题 ... -
封装链表和双向链表
2020-08-08 14:50:03尾添加的效率低,非法下标的判断也非常低。 1、单链表 节点: 数据域 指针域 数据项: 头指针 尾指针 节点数量 2、静态链表 节点: 数据域 游标 静态链表的节点存储在连续的内存,通过游戏来访问下一个节点。 这种... -
PHP双向链表定义与用法示例
2020-12-20 03:13:12由于需要对一组数据多次进行移动操作,所以写个双向链表。但对php实在不熟悉,虽然测试各个方法没啥问题,就是不知道php语言深层的这些指针和unset有什么注意的地方,贴出来让大家教育吧。效率没测试….求谅解~ &... -
双向链表的相关操作
2020-08-07 16:42:42双向链表的相关操作 主要思想 单链表的缺点是:只能顺序遍历,当查找或修改某个节点时,有时候效率会很低, 因此,双向链表的出现,解决了这个问题。 代码实现 1.节点类 package DoubleLinkList; /** * @program... -
数据结构之链表的进阶——双向链表
2020-09-08 09:07:40双向链表 上一章提到了链表,表中各节点均只有一个指针,且都统一指向直接后继节点,通常这类链表称为单向链表(或单链表)。 虽然单链表能100%解决“一对一”数据的存储问题,但是在解决某些特殊问题时,单链表并... -
python3实现双向链表图文详解
2020-07-09 15:04:31这一节我们就将其改进版本双向链表也来实现一下。当然单向链表的改进并不只有双向链表这一种,在以后的文章中我们再实现下单向循环链表甚至双向循环链表。 文章目录双向链表python3实现结点类链表类...