Home > Backend Development > PHP Tutorial > How to implement singly linked list header insertion in PHP (code example)

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

不言
Release: 2023-04-04 07:22:01
Original
1785 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
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template