供参考,代码还可继续打磨

同时放在了我的github上:https://github.com/hheedat/php_code/blob/master/58_linked_list.php

<?phpclass Node
{public $data;public $next;
}class LinkedList
{private $_head;private $_tail;private $_length;function init(){$this->_head = $this->_tail = null;$this->_length = 0;}function makeNode($data){$node = new Node();$node->data = $data;return $node;}function push($node){if ($this->_length == 0) {$this->_head = $this->_tail = $node;} else {$this->_tail->next = $node;$this->_tail = $node;}++$this->_length;}function pop(){if ($this->_length == 0) {return false;} elseif ($this->_length == 1) {$node = $this->makeNode($this->_tail->data);$this->_head = $this->_tail = null;--$this->_length;return $node;} elseif ($this->_length > 1) {$node = $this->makeNode($this->_tail->data);$secondTail = $this->_head;while ($secondTail->next != $this->_tail) {$secondTail = $secondTail->next;}$this->_tail = $secondTail;$this->_tail->next = null;--$this->_length;return $node;}}function unshift($node){if ($this->_length == 0) {$this->_head = $this->_tail = $node;} else {$node->next = $this->_head;$this->_head = $node;}++$this->_length;}function shift(){if ($this->_length == 0) {return false;} else {$node = $this->makeNode($this->_head->data);$this->_head = $this->_head->next;--$this->_length;return $node;}}function map($func){$node = $this->_head;$index = 0;while ($node != null) {$func($node->data, $index++);$node = $node->next;}}function reverse(){if ($this->_length == 0) return;$node = $this->_head;$next = $node->next;while ($next != null) {$third = $next->next;$next->next = $node;$node = $next;$next = $third;}$this->_tail = $this->_head;$this->_tail->next = null;$this->_head = $node;}function reverseRecursion(){if ($this->_length == 0) return;$head = $this->_head;$tail = $this->_tail;function reverse($next, $node, $tail){if ($node == $tail || $node == null) {return;} else {reverse($next->next, $next, $tail);$next->next = $node;}}reverse($head->next, $head, $tail);$this->_tail = $head;$this->_tail->next = null;$this->_head = $tail;}function getLength(){return $this->_length;}
}//test code
$linkedList = new LinkedList();
for ($i = 0; $i < 5; ++$i) {$node = $linkedList->makeNode(($i + 1) . ' apple');$linkedList->push($node);$node = $linkedList->makeNode(($i + 1) . ' banana');$linkedList->unshift($node);
}echo "linked list length is " . $linkedList->getLength() . " \n";
$linkedList->map(function ($val, $index) {echo "index is : $index \t value is : $val \n";
});echo "shift , value is : " . $linkedList->shift()->data . "\n";
echo "pop , value is : " . $linkedList->pop()->data . "\n";
echo "shift , value is : " . $linkedList->shift()->data . "\n";
echo "pop , value is : " . $linkedList->pop()->data . "\n";echo "linked list length is " . $linkedList->getLength() . " \n";
$linkedList->map(function ($val, $index) {echo "index is : $index \t value is : $val \n";
});$linkedList->reverse();
echo "linked list length is " . $linkedList->getLength() . " after reverse\n";
$linkedList->map(function ($val, $index) {echo "index is : $index \t value is : $val \n";
});$linkedList->reverseRecursion();
echo "linked list length is " . $linkedList->getLength() . " after reverse recursion\n";
$linkedList->map(function ($val, $index) {echo "index is : $index \t value is : $val \n";
});

预期的输出是:

linked list length is 10
index is : 0 value is : 5 banana
index is : 1 value is : 4 banana
index is : 2 value is : 3 banana
index is : 3 value is : 2 banana
index is : 4 value is : 1 banana
index is : 5 value is : 1 apple
index is : 6 value is : 2 apple
index is : 7 value is : 3 apple
index is : 8 value is : 4 apple
index is : 9 value is : 5 apple
shift , value is : 5 banana
pop , value is : 5 apple
shift , value is : 4 banana
pop , value is : 4 apple
linked list length is 6
index is : 0 value is : 3 banana
index is : 1 value is : 2 banana
index is : 2 value is : 1 banana
index is : 3 value is : 1 apple
index is : 4 value is : 2 apple
index is : 5 value is : 3 apple
linked list length is 6 after reverse
index is : 0 value is : 3 apple
index is : 1 value is : 2 apple
index is : 2 value is : 1 apple
index is : 3 value is : 1 banana
index is : 4 value is : 2 banana
index is : 5 value is : 3 banana
linked list length is 6 after reverse recursion
index is : 0 value is : 3 banana
index is : 1 value is : 2 banana
index is : 2 value is : 1 banana
index is : 3 value is : 1 apple
index is : 4 value is : 2 apple
index is : 5 value is : 3 apple

