Han Shunping _ PHP software engineer fun algorithm open course (first season) 03 _ single-chain table crud operations _ NLP hero ranking algorithm _ learning notes _ source code illustration _ PPT document sorting

Source: Internet
Author: User
Tags php software
Han Shunping _ PHP programmer fun algorithm open course (first season) 03 _ single-chain table crud operations _ NLP hero ranking algorithm _ learning NOTE _ source code illustration _ PPT document sorting text West Malone: http://blog.csdn.net/wenximalong/singleLink.php<html&gt Han Shunping _ PHP programmer fun algorithm open class (the first season) 03 _ single-chain table crud operations _ hero ranking algorithm _ learning notes _ source code illustration _ PPT document finishing

Wen Xi Malone: http://blog.csdn.net/wenximalong/


SingleLink. php

 One-Way linked list for hero ranking management
 Query heroes add heroes delete heroes modify heroes
 No = $ no; $ this-> name = $ name; $ this-> nickname = $ nickname ;}// create a head header, which is just a header, do not put the data $ head = new Hero (); // create a hero $ Hero = new Hero (1, 'songjiang ', 'timely Rain '); // link $ head-> next = $ hero; $ hero2 = new Hero (2, 'Lu Junyi ', 'Yu Qilin '); /// link // The method of comparison 2 is used now, which is improved immediately to facilitate understanding of $ hero-> next = $ hero2; // traversal of a single-chain table, you must start from the head node. // The value of the $ head header cannot be changed, function showHeros ($ head) {// traversal [when must I know, at the end of the linked list] // to avoid changing the direction of $ head, we can use a temporary variable $ cur = $ head; whi Le ($ cur-> next! = Null) {// The first node is the header node, so use $ cur-> next to point to the next node, and there is no blank echo in the header node'
The hero number is '. $ cur-> next-> no. 'name '. $ cur-> next-> name. 'Nickname = '. $ cur-> next-> nickname; // If you only write it here, it will be an endless loop // all the elements that need to move $ cul $ cur = $ cur-> next;} echo'
* ********** The current ranking of heroes is **************'; showHeros ($ head);?>
In the code above, a stupid method is used to add a hero, Now we can use a better method to add a job.
1. Add directly at the end of the linked list

SingleLink2.php

 One-Way linked list for hero ranking management
 Query heroes add heroes delete heroes modify heroes
 No = $ no; $ this-> name = $ name; $ this-> nickname = $ nickname ;}// create a head header, which is just a header, do not put the data $ head = new Hero (); // write a function dedicated to adding the hero function addHero ($ head, $ Hero) {// 1. add // directly at the end of the linked list to find the linked list. do not change $ head; $ cur = $ head; while ($ cur-> next! = Null) {$ cur = $ cur-> next;} // when the while loop is exited, $ cur is the end of the linked list. // add hero $ cur-> next = $ hero; // 2. add by ranking by hero (here I want to ensure the order of the linked list)} // For the traversal of a single-chain table, you must start from the head node and start to find // The traversal from the head, the value of the $ head header cannot be changed. after the change, the value of the function showHeros ($ head) in our single-chain table cannot be traversed {// traversal [when must we know, at the end of the linked list] // Here, we can use a temporary variable $ cur = $ head; while ($ cur-> next! = Null) {// The first node is the header node, so use $ cur-> next to point to the next node, and there is no blank echo in the header node'
The hero number is '. $ cur-> next-> no. 'name '. $ cur-> next-> name. 'Nickname = '. $ cur-> next-> nickname; // If you only write it here, it will be an endless loop // all the elements that need to move $ cul $ cur = $ cur-> next ;}// add $ hero = new Hero (1, 'songjiang ', 'timely Rain'); addHero ($ head, $ hero); $ hero = new Hero (2, 'Lu Junyi ', 'Yu Qilin'); addHero ($ head, $ hero ); $ hero = new Hero (6, 'Lin Chong', 'leopard head'); addHero ($ head, $ hero); $ hero = new Hero (3, 'Wu Yong ', 'zhi duoxing'); addHero ($ head, $ hero); // in the output, Lin Chong is in front of Wu Yong, while Lin Chong is in No. 6, it should be placed behind Wu Yong, which is the disadvantage of directly adding at the end of the linked list. Echo'
* ********** The current ranking of heroes is **************'; showHeros ($ head);?>



2. add the list of heroes (here I want to ensure the order of the linked list)
In the future, when you join the work, you may have this kind of requirement. someone else will give you a bunch of strings, so that you can sort the strings according to a certain attribute, and sort them in the memory.
How to add them in order?

