How to implement singly linked list header insertion in PHP (code example)

不言
Release: 2023-04-04 07:22:01
Original
1720 people have browsed it

The content of this article is about how to implement single-linked list header insertion in PHP (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Create a head node

2. Create a new node

3. The new node next points to the head node next

4.Head Node next points to the new node

<?php
class Node{
        public $data;
        public $next;
}
//头创建一个链表
$linkList=new Node();
$linkList->next=null;//头结点
for($i=1;$i<=10;$i++){
        $node=new Node();
        $node->data="aaa{$i}";//创建新结点$node
        $node->next=$linkList->next;//$node->next指向头结点->next
        $linkList->next=$node;//头结点->next指向$node
}

var_dump($linkList);
Copy after login

Related recommendations:

php example code for implementing a singly linked list

The above is the detailed content of How to implement singly linked list header insertion 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