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

同时放在了我的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实现单向链表的更多相关文章

  1. Reverse Linked List II 单向链表逆序(部分逆序)

    0 问题描述 原题点击这里. 将单向链表第m个位置到第n个位置倒序连接.例如, 原链表:1->2->3->4->5, m=2, n =4 新链表:1->4->3-& ...

  2. 【编程题目】输入一个单向链表,输出该链表中倒数第 k 个结点

    第 13 题(链表):题目:输入一个单向链表,输出该链表中倒数第 k 个结点.链表的倒数第 0 个结点为链表的尾指针.链表结点定义如下: struct ListNode {int m_nKey;Lis ...

  3. 输出单向链表中倒数第k个结点

    描述 输入一个单向链表,输出该链表中倒数第k个结点,链表的倒数第0个结点为链表的尾指针. 链表结点定义如下: struct ListNode { int       m_nKey; ListNode* ...

  4. Linus:利用二级指针删除单向链表

    Linus大神在slashdot上回答一些编程爱好者的提问,其中一个人问他什么样的代码是他所喜好的,大婶表述了自己一些观点之后,举了一个指针的例子,解释了什么才是core low-level codi ...

  5. 【转】Linus:利用二级指针删除单向链表

    原文作者:陈皓 原文链接:http://coolshell.cn/articles/8990.html 感谢网友full_of_bull投递此文(注:此文最初发表在这个这里,我对原文后半段修改了许多, ...

  6. C语言实现单向链表及其各种排序(含快排,选择,插入,冒泡)

    #include<stdio.h> #include<malloc.h> #define LEN sizeof(struct Student) struct Student / ...

  7. 数据结构——Java实现单向链表

    结点类: /** * @author zhengbinMac * 一个OnelinkNode类的对象只表示链表中的一个结点,通过成员变量next的自引用方式实现线性表中各数据元素的逻辑关系. */ p ...

  8. 输入一个单向链表,输出该链表中倒数第K个结点

    输入一个单向链表,输出该链表中倒数第K个结点,具体实现如下: #include <iostream> using namespace std; struct LinkNode { publ ...

  9. 单向链表JAVA代码

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

  10. C++ 单向链表反转

    单向链表反转,一道常见的面试题,动手实现下. #include "stdafx.h" #include <stdlib.h> struct Node{ int data ...

随机推荐

  1. SQL书写技巧

    SQL书写技巧: 1.针对分区表,如果可以使用分区条件的,一定要加分区条件.分区条件的使用,可以减少不必要的数据访问,加快查询数据,如TB_CSV_ACCEPT_FLOW_OPERATOR表,以acc ...

  2. careercup-中等难度 17.3

    17.3 写一个算法计算n的阶乘末尾0的个数? 解答: 首先,算出n的阶乘的结果再去计算末尾有多少个0这种方法是不可取的, 因为n的阶乘是一个非常大的数,分分种就会溢出.我们应当去分析, 是什么使n的 ...

  3. wcf自身作为宿主的一个小案例

    第一步:创建整个解决方案 service.interface:用于定义服务的契约(所有的类的接口)引用了wcf的核心程序集system.ServiceModel.dll service:用于定义服务类 ...

  4. java_IO读写模版

    InputStream in = null; OutputStream out = null; try{ in = new FileInputStream(); int len=0; byte buf ...

  5. swfupload操作手册

    SWFUpload SWFUpload 最初是Vinterwebb.se 开发的客户端文件上传工具.它联合javascript和flash,在浏览器中提供一个优于传统上传标签 <input ty ...

  6. 深入学习block

    首先,什么是block?block其实就是一个代码块,把你想要执行的代码封装在这个代码块里,等到需要的时候再去调用.那block是OC对象吗?答案是肯定的. 做一道很简单的关于block的测试题: i ...

  7. eclipse创建项目时出现appcompat_v7包及解决办法

    Android开发学习总结(三)--appcompat_v7项目说明 一.appcompat_v7项目说明 今天来说一下appcompat_v7项目的问题,使用eclipse创建Android项目时, ...

  8. ios通知-kvo

    // KVC: Key Value Coding, 常见作用:给模型属性赋值    // KVO: Key Value Observing, 常用作用:监听模型属性值的改变 // // ViewCon ...

  9. Android_GestureDetector

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...

  10. Java获取 JVM 运行信息

    import java.lang.management.ClassLoadingMXBean; import java.lang.management.ManagementFactory; impor ...