★You must first talk about your ideas and then write code.Analysis Chart

The picture is large. open the picture in a new window to view the complete picture.


Let's assume that we have an extreme example.
(1) add the No. 1 character first. First, the cur points to the head node, and the cur-> next is empty. add the No. 1 character directly to the back of the head node;
(2) add the number 2 character. first, cur points to the head node, and cur-> next points to the number 1 character node, cur-> next-> no is 1 and $ hero-> no is 2. we found that 1 is not greater than 2, so cur takes the following step, at this time, cur points to the character node 1, and then judges whether cur-> next-> no is greater than 2, because cur-> next is empty at this time, and you cannot leave, only the number 2 character can be added behind the number 1 character node.
(3) add the number 6 character. first, cur points to the head node, and cur-> next points to the number 1 character node, cur-> next-> no is 1 and $ hero-> next is 6. we found that 1 is not greater than 6, so cur takes the following step, at this point, cur points to the character node 1, and then compares cur-> next-> no with 2 and $ hero-> no with 6. it is found that 2 is not greater than 6, therefore, cur takes the following step. at this time, cur points to the character Node 2, and then judges whether cur-> next-> no is greater than 6, because cur-> next is empty at this time, you can only add the number 6 to the end of the number 2 node.
(4) add the third character. first, cur points to the head node, and cur-> next points to the first character node, cur-> next-> no is 1 and $ hero-> next is 3. we found that 1 is not greater than 3, so cur takes the following step, at this point, cur points to the character node 1, and then compares cur-> next-> no with 2 and $ hero-> no with 3. it is found that 2 is not greater than 3, therefore, cur will take the following step. at this time, cur points to the character Node 2, and then judges whether cur-> next-> no is 6 and $ hero-> no is 3 for comparison, after finding that the number 6 is greater than 3, locate the position and try to add the number 3.
SingleLink3.php

 One-Way linked list for hero ranking management
 Query heroes add heroes delete heroes modify heroes
 No = $ no; $ this-> name = $ name; $ this-> nickname = $ nickname ;}// create a head header, which is just a header, do not put the data $ head = new Hero (); // write a function dedicated to adding the hero function addHero ($ head, $ Hero) {// 1. add // directly at the end of the linked list to find the linked list. do not change $ head; $ cur = $ head; // 2. add the rankings by heroes (here I hope to ensure the order of the linked list) // thought: first talk about the idea, then write the code while ($ cur-> next! = Null) {if ($ cur-> next-> no >=$ hero-> no) {// locate break; // once break is found, it jumps out of the while loop} // continues $ cur = $ cur-> next;} // when exiting the while loop, locate the location and add // add hero to $ hero-> next = $ cur-> next; $ cur-> next = $ hero;} // traverse a single-chain table, you must start from the head node. // The value of the $ head header cannot be changed, function showHeros ($ head) {// traversal [when must I know, at the end of the linked list] // Here, we can use a temporary variable $ cur = $ head; while ($ cur-> next! = Null) {// The first node is the header node, so use $ cur-> next to point to the next node, and there is no blank echo in the header node'
The hero number is '. $ cur-> next-> no. 'name '. $ cur-> next-> name. 'Nickname = '. $ cur-> next-> nickname; // If you only write it here, it will be an endless loop // all the elements that need to move $ cul $ cur = $ cur-> next ;}// add $ hero = new Hero (1, 'songjiang ', 'timely Rain'); addHero ($ head, $ hero); $ hero = new Hero (2, 'Lu Junyi ', 'Yu Qilin'); addHero ($ head, $ hero ); $ hero = new Hero (6, 'Lin Chong', 'leopard head'); addHero ($ head, $ hero); $ hero = new Hero (3, 'Wu Yong ', 'zhi duoxing'); addHero ($ head, $ hero); // in the output, Lin Chong is in front of Wu Yong, while Lin Chong is in No. 6, it should be placed behind Wu Yong, which is the disadvantage of directly adding at the end of the linked list. Echo'
* ********** The current ranking of heroes is **************'; showHeros ($ head);?>

Execution Process Analysis Diagram

The picture is large. open the picture in a new window to view the complete picture.


