Simple Example of PHP linked list operation and simple example of php operation

Source: Internet
Author: User

Simple Example of PHP linked list operation and simple example of php operation

This example describes the PHP linked list operation. We will share this with you for your reference. The details are as follows:

Running the data structure in php is basically simulated by arrays, but it is always thought.

The problem today is that the two linked lists are merged.

Linked List Merging

Problem description:The A linked list is A template linked list. The length of the B linked list is unknown. The A and B linked lists are combined to form A C linked list.

Let's talk about programming ideas:The A linked list is A template linked list, so the operation is complete and the length is unique and unchanged. The length of the list B is uncertain. Therefore, you can judge the B linked list in three steps:

Is linked list B empty?
Is the B linked list shorter or equal to the linked list?
Is linked list B longer than linked list?

Programming is to list as many possibilities as possible. Catch the variable. As required by the question, when the linked list and the B linked list are equal, the B linked list is directly returned, so you do not need to consider this issue.

$ Node = array ("nameid" => "", "shoolid" => "", "depid" => "", "start" => "", "end" => "");/* If the preceding data exists, some data exists in linked list A, and some data exists in linked list B. If none exist, use the data node of the linked list instead. At the first time, I thought of a very painful way and used the arra_diff () function to make a difference using this linked list. Later, I thought about it carefully. * /// $ Data indicates the B linked list // $ time indicates the linked list // to save resources, the third linked list is not opened, but operated in the B linked list, why do you need to select a chain table with uncertain length? // you will know why if (empty ($ data) // {// apply for a node $ data = array (); foreach ($ time as $ value) {// process the data in the linked list as required, the node mode $ array = array ("nameid" => $ value ["id"], "depid" => $ depid, "schoolid" => $ schoolid, "start" => "", "end" => ""); array_push ($ data, $ array ); // press the new node into the stack} else if (count ($ data) <= count ($ time) // compare the length {for ($ I = 0; $ I <count ($ time); $ I ++ )// For loop, it is not recommended to continue dynamic judgment in the for loop. I am lazy here. {If (empty ($ data [$ I]) {// if the data node is empty, then the build node $ array = array ("nameid" => $ time [$ I] ["id"], "depid" => $ depid, "schoolid" => $ schoolid, "start" => "", "end" => ""); array_push ($ data, $ array );}}}

The above algorithm is A simple practice. It combines the data of A linked list with the data of B.

Related Article

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.