Home Backend Development PHP Tutorial php兑现单链表(静态链表)

php兑现单链表(静态链表)

Jun 13, 2016 am 10:49 AM
data function gt this

php实现单链表(静态链表)

<?php    /*     * 单链表的PHP实现     *     * @author zhaojiangwei     * @since 2011/10/20     */    //结点类    class Node{        private $next = NULL; //下一个结点指针        private $data = NULL; //数据           public function Node($data, $next = NULL){            $this->data = $data;            $next && $this->next = $next;        }        public function getData(){            return $this->data;        }        public function setData($data){            $this->data = $data;        }        public function getNext(){            return $this->next;        }        public function setNext($next){            $this->next = $next;        }    }    //单链表类    class LinkList{        private $data_list = NULL; //结点集                  public function LinkList($data = false){            $this->data_list = array();            $title = new Node(NULL);            $this->data_list[] = $title;                      if($data){                if(is_array($data)){                    $this->addMoreData($data);                }else{                    $this->addData($data);                }            }        }        //返回第N个结点的值        public function getNodeByNumber($number){            return $this->data_list[$this->findKeyByNumber($number)]->getData();        }        //添加一组结点        public function addMoreData($datas){            foreach($datas as $value){                $this->addData($value);            }        }        //添加结点统一入口,供外面调用        //$number 添加在第几个结点的后面        public function addData($data, $number = false){            $node = new Node($data);            if($number === FALSE || $number == count($this->data_list)){                $this->insertLastNode($node);            }elseif($number > count($this->data_list)){                return false;            }else{                $this->insertNode($node, $number);            }        }        //插入一个结点到最后        private function insertLastNode($node){                $node->setNext(NULL);                             $lastKey = $this->findLastNode();                $insert_key = $this->insertNodeIntoArray($node);                $this->data_list[$lastKey]->setNext($insert_key);        }            //插入一个结点        private function insertNode($node, $number){            $insert_number = $this->findKeyByNumber($number);                $node->setNext($this->data_list[$insert_number]->getNext());            $insert_key = $this->insertNodeIntoArray($node);            $this->data_list[$insert_number]->setNext($insert_key);        }        //查找第N个结点对应的数组key        private function findKeyByNumber($number){            $i = $key = 0;            while($i < $number){                $key = $this->data_list[$key]->getNext();                $i ++;            }                     return $key;        }        //将结点加入数组        private function insertNodeIntoArray($node){            $this->data_list[] = $node;                 return $this->getLastKey();        }        //删除结点        public function deleteNode($number){            if($number == 0 || $number > count($this->data_list)){                return false;            }            $pre_key = $this->findKeyByNumber($number - 1);            $key = $this->data_list[$pre_key]->getNext();          $this->data_list[$pre_key]->setNext($this->data_list[$key]->getNext());            unset($this->data_list[$key]);        }        //查找某结点的前一个结点        private function getPreNodeKey($key){            foreach($this->data_list as $k=>$v){                if($v->getNext() == $key){                    return $k;                  }            }            return false;        }        //打印链表        public function getData_list(){            return $this->data_list;        }        //返回数组的最后一个键        private function getLastKey(){            end($this->data_list);            return key($this->data_list);        }        //判断某个键值是否存在        private function ifExistKey($key){            if(array_key_exists($key, $this->data_list)){                return true;            }                      return false;        }        //查找尾结点        public function findLastNode(){            foreach($this->data_list as $key=>$value){                if($value->getNext() === NULL){                    return $key;                }            }        }    }?>
Copy after login

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

What does function mean? What does function mean? Aug 04, 2023 am 10:33 AM

Function means function. It is a reusable code block with specific functions. It is one of the basic components of a program. It can accept input parameters, perform specific operations, and return results. Its purpose is to encapsulate a reusable block of code. code to improve code reusability and maintainability.

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

How to Fix Can't Connect to App Store Error on iPhone How to Fix Can't Connect to App Store Error on iPhone Jul 29, 2023 am 08:22 AM

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

What is the purpose of the 'enumerate()' function in Python? What is the purpose of the 'enumerate()' function in Python? Sep 01, 2023 am 11:29 AM

In this article, we will learn about enumerate() function and the purpose of “enumerate()” function in Python. What is the enumerate() function? Python's enumerate() function accepts a data collection as a parameter and returns an enumeration object. Enumeration objects are returned as key-value pairs. The key is the index corresponding to each item, and the value is the items. Syntax enumerate(iterable,start) Parameters iterable - The passed in data collection can be returned as an enumeration object, called iterablestart - As the name suggests, the starting index of the enumeration object is defined by start. if we ignore

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

Detailed explanation of the role and function of the MySQL.proc table Detailed explanation of the role and function of the MySQL.proc table Mar 16, 2024 am 09:03 AM

Detailed explanation of the role and function of the MySQL.proc table. MySQL is a popular relational database management system. When developers use MySQL, they often involve the creation and management of stored procedures (StoredProcedure). The MySQL.proc table is a very important system table. It stores information related to all stored procedures in the database, including the name, definition, parameters, etc. of the stored procedures. In this article, we will explain in detail the role and functionality of the MySQL.proc table

What data is in the data folder? What data is in the data folder? May 05, 2023 pm 04:30 PM

The data folder contains system and program data, such as software settings and installation packages. Each folder in the Data folder represents a different type of data storage folder, regardless of whether the Data file refers to the file name Data or the extension. Named data, they are all data files customized by the system or program. Data is a backup file for data storage. Generally, it can be opened with meidaplayer, notepad or word.

See all articles