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

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
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
4 weeks agoByDDD
R.E.P.O. Save File Location: Where Is It & How to Protect It?
4 weeks agoByDDD

Hot Tools

Dreamweaver Mac version
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

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