The implementation of the linked list in PHP

Source: Internet
Author: User

This article introduces the content of the link list in PHP implementation, has a certain reference value, now share to everyone, the need for friends can refer to

Start learning about data structures

Today write code changed a font, used to look good console , today found a more like style Source Code Pro
On two pictures, it is very good-looking!!!

Step into the line and talk about the operation of the list

Node

    • First, you have to have a node class for storing data

<?phpnamespace linkedlist;class node{    /**     * @var $data integer     */public    $data;    /**     * Node points to the next element     *     * @var $next node */public    $next;    Public function __construct (int $data =-1)    {public        function __construct (int $data = null)        {            //Initialize Assignment The value data can also be obtained by $node->data = X; Assignment            $this->data = $data;        }}

Linked list management class (for manipulating node data)

    • The code of the Operation class is too long for us to parse in part

Header insertion (because it's relatively simple, so let's say this first)

    • If you listen to a name, you're going to insert a node from the head.

    • Initializes the current node when the linked list is empty

    • When the linked list is not empty, the new node is used as the head junction.

Public function inserthead (int $data): bool{//////////////////////////////////////////////////////////////////////           /////    // +-----------+    +--------+    +--------+    // |    |        |    |        |    | // | Head Node |  +> | Node |  +> | Node |           +>//|  | |        |  | |        |    | |           // |  | |        |  | |        |    | |    // |  Next | |  |  Next | |  |    Next | |  // +------+----+ |  +----+---+ |    +----+---+ |      //        |       |     |       |     |    |                   //        +------+       +-----+       +-----+    ///////////////////////////////////////////////////////////////    //           +-----------+    +--------+    +--------+    //                   |    |        |    |        |    | +---> | Head Node |  +> | Node |  +> | Node |     +>//|           |  | |        |  | |        |    | |     //             |           |  | |        |  | |        |    | |//             |    |  Next | |  |  Next | |  |    Next | |     //             |  +------+----+ |  +----+---+ |    +----+---+ |            //             |      |       |     |       |     |    |            //  +--------+ |        +------+       +-----+       +-----+    //  |    | |    |new node| |        //  |    | |        //  |    | |  //  |    Next | |    //  +----+---+ |     //       |    | +-----+////1. Instantiate a data node//2. Causes the current node's next equal to the present head node//Even if the current head node is NULL, it can also be set up//3.    Make the current Node a head node//To complete the insertion of the head node $newNode = new node ($data);    $newNode->next = $this->head;    $this->head = $newNode; return true;}

Insert the node (index=0 is the head node, go down, and return False if the position is exceeded)

Public function Insert (int $index = 0, int $data): bool{//Head node insertion, when header does not exist, or index 0 if (is_null ($this->head) | | $i    Ndex = = 0) {return $this->inserthead ($data);    }//Normal node insertion, index calculated starting from 0//skipping head node, starting from 1 $currNode = $this->head;    $startIndex = 1; Traversing the entire list, if the current node is null, represents the next end of the trailer, Exits the loop for ($currIndex = $startIndex;! is_null ($currNode); + + $currIndex) {//     //////////////////////////////////////////////////////////////////////////        ///        //   +--------+    +--------+        +-------------+    +--------+        //   |    |        |    |             |    |        |        |  //   | Node | +> |currnode| +> |currnode next|  +> | Node |        +>//|  | |        |  | |             |  | |        |        | |        //   |  | |        |  | |             |  | |        |        | |  //   |  Next | |  |  Next | |     |  Next | |  |        Next | |  //   +----+---+ |  +----+---+ |  +------+------+ | +----+---+ |     //        |       |     |         |        |       |     |        | //        +-----+       +-----+         +--------+       +-----+        //////////////////////////////////////////////////         //////////////////////////        //   +--------+    +--------+                +-------------+    +--------+        //   |    |        |                |             |    |        |        |  //   | Node |             +> |currnode| +> |currnode next|  +> | Node |        +>//|  | |        |             |  |             |  | |        |        | |        //   |  | |        |             |  |             |  | |        |        | |  //   |  Next | |  |             Next |  |     |  Next | |  |        Next | |  //   +----+---+ |  +--------+             |  +------+------+ |        +----+---+ |     //        |              |         +--------+ |        |       |     |        |        //        +-----+              |         | | +--------+       +-----+       |new node| |        //                             |        | |        //                             |        | |  //                             |        Next | |        //                             +----+---+ |     //                                  |        | //                                  +-----+        ///////////////////////////////////////////////////////////////////////        /////        //        //   +--------+    +--------+                +-------------+    +--------+        //   |    |        |                |             |    |        |        |  //   | Node |             +> |currnode| +> |currnode next|  +> | Node |        +>//|  | |        |             |  |             |  | |        |        | |        //   |  | |        |             |  |             |  | |        |        | |  //   |  Next | |  |             Next |  |     |  Next | |  |        Next | |  //   +----+---+ | +----+---+             |  +------+------+ |        +----+---+ |     //        |       |      |         +--------+ |        |       |     |        |      //        +-----+       |        |         | |        +--------+ +-----+//+----> |new node| |        //                             |        | |        //                             |        | |  //                             |        Next | |        //                             +----+---+ |     //                                  |        | +-----+////1. The current index equals the index of the incoming parameter//2. Instantiate the new data node//3. The next node of the new node that points to the current node//4.            The next node of the current node points to the new node if ($currIndex = = = $index) {$newNode = new node ($data);            $newNode->next = $currNode->next;            $currNode->next = $newNode;        return true;    }//move to the next node $currNode = $currNode->next; } return false;}

Above two this is the basic operation of inserting. Take a look at the code for the instance.

<?php//automatically loaded code is not posted, directly in the Githubrequire __dir__. ' /.. /vendor/bootstrap.php ';//Instantiate a list of managed objects $manager = new \linkedlist\manager ();//8$manager->inserthead (8);//5 8$ Manager->inserthead (5);//1 5 8$manager->inserthead (1);//1 2 5 8$manager->insert (1, 2);//False node element less than 6 $manag Er->insert (5, 4);//1 2 5 8 9$manager->insertend (9);//3$manager->find (8);//1 2 8 9$manager->delete (2);

Find

    • Finding the value of a linked list is also straightforward, as long as you traverse it

/*** Index found in the value of the linked list * Successfully returned index value, cannot find return -1** @param int $data * @return int*/public function find (int $data): int{    $currNode = $this->head;    Find or is very simple, as long as the list is traversed once, and then determine whether the value is equal to the for    ($i = 0;! is_null ($currNode); + + $i) {        if ($currNode->data = = = $data) {            return $i;        }        $currNode = $currNode->next;    }    return-1;}
    • Only need to traverse the list once, find the equal value, find the return index value, can't find return-1

Delete

/** * Delete Linked list node * * @param int $index * @return bool */public function Delete (int $index): bool{    //No nodes, skip directly    if (Is_null ($this->head)) {       return false;    } ElseIf ($index = = = 0) {        //Head node deletion        $this->head = $this->head->next;    }    The starting index here is 1    //But the current node points to the exact head node/    /Because the deletion must be marked by the deletion of the previous nodes    //For the judge is to determine whether the next node is null    //$currNode is the node of the operation    //    $currNode->next is the node to be removed    $startIndex = 1;    $currNode = $this->head;    for ($i = $startIndex; Is_null ($currNode->next); + + $i) {        if ($index = = = $i) {            //make the current node equal to the next            //of the node to be deleted You can complete the deletion            $currNode->next = $currNode->next->next;            break;        }        $currNode = $currNode->next;    }    return true;}

End

    • Code is hosted on GitHub

    • Follow the time to continue learning data structure, double-linked list, tree and so on!!!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.