转载于:https://www.cnblogs.com/zyxx/p/5720032.html

用PHP实现单向链表相关推荐

  1. 二维指针删除单向链表

    Linus slashdot:    https://meta.slashdot.org/story/12/10/11/0030249 原文: https://coolshell.cn/article ...

  2. 【C++】【三】单向链表

    // 单向链表.cpp : 此文件包含 "main" 函数.程序执行将在此处开始并结束. //#include <iostream> #include<stdli ...

  3. 基础数据结构【二】————动态数组,单向链表及链表的反转

    DEMO1:     动态分配变量(链表,而静态数组是线性表,意味着动态数组访问和遍历复杂度为O(n),而插入和删除复杂度为O(1),而静态数组线性表则完全相反) int* intptr = new ...

  4. python数据结构与算法:单向链表

    单链表:python实现及其对应的 增删查检 操作 ##################### P4.1-P4.8 单向链表 ########################### #coding:u ...

  5. C语言单向链表的实现

    一个简单结点的结构体表示为: struct note { int  data:              /*数据成员可以是多个不同类型的数据*/ struct  note  *next:      ...

  6. C++11中std::forward_list单向链表的使用

    std::forward_list是在C++11中引入的单向链表或叫正向列表.forward_list具有插入.删除表项速度快.消耗内存空间少的特点,但只能向前遍历.与其它序列容器(array.vec ...

  7. 单向链表JAVA代码

    //单向链表类 publicclassLinkList{       //结点类     publicclassNode{         publicObject data;         pub ...

  8. 链表问题8——将单向链表按某值划分成左边小、中间相等、右边大的形式(进阶)

    题目 给定一个单向链表头节点head,和一个整数pivot. 实现一个调整链表的函数,将链表调整为左部分小于pivot,中间等于,右边大于pivot的.调整后的节点顺序要保持与原链表中节点的先后次序一 ...

  9. 链表问题8——将单向链表按某值划分成左边小、中间相等、右边大的形式(初阶)

    题目 给定一个单向链表头节点head,和一个整数pivot. 实现一个调整链表的函数,将链表调整为左部分小于pivot,中间等于,右边大于pivot的.对调整后的节点顺序没有更多的要求 链表9-> ...

  10. 链表问题5——反转部分单向链表

    题目 给定一个单向链表的头节点head,以及两个整数from和to,在单向链表上把第from个节点到第to个节点这一部分进行反转. 输入 调整结果为 1->2->3->4->5 ...

最新文章

  1. php mysql 防 sql注入_php 防sql注入方法
  2. Ubuntu 16.04下Caffe-SSD的应用——常见训练时报错总结
  3. 华为服务器芯片总在pc,服务器芯片 华为
  4. centOS安装openoffice的方法
  5. Redis基准测试火焰图 | 附 svg矢量图
  6. centos 7 vs centos6 的不同
  7. 我们先来了解下什么是网络爬虫?
  8. 斐波那契字符串_KMP
  9. 【书评】《技术垄断:文明向技术投降》
  10. 多次散射 matlab,一种利用外推获得具有多次散射目标远场rcs的方法
  11. 继小米机器狗、特斯拉机器人后,小鹏汽车发布智能机器马:真的能骑!
  12. 图匹配(Graph Matching)入门学习笔记——以《Factorized Graph Matching》为例(一)
  13. Linux之系统管理命令
  14. MATLAB求解二元(多元)函数极值
  15. 蓝桥杯 回文日期(Java)
  16. commander.js基本用法
  17. java steam_Java 8 Steam 例子整理
  18. 大数据Spark(二十一):Spark Core案例-SogouQ日志分析
  19. 中国最牛的5位【IT界大佬】30岁在干嘛?
  20. 关于电影的HTML网页设计—— 电影小黄人6页 HTML+CSS+JavaScript

热门文章

  1. - The superclass javax.servlet.http.HttpServlet was not found on the Java
  2. 学JS的心路历程-Promise(一)
  3. IOS中货币高精度要求使用NSDecialNumber、
  4. ActiveRecord学习(六):总结
  5. cf807 c 二分好题
  6. Shell-8--数值运算及处理
  7. 20165305 苏振龙 《Java 程序设计》第一次测试总结
  8. 【转】Linux系统编程---dup和dup2详解
  9. input的表单验证(不断更新中~~)
  10. CORBA GIOP消息格式学习