用PHP实现单向链表
供参考,代码还可继续打磨
同时放在了我的github上:https://github.com/hheedat/php_code/blob/master/58_linked_list.php
<?php class 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
用PHP实现单向链表的更多相关文章
- Reverse Linked List II 单向链表逆序(部分逆序)
0 问题描述 原题点击这里. 将单向链表第m个位置到第n个位置倒序连接.例如, 原链表:1->2->3->4->5, m=2, n =4 新链表:1->4->3-& ...
- 【编程题目】输入一个单向链表,输出该链表中倒数第 k 个结点
第 13 题(链表):题目:输入一个单向链表,输出该链表中倒数第 k 个结点.链表的倒数第 0 个结点为链表的尾指针.链表结点定义如下: struct ListNode {int m_nKey;Lis ...
- 输出单向链表中倒数第k个结点
描述 输入一个单向链表,输出该链表中倒数第k个结点,链表的倒数第0个结点为链表的尾指针. 链表结点定义如下: struct ListNode { int m_nKey; ListNode* ...
- Linus:利用二级指针删除单向链表
Linus大神在slashdot上回答一些编程爱好者的提问,其中一个人问他什么样的代码是他所喜好的,大婶表述了自己一些观点之后,举了一个指针的例子,解释了什么才是core low-level codi ...
- 【转】Linus:利用二级指针删除单向链表
原文作者:陈皓 原文链接:http://coolshell.cn/articles/8990.html 感谢网友full_of_bull投递此文(注:此文最初发表在这个这里,我对原文后半段修改了许多, ...
- C语言实现单向链表及其各种排序(含快排,选择,插入,冒泡)
#include<stdio.h> #include<malloc.h> #define LEN sizeof(struct Student) struct Student / ...
- 数据结构——Java实现单向链表
结点类: /** * @author zhengbinMac * 一个OnelinkNode类的对象只表示链表中的一个结点,通过成员变量next的自引用方式实现线性表中各数据元素的逻辑关系. */ p ...
- 输入一个单向链表,输出该链表中倒数第K个结点
输入一个单向链表,输出该链表中倒数第K个结点,具体实现如下: #include <iostream> using namespace std; struct LinkNode { publ ...
- 单向链表JAVA代码
//单向链表类 publicclassLinkList{ //结点类 publicclassNode{ publicObject data; ...
- C++ 单向链表反转
单向链表反转,一道常见的面试题,动手实现下. #include "stdafx.h" #include <stdlib.h> struct Node{ int data ...
随机推荐
- paip.提升性能---- 网站并发数的总结.txt
paip.提升性能---- 网站并发数的总结.txt 作者Attilax , EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.n ...
- careercup-C和C++ 13.2
13.2 浅析哈希表和STL map.对比哈希表和STL map.哈希表是怎么实现的?如果输入数据规模不大, 我们可以使用什么数据结构来代替哈希表. 解答 对比哈希表和STL map 在哈希表中,实值 ...
- 编写程序,查找并删除forward_list<int>中的奇数元素
#include<iostream> #include<forward_list> using namespace std; int main() { forward_list ...
- 【转】 如何利用Cocos2d-x开发一个游戏
原文:http://blog.csdn.net/honghaier/article/details/7888592 这个问题的结果应该是一个流程.我将从一些长期的PC端游戏开发经验结合Cocos2d- ...
- Android(java)学习笔记130:ProgressBar使用的
首先我们看例程如下: 1.main.xml文件如下: <?xml version="1.0" encoding="utf-8"?> <Line ...
- 【转】Split strings the right way – or the next best way
I know many people are bored of the string splitting problem, but it still seems to come up almost ...
- 排序并获取index的顺序
//排序并获取index的顺序:4,7,2,9-->9,7,4,2-->4,2,1,3 Array.prototype.getIndex=function(){ var orderLeng ...
- SpringAOP的注解方式
AOP(注解)[理解][应用][重点] 1.AOP注解配置流程 A.开启AOP配置支持注解@aspectj 核心配置文件中添加以下配置,功能等同于注解配置Bean的自动扫描路径 <aop:asp ...
- oracle的sid
SID是一个数据库的唯一标识符!是你在建立一个数据库时系统自动赋予的一个初始ID,虽说他和数据库名(DB_NAME)都是一个数据库的唯一标识符,但是在作用上就有不小区别.SID主要用于在一些DBA操作 ...
- HW--自守数
package testcase; import huawei.Demo; import junit.framework.TestCase;//加入测试框架,不需要写Main函数 public cla ...