Home > Backend Development > PHP Tutorial > How to print a linked list from end to head in php (code example)

How to print a linked list from end to head in php (code example)

不言
Release: 2023-04-04 07:12:01
Original
1374 people have browsed it

The content of this article is about how PHP can print a linked list from the end to the beginning (code example). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Push the reversed array after traversal and output
2.array_unshift — Insert one or more cells at the beginning of the array, insert the incoming cells to the beginning of the array 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);
Copy after login

Related recommendations:

How to implement thermal 58MM receipt printer printing in php

PHP implementation of printing binary tree code sharing from top to bottom

The above is the detailed content of How to print a linked list from end to head in php (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template