As shown in,When a third character is added, cur points to the second character node, and cur-> next points to the address 0x1234, that is, the sixth character node, the address of character 3 is 0x345. Execute $ hero-> next = $ cur-> next, that is, let the next node of the character 3 point to the node address of the character 6 0x1234, as shown in figure ① virtual blue line; then $ cur-> next = $ hero; that is, change the next of the character node 2 from pointing to address 0x1234 to point to address 0x345 of the character Node 3, as shown in figure ③ dotted blue line, the original ② solid line is disconnected.
In this way, the third character is inserted behind the second character.
There is no good way. you can't simply learn it. Instead, you can tap on the code and draw and analyze your own images to deepen your understanding.

Added improved functions:
Do not add heroes with the same ranking to the linked list.

Modify the while loop code in singleLink3.php as follows:
SingleLink4.php

 One-Way linked list for hero ranking management
 Query heroes add heroes delete heroes modify heroes
 No = $ no; $ this-> name = $ name; $ this-> nickname = $ nickname ;}// create a head header, which is just a header, do not put the data $ head = new Hero (); // write a function dedicated to adding the hero function addHero ($ head, $ Hero) {// 1. add // directly at the end of the linked list to find the linked list. do not change $ head; $ cur = $ head; // 2. add the rankings by heroes (here I hope to ensure the order of the linked list) // idea: first talk about ideas and then write the code $ flag = false; // indicates no duplicate number while ($ cur-> next! = Null) {if ($ cur-> next-> no> $ hero-> no) {// locate break ;} else if ($ cur-> next-> no ==$ hero-> no) {$ flag = true; // if you enter this field, it indicates that there are duplicates, set to trueecho'
Cannot grab location ,'. $ hero-> no. 'location already exists';} // Continue $ cur = $ cur-> next;} // when exiting the while loop, location found // add hero to if ($ flag = false) {// if $ flag is false, it indicates that no duplicate occurs, before the insert operation. If the $ flag is not added, although the above else if is determined, it still inserts a duplicate. $ Hero-> next = $ cur-> next; $ cur-> next = $ hero;} // traversal of a single-chain table, you must start from the head node. // The value of the $ head header cannot be changed, function showHeros ($ head) {// traversal [when must I know, at the end of the linked list] // Here, we can use a temporary variable $ cur = $ head; while ($ cur-> next! = Null) {// The first node is the header node, so use $ cur-> next to point to the next node, and there is no blank echo in the header node'
The hero number is '. $ cur-> next-> no. 'name '. $ cur-> next-> name. 'Nickname = '. $ cur-> next-> nickname; // If you only write it here, it will be an endless loop // all the elements that need to move $ cul $ cur = $ cur-> next ;}// add $ hero = new Hero (1, 'songjiang ', 'timely Rain'); addHero ($ head, $ hero); $ hero = new Hero (2, 'Lu Junyi ', 'Yu Qilin'); addHero ($ head, $ hero ); $ hero = new Hero (6, 'Lin Chong', 'leopard head'); addHero ($ head, $ hero); $ hero = new Hero (3, 'Wu Yong ', 'zhi duo Xing'); addHero ($ head, $ hero); $ hero = new Hero (1, 'songjiang ', 'timely Rain'); addHero ($ head, $ hero ); $ hero = new Hero (2, 'Lu Junyi ', 'Yu Qilin '); AddHero ($ head, $ hero); // in the output, Lin Chong is in front of Wu Yong, and Lin Chong is in front of No. 6, which should be placed behind Wu Yong, this is the disadvantage of directly adding a linked list. Echo'
* ********** The current ranking of heroes is **************'; showHeros ($ head);?>
The above is the addition of features.
==================
Now we can add the deletion function:

Analysis Chart

The picture is large. open the picture in a new window to view the complete picture.


Now there are three character nodes. you need to delete the character Node 3.
(1) first, a variable cur points to the head node, that is, $ cur = $ head; $ cur-> next! = Null is true. after entering the while loop, perform if judgment. if $ cur-> next-> no is 1, it is not equal to $ herono 3, and cur goes down one step.
(2) now cur points to the character node 1, $ cur-> next! = Null is true. it enters the while loop and determines if,
If $ cur-> next-> no is 3, it is equal to $ herono. when the position is found, the break jumps out of the while loop. We need to remove this node number 3. how should we take it? Assume that the node address of the third character is 0x123, and the node address of the third character is 0x456. now, you need to change the next value of the first character node to 0x456, in this way, Node 3 is not in this single-link list. Some people may wonder: isn't the character Node 3 having a next value, pointing to 0x456 or pointing to the character node 7? don't worry. at this time, the character Node 3 is already a junk object, because php, java, and c # all stipulate that if an object does not have any reference pointing to it, it will be junk, and this 1.1 must be clear, the garbage collection mechanism recycles Node 3, This is the idea of removing Node 3.
Delete code

