zoukankan      html  css  js  c++  java
  • [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);
  • 相关阅读:
    Web开发快速上手
    前端概述
    Python语言进阶
    图像和办公文档处理
    网络编程
    进程和线程
    正则表达式
    面向对象进阶
    面向对象
    js 获取指定时间上月26 ,
  • 原文地址:https://www.cnblogs.com/taoshihan/p/9588435.html
Copyright © 2011-2022 走看看