This article mainly introduces the PHP linked list operation, combined with a simple example of the definition and use of the PHP chain list, with a certain reference value, the need for friends can refer to the next
Running the data structure in PHP is basically simulated with an array, but only with thought.
The problem encountered today is that two linked lists are merged.
Linked list Merging
Problem Description: A linked list is a template linked list, the length of the B-linked list is not determined, a, a, a two linked list to form a C linked list.
Talk about programming ideas: A linked list is a template linked list so at the completion of the operation, the length is the only constant. The length of the B-linked list is indeterminate. So you can judge the B-linked list first, divided by three steps:
B-linked list is not empty
b linked list is not shorter or equal than a linked list
b linked list is not longer than a linked list
Programming is to list the possibilities as much as possible. Catch variable, due to the topic requirements, when a linked list and B linked list is equal, return directly to the list of B, so do not consider this problem.
$node = Array ("NameID" and "" "," Shoolid "and" "," Depid "and" "," Start "and" "", "End" = "" ");/* Now the above data, some data exist in a linked list, Some exist in the B-linked list, if not, use a linked list of data nodes instead. When I first finished, I thought of a very painful way, and I used the Arra_diff () function to make the list worse. I thought about it a little bit later. *///$data represents a list of B//$time represents a linked list//here in order to conserve resources, not open the third linked list, but in the B-linked list operation, why choose an indeterminate length of the chain list//Read you know why if (empty ($data))//{ // Application node $data = Array (); foreach ($time as $value) { //The data for a linked list is processed in demand, making up the node pattern we need $array = Array ("NameID" + $value ["id"], "depid "=" and $depid, "SchoolID", "=" and "$schoolid", "Start" and "", "End" and "" "); Array_push ($data, $array); Press the new node into the stack }}else if (count ($data) <=count ($time))//For length comparison {for ($i =0; $i <count ($time); $i + +)//for cycle, It is not recommended to continue the dynamic judgment in the For loop, I am lazy here. { if (empty ($data [$i])) { //If the data node is empty, the build node $array = Array ("NameID" + = $time [$i] ["id"], " Depid "=" and $depid, "schoolid" and "=" $schoolid, "Start" and "", "End" and "" "); Array_push ($data, $array);}}}
The above algorithm is a simple internship, a linked list of data, and B data combination.
Summary: The above is the entire content of this article, I hope to be able to help you learn.