


PHP implements single linked list, PHP implements single chain_PHP tutorial
php implements single linked list, php implements single chain
<?<span>php </span><span>/*</span><span>* * 单链表 </span><span>*/</span> <span>class</span><span> Demo { </span><span>private</span> <span>$id</span><span>; </span><span>public</span> <span>$name</span><span>; </span><span>public</span> <span>$next</span><span>; </span><span>public</span> <span>function</span> __construct (<span>$id</span> = '', <span>$name</span> = ''<span>) { </span><span>$this</span>->id = <span>$id</span><span>; </span><span>$this</span>->name = <span>$name</span><span>; } </span><span>static</span> <span>public</span> <span>function</span> show (<span>$head</span><span>) { </span><span>$cur</span> = <span>$head</span><span>; </span><span>while</span> (<span>$cur</span>-><span>next</span><span>) { </span><span>echo</span> <span>$cur</span>-><span>next</span>->id,'###',<span>$cur</span>-><span>next</span>->name,'<br />'<span>; </span><span>$cur</span> = <span>$cur</span>-><span>next</span><span>; } </span><span>echo</span> '<hr />'<span>; } </span><span>//</span><span>尾插法</span> <span>static</span> <span>public</span> <span>function</span> push (<span>$head</span>, <span>$node</span><span>) { </span><span>$cur</span> = <span>$head</span><span>; </span><span>while</span> (<span>NULL</span> != <span>$cur</span>-><span>next</span><span>) { </span><span>$cur</span> = <span>$cur</span>-><span>next</span><span>; } </span><span>$cur</span>-><span>next</span> = <span>$node</span><span>; </span><span>return</span> <span>$head</span><span>; } </span><span>static</span> <span>public</span> <span>function</span> insert(<span>$head</span>, <span>$node</span><span>) { </span><span>$cur</span> = <span>$head</span><span>; </span><span>while</span> (<span>NULL</span> != <span>$cur</span>-><span>next</span><span>) { </span><span>if</span> (<span>$cur</span>-><span>next</span>->id > <span>$node</span>-><span>id) { </span><span>break</span><span>; } </span><span>$cur</span> = <span>$cur</span>-><span>next</span><span>; } </span><span>$node</span>-><span>next</span> = <span>$cur</span>-><span>next</span><span>; </span><span>$cur</span>-><span>next</span> = <span>$node</span><span>; </span><span>return</span> <span>$head</span><span>; } </span><span>static</span> <span>public</span> <span>function</span> edit(<span>$head</span>, <span>$node</span><span>) { </span><span>$cur</span> = <span>$head</span><span>; </span><span>while</span> (<span>NULL</span> != <span>$cur</span>-><span>next</span><span>) { </span><span>if</span> (<span>$cur</span>-><span>next</span>->id == <span>$node</span>-><span>id) { </span><span>break</span><span>; } </span><span>$cur</span> = <span>$cur</span>-><span>next</span><span>; } </span><span>$cur</span>-><span>next</span>->name = <span>$node</span>-><span>name; </span><span>return</span> <span>$head</span><span>; } </span><span>static</span> <span>public</span> <span>function</span> pop (<span>$head</span>, <span>$node</span><span>) { </span><span>$cur</span> = <span>$head</span><span>; </span><span>while</span> (<span>NULL</span> != <span>$cur</span>-><span>next</span><span>) { </span><span>if</span> (<span>$cur</span>-><span>next</span> == <span>$node</span><span>) { </span><span>break</span><span>; } </span><span>$cur</span> = <span>$cur</span>-><span>next</span><span>; } </span><span>$cur</span>-><span>next</span> = <span>$node</span>-><span>next</span><span>; </span><span>return</span> <span>$head</span><span>; } } </span><span>$team</span> = <span>new</span><span> Demo(); </span><span>$node1</span> = <span>new</span> Demo(1, '唐三藏'<span>); Demo</span>::push(<span>$team</span>, <span>$node1</span><span>); </span><span>$node1</span>->name = '唐僧'<span>; Demo</span>::show(<span>$team</span><span>); </span><span>//</span><span> Demo::show($team);</span> <span>$node2</span> = <span>new</span> Demo(2, '孙悟空'<span>); Demo</span>::insert(<span>$team</span>, <span>$node2</span><span>); </span><span>//</span><span> Demo::show($team);</span> <span>$node3</span> = <span>new</span> Demo(5, '白龙马'<span>); Demo</span>::push(<span>$team</span>, <span>$node3</span><span>); </span><span>//</span><span> Demo::show($team);</span> <span>$node4</span> = <span>new</span> Demo(3, '猪八戒'<span>); Demo</span>::insert(<span>$team</span>, <span>$node4</span><span>); </span><span>//</span><span> Demo::show($team);</span> <span>$node5</span> = <span>new</span> Demo(4, '沙和尚'<span>); Demo</span>::insert(<span>$team</span>, <span>$node5</span><span>); </span><span>//</span><span> Demo::show($team);</span> <span>$node4</span>->name = '猪悟能';<span>//</span><span>php对象传引用,所以Demo::edit没有必要 // unset($node4); // $node4 = new Demo(3, '猪悟能'); // Demo::edit($team, $node4);</span> Demo::pop(<span>$team</span>, <span>$node1</span><span>); Demo</span>::show(<span>$team</span>);

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Given a singly linked list and a positive integer N as input. The goal is to find the Nth node from the end of the given list using recursion. If the input list has nodes a→b→c→d→e→f and N is 4, then the 4th node from the last will be c. We will first traverse until the last node in the list and when returning from the recursive (backtracking) increment count. When count equals N, a pointer to the current node is returned as the result. Let's look at various input and output scenarios for this - Input - List: -1→5→7→12→2→96→33N=3 Output − The Nth node from the last is: 2 Explanation − The third node is 2 . Input − List: -12→53→8→19→20→96→33N=8 Output – Node does not exist

Comparison of the algorithm time complexity of arrays and linked lists: accessing arrays O(1), linked lists O(n); inserting arrays O(1), linked lists O(1)/O(n); deleting arrays O(1), linked lists O(n) (n); Search array O(n), linked list O(n).

A linked list representation of a number is provided like this: All nodes of the linked list are considered to be one digit of the number. Nodes store numbers such that the first element of the linked list holds the most significant digit of the number, and the last element of the linked list holds the least significant digit of the number. For example, the number 202345 is represented in the linked list as (2->0->2->3->4->5). To add 1 to this linked list representing numbers, we must check the value of the least significant bit in the list. If it's less than 9 it's ok, otherwise the code will change the next number and so on. Now let us see an example to understand how to do this, 1999 is represented as (1->9->9->9) and adding 1 should change it

Overview of the PHPSPL Data Structure Library The PHPSPL (Standard PHP Library) data structure library contains a set of classes and interfaces for storing and manipulating various data structures. These data structures include arrays, linked lists, stacks, queues, and sets, each of which provides a specific set of methods and properties for manipulating data. Arrays In PHP, an array is an ordered collection that stores a sequence of elements. The SPL array class provides enhanced functions for native PHP arrays, including sorting, filtering, and mapping. Here is an example of using the SPL array class: useSplArrayObject;$array=newArrayObject(["foo","bar","baz"]);$array

A linked list is a data structure that uses a series of nodes with data and pointers to organize elements, and is particularly suitable for processing large data sets and frequent insertion/deletion operations. Its basic components include nodes (data and pointers to the next node) and head nodes (pointing to the first node in the linked list). Common linked list operations include: addition (tail insertion), deletion (specific value) and traversal.

LinkedList is a common data structure, which consists of a series of nodes. Each node contains two key attributes: data field (Data) and pointer field (Next). Among them, the data field is used to store actual data, and the pointer field points to the next node. In this way, linked lists store data in a flexible way that is suitable for many different application scenarios. In the Go language, the linked list structure is also well supported. Cont is provided in Go's built-in standard library

In Python, a linked list is a linear data structure that consists of a sequence of nodes, each node containing a value and a reference to the next node in the linked list. In this article, we will discuss how to add elements to the first and last position of a linked list in Python. LinkedList inPython A linked list is a reference data structure used to store a set of elements. It is similar to an array in a way, but in an array, the data is stored in contiguous memory locations, whereas in a linked list, the data is not subject to this condition. This means that the data is not stored sequentially but in a random manner in memory. Thisraisesonequestionthatis,howwecanac

Method to implement a linked list: 1. Define a Node structure to represent the nodes of the linked list. Each node contains a data item and a pointer to the next node; 2. Define a LinkedList structure to represent the linked list itself, where Contains a pointer to the head node of the linked list; 3. Two methods are implemented, append is used to insert nodes at the end of the linked list, and printList is used to print the elements of the linked list; 4. In this way, structures and pointers of the Go language can be used To implement the basic functions of a linked list.
