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

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

不言
不言Original
2018-09-12 17:20:091691browse

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);

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!

Statement:
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