$ Cur-> next = $ cur-> next;
Note:Will there be a problem when you just delete the end?
Delete the last node, that is, to delete the number 7 character, then cur locates the number 3 character node. at this time, $ cur-> next is the next attribute value of the number 7 character node, the character node 7 is the last node, and its next attribute value is null, then $ cur-> next = $ cur-> next; if the next attribute value of node 3 is set to null, the Node 3 is the last node and the end.

SingleLink5.php

 One-Way linked list for hero ranking management
 Query heroes add heroes delete heroes modify heroes
 No = $ no; $ this-> name = $ name; $ this-> nickname = $ nickname ;}// create a head header, which is just a header, do not put the data $ head = new Hero (); // write a function dedicated to adding the hero function addHero ($ head, $ Hero) {// 1. add // directly at the end of the linked list to find the linked list. do not change $ head; $ cur = $ head; // 2. add the rankings by heroes (here I hope to ensure the order of the linked list) // idea: first talk about ideas and then write the code $ flag = false; // indicates no duplicate number while ($ cur-> next! = Null) {if ($ cur-> next-> no> $ hero-> no) {// locate break ;} else if ($ cur-> next-> no ==$ hero-> no) {$ flag = true; // if you enter this field, it indicates that there are duplicates, set to trueecho'
Cannot grab location ,'. $ hero-> no. 'location already exists';} // Continue $ cur = $ cur-> next;} // when exiting the while loop, location found // add hero to if ($ flag = false) {// if $ flag is false, it indicates that no duplicate occurs, before the insert operation. If the $ flag is not added, although the above else if is determined, it still inserts a duplicate. $ Hero-> next = $ cur-> next; $ cur-> next = $ hero;} // traversal of a single-chain table, you must start from the head node. // The value of the $ head header cannot be changed, function showHeros ($ head) {// traversal [when must I know, at the end of the linked list] // Here, we can use a temporary variable $ cur = $ head; while ($ cur-> next! = Null) {// The first node is the header node, so use $ cur-> next to point to the next node, and there is no blank echo in the header node'
The hero number is '. $ cur-> next-> no. 'name '. $ cur-> next-> name. 'Nickname = '. $ cur-> next-> nickname; // If you only write it here, it will be an endless loop. // all the functions that need to move $ cul $ cur = $ cur-> next ;}// delete a hero function delHero ($ head, $ herono) {// first, locate where the hero is located $ cur = $ head; // point $ cur to $ head; $ flag = false; // flag, assume that the while ($ cur-> next! = Null) {if ($ cur-> next-> no ==$ herono) {$ flag = true; // the if statement is found, set to true // find the next node of $ cur to be deleted. Break;} $ cur = $ cur-> next;} if ($ flag) {// if the flag is true, delete/delete $ cur-> next = $ cur-> next;} else {echo'
No hero ID you want to delete '. $ herono ;}/// add $ hero = new Hero (1, 'songjiang ', 'timely Rain'); addHero ($ head, $ hero ); $ hero = new Hero (2, 'Lu Junyi ', 'Yu Qilin'); addHero ($ head, $ hero); $ hero = new Hero (6, 'Lin Chong ', 'leopard head'); addHero ($ head, $ hero); $ hero = new Hero (3, 'wuyun ', 'zhiduo start'); addHero ($ head, $ hero ); $ hero = new Hero (1, 'jiang', 'timely Rain'); addHero ($ head, $ hero); $ hero = new Hero (2, 'Lu Junyi ', 'yu Qilin '); addHero ($ head, $ hero); // in the output, Lin Chong is in front of Wu Yong, while Lin Chong is in No. 6, it should be placed behind Wu Yong, which is the disadvantage of directly adding at the end of the linked list. Echo'
* ********************** '; ShowHeros ($ head ); echo'
* ********************** '; DelHero ($ head, 1); delHero ($ head, 21); // delete a nonexistent showHeros ($ head);?>
==========
Continue to add the modification function:

