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

同时放在了我的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. MVC - 学习总目录

    MVC - 基础 MVC - HtmlHelper类 MVC - 路由 MVC - 身份验证 MVC - 模型验证 MVC - Ajax MVC - 布局

  2. Android利用Looper在子线程中改变UI

    MainActivity如下: package cn.testlooper; import android.app.Activity; import android.os.Bundle; import ...

  3. DataGrid的ItemCreated和ItemDataBound以及合计平均行

    DataGrid为数据绑定控件,是重量级控件,臃肿,无华不实这么一个控件定位.如果做为管理系统,那么还是可以使用的. 本文只是为了记录ItemCreated和ItemDataBound两个事件的用法. ...

  4. Android之HTTP网络通信--GET传递(二)

    根据上一篇写的是实现了通过url接口将接口中的数据显示出来,这次根据上一篇的基础,进一步说明一下AsynTask的使用. AsynTask类有几个函数是大家必须知道的. doInBackGround( ...

  5. F5中源地址转换(AutoMap)模式下后端服务器获取客户端真正的IP地址

    F5中开启AutoMap,并传递X-Forwarded-For值 开启F5源地址转换"Auto Map" 方式一: 在http profile中开启X-Forwarded-For ...

  6. Failed to create java virtue machine(不能创建java虚拟机)

    今天开发模块时,遇到这个问题,本来是版本的问题,jdk1.6的版本有点低,与cxf框架不兼容,需要用到jdk1.7,结果安装了jdk1.7之后,客户方要求必须用jdk1.6,要统一,所以卸载jdk1. ...

  7. jquery uploadify修改上传的文件名和显示

    如果觉得看文章太麻烦,可以直接看参考:http://stackoverflow.com/questions/7707687/jquery-uploadify-change-file-name-as-i ...

  8. .net MVC中JsonResult 返回类

    /// <summary> /// 返回消息类 /// </summary> public class ReturnMessage { private IDictionary& ...

  9. Android之 Fragment

    什么是Fragment: Android是在Android 3.0 (API level 11)开始引入Fragment的. 可以把Fragment想成Activity中的模块,这个模块有自己的布局, ...

  10. Invalid result location value/parameter

    Invalid result location value/parameter(struts2),该问题在myeclipse8.6一下的版本不会出现,但是在myeclipse9.0中就会出现该错误.有 ...