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

同时放在了我的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. win8如何删除未知账户(s-1-5-21-2000478354-1390067357-725345543-1003)

    今天突然发现从别处复制来的游戏压缩文件不能解压,并且以前把游戏文件都是放在该目录下的,以前局域网玩起游戏来老是不能作为主机,且不能下载局域网玩的RPG地图,以前就注意过这个未知账户(s-1-5-21- ...

  2. 《Java并发编程实战》第十一章 性能与可伸缩性 读书笔记

    造成开销的操作包含: 1. 线程之间的协调(比如:锁.触发信号以及内存同步等) 2. 添加�的上下文切换 3. 线程的创建和销毁 4. 线程的调度 一.对性能的思考 1 性能与可伸缩性 执行速度涉及下 ...

  3. php单引号、双引号与数据库

    /**  * 初始化http参数数据  */ public static function init () {  if (!get_magic_quotes_gpc()) {   $_POST  = ...

  4. Android(java)学习笔记131:Intent启动别的Activity

    1.案例 (1)首先是main.xml和other.xml文件如下: main.xml文件: <?xml version="1.0" encoding="utf-8 ...

  5. Android_AsyncTask_Method

    package com.example.day07_asynctask_method; import android.os.AsyncTask; import android.os.Bundle; i ...

  6. IE兼容低版本

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><m ...

  7. SpringMVC与Struts2配置区别

     Spring MVC模型与Struts2模型应用:  Html表单: 上述这两段代码无论是SpringMVC还是Struts2,都可以共用.而在请求响应处理类(也就是Controller)上的设计差 ...

  8. 史上最全的phpstorm常用配置

    取消自动保存并标识修改的文件为星星标记 1.取消自动保存 进入 File -> Settings -> General,取消下面两选项的勾选: 2.星星标记 进入 File -> S ...

  9. (原创)fedora 17安装KVM虚拟机

    1.安装KVM yum groupinstall Virtualization 'Virtualization Client' 2.安装api支持 yum install libvirt servic ...

  10. arguments 函数内部属性

    1.arguments 是在function方法里面的,是实参数组,用法是挺多的,下面来记录一下 2.利用arguments实现方法的重载 //01.使用argument模拟方法重载 function ...