SingleLink6.php

 One-Way linked list for hero ranking management
 Query heroes add heroes delete heroes modify heroes
 No = $ no; $ this-> name = $ name; $ this-> nickname = $ nickname ;}// create a head header, which is just a header, do not put the data $ head = new Hero (); // write a function dedicated to adding the hero function addHero ($ head, $ Hero) {// 1. add // directly at the end of the linked list to find the linked list. do not change $ head; $ cur = $ head; // 2. add the rankings by heroes (here I hope to ensure the order of the linked list) // idea: first talk about ideas and then write the code $ flag = false; // indicates no duplicate number while ($ cur-> next! = Null) {if ($ cur-> next-> no> $ hero-> no) {// locate break ;} else if ($ cur-> next-> no ==$ hero-> no) {$ flag = true; // if you enter this field, it indicates that there are duplicates, set to trueecho'
Cannot grab location ,'. $ hero-> no. 'location already exists';} // Continue $ cur = $ cur-> next;} // when exiting the while loop, location found // add hero to if ($ flag = false) {// if $ flag is false, it indicates that no duplicate occurs, before the insert operation. If the $ flag is not added, although the above else if is determined, it still inserts a duplicate. $ Hero-> next = $ cur-> next; $ cur-> next = $ hero;} // traversal of a single-chain table, you must start from the head node. // The value of the $ head header cannot be changed, function showHeros ($ head) {// traversal [when must I know, at the end of the linked list] // Here, we can use a temporary variable $ cur = $ head; while ($ cur-> next! = Null) {// The first node is the header node, so use $ cur-> next to point to the next node, and there is no blank echo in the header node'
The hero number is '. $ cur-> next-> no. 'name '. $ cur-> next-> name. 'Nickname = '. $ cur-> next-> nickname; // If you only write it here, it will be an endless loop. // all the functions that need to move $ cul $ cur = $ cur-> next ;}// delete a hero function delHero ($ head, $ herono) {// first, locate where the hero is located $ cur = $ head; // point $ cur to $ head; $ flag = false; // flag, assume that the while ($ cur-> next! = Null) {if ($ cur-> next-> no ==$ herono) {$ flag = true; // the if statement is found, set to true // find the next node of $ cur to be deleted. Break;} $ cur = $ cur-> next;} if ($ flag) {// if the flag is true, delete/delete $ cur-> next = $ cur-> next;} else {echo'
No hero ID you want to delete '. $ herono ;}/// modify hero function updateHero ($ head, $ hero) {// first find this hero $ cur = $ head; // $ cur is the while ($ cur-> next! = Null) {if ($ cur-> next-> no ==$ hero-> no) {break ;} // continue with $ cur = $ cur-> next;} // this time, the flag is not used. // after exiting the while loop, if $ cur-> next = null indicates that cur is at the end of the team, if ($ cur-> next = null) {echo 'is not found'
You need to modify '. $ hero-> no. 'Nonexistent ';} else {// The ID cannot be modified. you can only modify the name and nickname $ cur-> next-> name = $ hero-> name; $ cur-> next-> nickname = $ hero-> nickname;} // add $ hero = new Hero (1, 'songjiang ', 'timely Rain '); addHero ($ head, $ hero); $ hero = new Hero (2, 'Lu Junyi ', 'Yu Qilin'); addHero ($ head, $ hero ); $ hero = new Hero (6, 'Lin Chong', 'leopard head'); addHero ($ head, $ hero); $ hero = new Hero (3, 'Wu Yong ', 'zhi duo Xing'); addHero ($ head, $ hero); $ hero = new Hero (1, 'songjiang ', 'timely Rain'); addHero ($ head, $ hero ); $ hero = new Hero (2, 'Lu Junyi ', 'Yu Qilin '); AddHero ($ head, $ hero, this is the disadvantage of directly adding a linked list. Echo'
* ********************** '; ShowHeros ($ head ); echo'
* *********** The deleted hero ranking is **************'; // delHero ($ head, 1); delHero ($ head, 21); // delete a nonexistent showHeros ($ head); echo'
**************'; $ Hero = new Hero (1, 'Hu Han 3', 'right White Tiger '); updateHero ($ head, $ hero); showHeros ($ head);?>
The key is application,For example, use a circular linked list to solve the handkerchief loss problem.
Why is there no idea,There is no algorithm or data structure.
In this case, Click it on your own, You will find that it is not so difficult.

Use a two-way linked list with a head header to implement-hero ranking management

Disadvantages of one-way linked list:It cannot be deleted by itself. secondary nodes are required.
The two-way linked list can be deleted by itself,At the same time, in binary trees and generalized tables, one node must be used to execute two or more nodes!


Han Shunping _ PHP programmer fun algorithm open course _ learning notes _ source code illustration _ PPT document arrangement _ Directory


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.