php如何实现从尾到头打印链表(代码实例)

首页 编程分享 PHP丨JAVA丨OTHER 正文

anonymous 转载 编程分享 2019-04-05 01:27:11

简介 关于php如何实现从尾到头打印链表(代码实例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。


本篇文章给大家带来的内容是关于php如何实现从尾到头打印链表(代码实例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

1.遍历后压入反转数组,输出
2.array_unshift — 在数组开头插入一个或多个单元,将传入的单元插入到 array 数组的开头
int array_unshift ( array &$array , mixed $value1 [, mixed $... ] )

<?php
class Node{
        public $data;
        public $next;
}
//创建一个链表
$linkList=new Node();
$linkList->next=null;
$temp=$linkList;
for($i=1;$i<=10;$i++){
        $node=new Node();
        $node->data="aaa{$i}";
        $node->next=null;
        $temp->next=$node;
        $temp=$node;
}
function printListFromTailToHead($linkList){
        $arr=array();
        $p=$linkList;
        while($p->next!=null){
                $p=$p->next;
                array_unshift($arr,$p->data);
        }
        return $arr;
}
$arr=printListFromTailToHead($linkList);
var_dump($arr);


转载链接:http://www.php.cn//php-weizijiaocheng-409929.html


Tags:


本篇评论 —— 揽流光,涤眉霜,清露烈酒一口话苍茫。


    声明:参照站内规则,不文明言论将会删除,谢谢合作。


      最新评论




ABOUT ME

Blogger:袅袅牧童 | Arkin

Ido:PHP攻城狮

WeChat:nnmutong

Email:nnmutong@icloud.com

标签云