* 参照php标准库设计接口

http://php.net/manual/en/class.spldoublylinkedlist.php

* 反转单向链表

reverse方法, 其他的方法为了方便测试

<?php
/**
* Created by PhpStorm.
* User: Mch
* Date: 8/11/18
* Time: 00:25
*/
class Node {
public $value;
public $next;
public function __construct($data) {
$this->value = $data;
$this->next = null;
}
} class LinkedList implements Iterator, Countable {
private $head;
private $cur; public function __construct() {
$this->head = $this->cur = null;
} public function isEmpty() {
return is_null($this->head);
} public function count() {
$p = $this->head;
$count = 0;
while ($p !== null) {
$p = $p->next;
$count++;
}
return $count;
}
public function current() {
if ($this->isEmpty()) {
return null;
}
return $this->cur->value;
}
public function next() {
if (is_null($this->cur)) {
return null;
}
$this->cur = $this->cur->next;
}
public function rewind() {
$this->cur = $this->head;
} public function valid() {
return !is_null($this->cur);
}
public function key() {
$p = $this->head;
$key = 0;
while ($p !== $this->cur) {
$p = $p->next;
$key++;
}
return $key;
}
public function push($value) {
if ($this->isEmpty()) {
$this->head = new Node($value);
$this->cur = $this->head;
} else {
$this->cur = $this->head;
while ($this->cur->next !== null) {
$this->cur = $this->cur->next;
}
$this->cur->next = new Node($value);
}
return $this;
} public function forEach(callable $callback, mixed $userdata = null) {
$this->rewind();
while($this->valid()) {
call_user_func($callback, $this->current(), $this->key(), $userdata);
$this->next();
}
} public function reverse() {
if ($this->isEmpty())
return;
if ($this->count() < 2)
return;
$prev = null;
$cur = $this->head;
while ($cur) {
// save next
$next = $cur->next;
// move cur to head
$this->head = $cur;
$cur->next = $prev;
// iterate
$prev = $cur;
$cur = $next;
}
} }

  

* test

$list = new LinkedList();

for ($i = 65; $i < 91; $i++) {
$list->push(chr($i));
} $list->forEach(function($value, $index) {
printf("[%d] => %s<br />", $index, $value);
});
echo '-------------------<br />';
$list->reverse();
$list->forEach(function($value, $index) {
printf("[%d] => %s<br />", $index, $value);
});

  

* output:

[0] => A
[1] => B
[2] => C
[3] => D
[4] => E
[5] => F

....

-------------------
[0] => Z
[1] => Y
[2] => X
[3] => W
[4] => V
[5] => U
...

最新文章

  1. 关于laravel基础知识
  2. OpenStack学习参考
  3. 在.NET使用JSON作为数据交换格式
  4. NYOJ 77 开灯问题
  5. Java程序编译和运行的过程【转】
  6. android如何用adb shell启动应用程序
  7. Lua 迭代器
  8. [POJ2259]Team Queue (队列,模拟)
  9. #WEB安全基础 : HTTP协议 | 0x14 HTTP的详细安全问题
  10. java中接口和继承的区别
  11. tp3.2 事务
  12. C# 自定义等待窗口
  13. python2.7安装beautifulsoup包
  14. Object类--equals方法
  15. 登录sqlplus 后,显示问号 ????
  16. 用laravel dingo/api创建简单的api
  17. Cassandra的commitLog、memtable、 SStable
  18. wpf 使用Font-Awesome图标字体
  19. JavaSE——TCP协议网络编程(一)
  20. Codeforces Round #550 (Div. 3) E. Median String (模拟)

热门文章

  1. S3C2440—6.串口的printf实现
  2. Spring-Boot注入自定义properties文件配置
  3. ES6中新增的数组知识记录
  4. ASP.NET Core教程:在ASP.NET Core中使用HttPClient调用WebService
  5. 【springboot】过滤器、监听器、拦截器,Aspect切片
  6. 使用DOM方法来遍历一个文档
  7. SpringBoot博客开发之AOP日志处理
  8. T-SQL - query03_去重查询|模糊查询|排序|分组|使用函数
  9. MySQL大数据迁移备份
  10. Tolist案例(父子传参实现增删改)