Hanshunping _php Programmer Play Algorithm Open Class (first quarter) 03_ single-linked list crud operation _ margin Hero ranking Algorithm _ Learning Notes _ source code diagram _ppt document collation
Vencimaron:http://blog.csdn.net/wenximalong/
singlelink.php
One-way linked list to complete the hero ranking management
Query Heroes Add Heroes Delete Heroes Change Heroes
no= $no $this->name= $name $this->nickname= $nickname;} Create a head header, the head is just a header, do not put the data $head=new Hero ();//Create a Hero $hero=new Hero (1, ' Song Jiang ', ' timely rain ');//link $head->next= $hero; Hero2=new Hero (2, ' Lu Junyi ', ' Jade Kylin ');//link//Now using a comparison of two methods, immediately improved, so as to facilitate the understanding of $hero->next= $hero 2;//single-linked list of the traversal, must start from head node to find// is from head Start Traverse, $head the value of the head must not change, change cannot traverse our single-linked list the function Showheros ($head) {//traversal [must know when, to the end of the list]//here in order not to change the direction of $head, We can use a temporary variable $cur= $head, while ($cur->next!=null) {///The first node is the header, so use $cur->next to refer to the next node, the head node nothing is empty echo '
The hero's number is '. $cur->next->no. ' Name '. $cur->next->name. ' nickname = '. $cur->next->nickname;//If it's written here, it's going to be a dead loop./ All to let $cul move $cur= $cur->next;}} Echo
The current hero ranking situation is ************** '; Showheros ($head);? >
In the above code, add a hero to use a more stupid method,
now it's a good way to add work.
1. Directly at the end of the list add
singlelink2.php
One-way linked list to complete the hero ranking management
Query Heroes Add Heroes Delete Heroes Change Heroes
no= $no $this->name= $name $this->nickname= $nickname;} Create a head header, the head is just a header, do not put the data $head=new Hero ();//write a function specifically for adding hero function Addhero ($head, $hero) {//1. Directly at the end of the list Add// To find the end of the list, you must not move the $head; $cur = $head; while ($cur->next!=null) {$cur = $cur->next;} When exiting the while loop, the $cur is the end of the list//join hero$cur->next= $hero;//2. Join as a hero (here I want to be able to guarantee the order of the list)}//single-linked list traversal, must start from head node to find is from head Start Traverse, $head the value of the head must not change, change cannot traverse our single-linked list the function Showheros ($head) {//traversal [must know when, to the end of the list]//here in order not to change the direction of $head, We can use a temporary variable $cur= $head, while ($cur->next!=null) {///The first node is the header, so use $cur->next to refer to the next node, the head node nothing is empty echo '
The hero's number is '. $cur->next->no. ' Name '. $cur->next->name. ' nickname = '. $cur->next->nickname;//If it's written here, it's going to be a dead loop./ All to let $cul move $cur= $cur->next;}} Add $hero=new Hero (1, ' Song Jiang ', ' timely rain '), Addhero ($head, $hero), $hero =new Hero (2, ' Lu Junyi ', ' Jade Kylin '), Addhero ($head, $hero); $hero = New Hero (6, ' Lin ', ' Leopard Head '), Addhero ($head, $hero), $hero =new Hero (3, ' Wu ', ' Mastermind '), Addhero ($head, $hero);//In the output, Lin is in the front of Wu, and Lin is number 6th, should be in the back of Wu, which is directly in the list of the last added drawbacks. Echo
The current hero ranking situation is ************** '; Showheros ($head);? >
2. Join as a hero (I want to keep the list in order)
In the future, there may be a need for a job, someone gives you a bunch of strings that you can sort by one of the properties of the string, and it's done in memory.
How to add in order?
★ must first talk about ideas, and then write code. Analysis Diagram
Big picture, open the picture in a new window, watch the full picture
Suppose, to do a very extreme example.
(1) First add 1th characters, first cur point to head node, Cur->next is empty, directly add 1th characters behind the head node;
(2) Add 2nd characters, first cur point to head node, cur->next at this point 1th people node, Cur->next->no 1 and $hero->no for 2 to compare, found that 1 is not greater than 2, so cur to go down one step, At this point cur point to the number 1th character node, and then to determine whether the cur->next->no is greater than 2, because at this time cur->next is empty, can not go, only 2nd characters added to the number 1th character node behind.
(3) Add 6th characters, first cur point to head node, cur->next at this point 1th people node, Cur->next->no 1 and $hero->next for 6 to compare, found that 1 is not greater than 6, so cur go down one step below , at this point Cur point 1th character node, and then to judge Cur->next->no for 2 and $hero->no 6 for comparison, found 2 is not greater than 6, so cur to go down a step, at this time cur point to the 2nd character node, and then to Judge cur- >next->no is greater than 6, because at this time cur->next is empty, can't go, can only add 6th characters in the number 2nd character node behind.
(4) Add 3rd characters, first cur point to head node, cur->next at this point 1th people node, Cur->next->no 1 and $hero->next for 3 to compare, found that 1 is not greater than 3, so cur go down one step below , at this point Cur point 1th character node, and then to judge Cur->next->no for 2 and $hero->no 3 for comparison, found 2 is not greater than 3, so cur to go down a step, at this time cur point to the 2nd character node, and then to Judge cur- >next->no for 6 and $hero->no for 3 comparison, found that 6 is greater than 3, found a position, through an operation to find a way to add the number 3rd character.
singlelink3.php
One-way linked list to complete the hero ranking management
Query Heroes Add Heroes Delete Heroes Change Heroes
no= $no $this->name= $name $this->nickname= $nickname;} Create a head header, the head is just a header, do not put the data $head=new Hero ();//write a function specifically for adding hero function Addhero ($head, $hero) {//1. Directly at the end of the list Add// To find the end of the list, do not move $head; $cur = $head;//2 in accordance with the hero's rank (here I want to be able to guarantee the order of the list)//ideas: must first talk about ideas, and then write code while ($cur->next!=null) {if ($ cur->next->no>= $hero->no) {//Find position break;//Once break, jump out of the while Loop}//continue $cur= $cur->next;} When exiting the while loop, position find//join//Let hero join $hero->next= $cur->next; $cur->next= $hero;} A single-linked list of traversal, must start from head node to find//is from the head start to traverse, $head the value of the head must not change, changes can not traverse our single-linked list of function Showheros ($head) {//traversal [must know when, To the end of the list]//here in order not to change the direction of $head, we can use a temporary variable $cur= $head; while ($cur->next!=null) {//The first node is a header, so $cur-> Next refers to the next node, where nothing in the head node is empty echo '
The hero's number is '. $cur->next->no. ' Name '. $cur->next->name. ' nickname = '. $cur->next->nickname;//If it's written here, it's going to be a dead loop./ All to let $cul move $cur= $cur->next;}} Add $hero=new Hero (1, ' Song Jiang ', ' timely rain '), Addhero ($head, $hero), $hero =new Hero (2, ' Lu Junyi ', ' Jade Kylin '), Addhero ($head, $hero); $hero = New Hero (6, ' Lin ', ' Leopard Head '), Addhero ($head, $hero), $hero =new Hero (3, ' Wu ', ' Mastermind '), Addhero ($head, $hero);//In the output, Lin is in the front of Wu, and Lin is number 6th, should be in the back of Wu, which is directly in the list of the last added drawbacks. Echo
The current hero ranking situation is ************** '; Showheros ($head);? >
Execution Process Analysis diagram
Big picture, open the picture in a new window, watch the full picture
as shown,When added to number 3rd, cur points to the 2nd character node, Cur->next points to address 0x1234, that is, the 6th character node, at this time the address of the 3rd character is 0x345. Execute the $hero->next= $cur->next; the node of number 3rd will point to the number 6th character node address 0x1234, the ① virtual Blue line shown in the figure, and then $cur->next= $hero; That is, the 2nd character node of the next from the point of address 0x1234 to point 3rd person node address 0x345, the figure of the ③ virtual Blue line shown, the original ② solid line disconnected.
This puts number 3rd in the back of Figure 2nd.
There is no good way to see the light is not learning, and self-tapping code, their own drawing analysis, deepen understanding.
Plus the improved features:Do not let heroes with the same rank join the list.
Modify the while loop code in the singlelink3.php as follows:
singlelink4.php
One-way linked list to complete the hero ranking management
Query Heroes Add Heroes Delete Heroes Change Heroes
no= $no $this->name= $name $this->nickname= $nickname;} Create a head header, the head is just a header, do not put the data $head=new Hero ();//write a function specifically for adding hero function Addhero ($head, $hero) {//1. Directly at the end of the list Add// To find the end of the list, do not move $head; $cur = $head;//2 in accordance with the hero's ranks (here I want to be able to guarantee the order of the list)//ideas: must first talk about ideas, and then write code $flag=false; Indicates that there is no duplicate number while ($cur->next!=null) {if ($cur->next->no> $hero->no) {//location was found break;} else if ($cur->next->no== $hero->no) {$flag =true;//If you enter here, it means duplicate, set to Trueecho '
Cannot rob position, '. $hero->no. ' Location already has someone ';} Continue to $cur= $cur->next;} When exiting the while loop, position find//join//Let Hero join if ($flag ==false) {///only when $flag is false, the description does not encounter duplicates before the insert is executed. If the $FLAG flag bit is not added, although the else above is judged, it encounters duplicates, but still inserts. $hero->next= $cur->next; $cur->next= $hero;} A single-linked list of traversal, must start from head node to find//is from the head start to traverse, $head the value of the head must not change, changes can not traverse our single-linked list of function Showheros ($head) {//traversal [must know when, To the end of the list]//here in order not to change the direction of $head, we can use a temporary variable $cur= $head; while ($cur->next!=null) {//The first node is a header, so $cur-> Next refers to the next node, where nothing in the head node is empty echo '
The hero's number is '. $cur->next->no. ' Name '. $cur->next->name. ' nickname = '. $cur->next->nickname;//If it's written here, it's going to be a dead loop./ All to let $cul move $cur= $cur->next;}} Add $hero=new Hero (1, ' Song Jiang ', ' timely rain '), Addhero ($head, $hero), $hero =new Hero (2, ' Lu Junyi ', ' Jade Kylin '), Addhero ($head, $hero); $hero = New Hero (6, ' Lin ', ' Leopard Head '), Addhero ($head, $hero), $hero =new Hero (3, ' Wu ', ' mastermind '); Addhero ($head, $hero); $hero =new Hero (1, ' Song Jiang ', ' timely rain '); Addhero ($head, $hero); $hero =new Hero (2, ' Lu Junyi ', ' Jade Kylin '), Addhero ($head, $hero);//In the output, Lin is in front of Wu, and Lin is number 6th , should be in the back of Wu use, this is directly in the list of the last added drawbacks. Echo
The current hero ranking situation is ************** '; Showheros ($head);? >
The above is the increase function.
=============
we will now add the Delete function:
Analysis diagram
Big picture, open the picture in a new window, watch the full picture
now there are 3 character nodes to remove the number 3rd node.
(1) First, there is a variable cur point to the head node, that is, $cur= $head; $cur->next!=null, enter the while loop, make if judgment, $cur->next->no to 1, then and $ Herono is not equal to 3, cur down one step.
(2) At this time cur point to the number 1th character node, $cur->next!=null set up, into the while loop, if judged,
$cur->next->no is 3, and $herono is equal to 3, the position is found and the break jumps out of the while loop. I want to take this number 3rd figure node off, how should I take it? Assuming that the number 3rd character node address is 0x123,7 number of people node address is 0x456, now is to change the number 1th character node's next value to 0x456, so that the number 3rd character node is not in this single linked list. Some people will have doubts: the number 3rd character node is not also the next value, pointing to the 0x456 is pointing to the number 7th character node, do not worry, this time the 3rd character node is already a garbage object, because in PHP, Java and C # has provisions, if an object does not have any reference to it, it is garbage, It must be clear that the garbage collection system will collect the number 3rd node,
This is the idea of taking out the number 3rd character node.
Delete Code
$cur->next= $cur->next->next;
Note:Will there be a problem when you just delete the end?
Delete the last node, that is, to remove the number 7th, then cur to the number 3rd character node, at this time $cur->next->next is the 7th character node of the next property value, the 7th character node is the last node, its next property value is NULL, then $ cur->next= $cur->next->next; is to set the value of the next attribute of the number 3rd character node to null, then the 3rd character node is the last node is at the end.
singlelink5.php
One-way linked list to complete the hero ranking management
Query Heroes Add Heroes Delete Heroes Change Heroes
no= $no $this->name= $name $this->nickname= $nickname;} Create a head header, the head is just a header, do not put the data $head=new Hero ();//write a function specifically for adding hero function Addhero ($head, $hero) {//1. Directly at the end of the list Add// To find the end of the list, do not move $head; $cur = $head;//2 in accordance with the hero's ranks (here I want to be able to guarantee the order of the list)//ideas: must first talk about ideas, and then write code $flag=false; Indicates that there is no duplicate number while ($cur->next!=null) {if ($cur->next->no> $hero->no) {//location was found break;} else if ($cur->next->no== $hero->no) {$flag =true;//If you enter here, it means duplicate, set to Trueecho '
Cannot rob position, '. $hero->no. ' Location already has someone ';} Continue to $cur= $cur->next;} When exiting the while loop, position find//join//Let Hero join if ($flag ==false) {///only when $flag is false, the description does not encounter duplicates before the insert is executed. If the $FLAG flag bit is not added, although the else above is judged, it encounters duplicates, but still inserts. $hero->next= $cur->next; $cur->next= $hero;} A single-linked list of traversal, must start from head node to find//is from the head start to traverse, $head the value of the head must not change, changes can not traverse our single-linked list of function Showheros ($head) {//traversal [must know when, To the end of the list]//here in order not to change the direction of $head, we can use a temporary variable $cur= $head; while ($cur->next!=null) {//The first node is a header, so $cur-> Next refers to the next node, where nothing in the head node is empty echo '
The hero's number is '. $cur->next->no. ' Name '. $cur->next->name. ' nickname = '. $cur->next->nickname;//If it's written here, it's going to be a dead loop./ All to let $cul move $cur= $cur->next;}} Remove a Hero function Delhero ($head, $herono) from the list {//first to find out where the hero $cur= $head;//Let $cur point to $head; $flag =false;//Flag bit, Suppose you do not find the while ($cur->next!=null) {if ($cur->next->no== $herono) {$flag =true;//Enter if statement to find, set to true//find $ The next node of the Cur is the node that should be deleted. break;} $cur = $cur->next;} if ($flag) {//If flag is true, delete//delete $cur->next= $cur->next->next;} Else{echo '
No number of heroes you want to delete '. $herono;}} Add $hero=new Hero (1, ' Song Jiang ', ' timely rain '), Addhero ($head, $hero), $hero =new Hero (2, ' Lu Junyi ', ' Jade Kylin '), Addhero ($head, $hero); $hero = New Hero (6, ' Lin ', ' Leopard Head '), Addhero ($head, $hero), $hero =new Hero (3, ' Wu ', ' mastermind '); Addhero ($head, $hero); $hero =new Hero (1, ' Song Jiang ', ' timely rain '); Addhero ($head, $hero); $hero =new Hero (2, ' Lu Junyi ', ' Jade Kylin '), Addhero ($head, $hero);//In the output, Lin is in front of Wu, and Lin is number 6th , should be in the back of Wu use, this is directly in the list of the last added drawbacks. Echo
The current hero ranking situation is ************** '; Showheros ($head); Echo '
The post-deletion hero ranking is ************** ';d elhero ($head, 1);d Elhero ($head, 21); Delete a non-existent showheros ($head); >
========
continue to add modification features:
singlelink6.php
One-way linked list to complete the hero ranking management
Query Heroes Add Heroes Delete Heroes Change Heroes
no= $no $this->name= $name $this->nickname= $nickname;} Create a head header, the head is just a header, do not put the data $head=new Hero ();//write a function specifically for adding hero function Addhero ($head, $hero) {//1. Directly at the end of the list Add// To find the end of the list, do not move $head; $cur = $head;//2 in accordance with the hero's ranks (here I want to be able to guarantee the order of the list)//ideas: must first talk about ideas, and then write code $flag=false; Indicates that there is no duplicate number while ($cur->next!=null) {if ($cur->next->no> $hero->no) {//location was found break;} else if ($cur->next->no== $hero->no) {$flag =true;//If you enter here, it means duplicate, set to Trueecho '
Cannot rob position, '. $hero->no. ' Location already has someone ';} Continue to $cur= $cur->next;} When exiting the while loop, position find//join//Let Hero join if ($flag ==false) {///only when $flag is false, the description does not encounter duplicates before the insert is executed. If the $FLAG flag bit is not added, although the else above is judged, it encounters duplicates, but still inserts. $hero->next= $cur->next; $cur->next= $hero;} A single-linked list of traversal, must start from head node to find//is from the head start to traverse, $head the value of the head must not change, changes can not traverse our single-linked list of function Showheros ($head) {//traversal [must know when, To the end of the list]//here in order not to change the direction of $head, we can use a temporary variable $cur= $head; while ($cur->next!=null) {//The first node is a header, so $cur-> Next refers to the next node, where nothing in the head node is empty echo '
The hero's number is '. $cur->next->no. ' Name '. $cur->next->name. ' nickname = '. $cur->next->nickname;//If it's written here, it's going to be a dead loop./ All to let $cul move $cur= $cur->next;}} Remove a Hero function Delhero ($head, $herono) from the list {//first to find out where the hero $cur= $head;//Let $cur point to $head; $flag =false;//Flag bit, Suppose you do not find the while ($cur->next!=null) {if ($cur->next->no== $herono) {$flag =true;//Enter if statement to find, set to true//find $ The next node of the Cur is the node that should be deleted. break;} $cur = $cur->next;} if ($flag) {//If flag is true, delete//delete $cur->next= $cur->next->next;} Else{echo '
No number of heroes you want to delete '. $herono;}} Modify Hero Function Updatehero ($head, $hero) {//To find the hero $cur= $head First,//$cur is the walk-in while ($cur->next!=null) {if ($cur- >next->no== $hero->no) {break;} Keep going. $cur= $cur->next;} This time does not use the flag flag bit//when exiting the while loop, if the $cur->next==null description cur to the end of the queue, nor found if ($cur->next==null) {echo '
You need to modify the '. $hero->no. ' Not present ';} else{//number cannot be modified, only name and nickname $cur->next->name= $hero->name; $cur->next->nickname= $hero->nickname;} Add $hero=new Hero (1, ' Song Jiang ', ' timely rain '), Addhero ($head, $hero), $hero =new Hero (2, ' Lu Junyi ', ' Jade Kylin '), Addhero ($head, $hero); $hero = New Hero (6, ' Lin ', ' Leopard Head '), Addhero ($head, $hero), $hero =new Hero (3, ' Wu ', ' mastermind '); Addhero ($head, $hero); $hero =new Hero (1, ' Song Jiang ', ' timely rain '); Addhero ($head, $hero); $hero =new Hero (2, ' Lu Junyi ', ' Jade Kylin '), Addhero ($head, $hero);//In the output, Lin is in front of Wu, and Lin is number 6th , should be in the back of Wu use, this is directly in the list of the last added drawbacks. Echo
The current hero ranking situation is ************** '; Showheros ($head); Echo '
The post-deletion hero ranking is ************** ';//delhero ($head, 1);d Elhero ($head, 21); Delete a nonexistent showheros ($head); Echo '
The modified hero ranking situation is ************** '; $hero =new Hero (1, ' am ', ' right white Tiger '), Updatehero ($head, $hero); Showheros ($head);? >
The key is to use,such as the use of ring linked list to solve the problem of lost handkerchief.
Why is there no thought,Is that there is no algorithm and data structure.
Put this single-linked list of cases,
knock yourself down ., you'll find it's not so hard.
using a two-way linked list with head head-the margin of Heroes leaderboard Management
disadvantage analysis of one-way linked list:Can not be self-deleted, need to rely on the secondary node.
and a doubly linked list can be self-deleted,At the same time in the binary tree, the generalized table needs to use to one node to perform two or more nodes of the application!
Hanshunping _php Programmer Play Algorithm Public Lesson _ Learning Note _ Source code diagram _ppt Document Sorting _ Directory