Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the unidirectional linked list of data structure in php

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

What this article shares with you is the one-way linked list about how to realize the data structure in php. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Code implementation

Define nod

Class Node {public $data; / * * @ var null | Node * / public $next; public function _ _ construct ($data) {$this- > data = $data; $this- > next = null;}}

Single linked list implementation

/ * Class SingleLinkList * single link implementation example, to achieve simple add, insert, delete, query, length, traversal of these simple operations * / class SingleLinkList {/ * chain header node, header node must exist, * @ var Node * / public $header; private $size = 0 / * constructor. Add a Sentinel node by default. The node element is empty * SingleLinkList constructor. * / public function _ _ construct () {$this- > header = new Node (null);} / * add a node at the end of the list * @ param Node $node * @ return int * / public function addNode (Node $node) {$current = $this- > header; while ($current- > next! = null) {$current = $current- > next } $current- > next = $node; return + + $this- > size;} / * insert the node * @ param int $index node location at the specified location and count * @ param Node $node * @ return int * @ throws Exception * / public function insertNodeByIndex ($index, Node $node) {if ($index) starting from 1

< 1 || $index >

($this- > size + 1) {throw new Exception ('the position you want to insert exceeds the length of the linked list% dink, $this- > size);} $current = $this- > header; $tempIndex = 1; do {if ($index = = $tempIndex++) {$node- > next = $current- > next; $current- > next = $node Break;}} while ($current- > next! = null & & ($current = $current- > next); return + + $this- > size;} / * * Delete node * @ param int $index node location, count * @ return int * @ throws Exception * / public function deleteNodeByIndex ($index) {if ($index) from 1

< 1 || $index >

($this- > size + 1) {throw new Exception ('the node you deleted does not exist');} $current = $this- > header; $tempIndex = 1; do {if ($index = = $tempIndex++) {$current- > next = $current- > next- > next; break }} while ($current- > next! = null & & ($current = $current- > next); return-- $this- > size;} / * * query node * @ param int $index node location, counting from 1 * @ return Node | null * @ throws Exception * / public function searchNodeByIndex ($index) {if ($index)

< 1 || $index >

($this- > size + 1) {throw new Exception ('the node you queried does not exist');} $current = $this- > header; $tempIndex = 1; do {if ($index = = $tempIndex++) {return $current- > next;}} while ($current- > next! = null & & ($current = $current- > next)) } / * get node length * @ return int * / public function getLength () {return $this- > size;} / * traversal list * / public function showNode () {$current = $this- > header; $index = 1; while ($current- > next! = null) {$current = $current- > next Echo 'index -'. $index++. '-'; echo var_export ($current- > data); echo PHP_EOL;}}

Example

$link = new SingleLinkList (); $link- > addNode (new Node (1)); $link- > addNode (new Node (2)); $link- > insertNodeByIndex (3, new Node (3)); $link- > addNode (new Node (4)); $link- > addNode (new Node (5)); echo $link- > getLength (), PHP_EOL;$link- > showNode (); echo'-', PHP_EOL;var_dump ($link- > searchNodeByIndex (3); echo'-', PHP_EOL $link- > deleteNodeByIndex (3); $link- > showNode (); the above is the one-way linked list of how to implement the data structure in php. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report