[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]
In the first two blogs, we discussed the algorithm of the prim minimal spanning tree and were familiar with the basic process. Basically, we write code in the top-down order. First, we build an architecture, and then complete each of the sub-functions step by step, so as to form a complete prim algorithm computing process.
F) Delete the non-conforming data in the DIR_LINE queue, mainly the DIR_LINE data that has been accessed by both nodes.
Void delete_unvalid_line_from_list (DIR_LINE ** ppHead, MINI_GENERATE_TREE * pMiniTree)
{
DIR_LINE * prev;
DIR_LINE * pcur;
STATUS result;
Prev = NULL;
Pcur = * ppHead;
While (pcur ){
If (! Check_valid_for_line (pcur, pMiniTree )){
Result = delete_line_from_queue (ppHead, pcur );
Assert (TRUE = result );
If (NULL = prev)
Pcur = * ppHead;
Else
Pcur = prev-> next;
Continue;
}
Prev = pcur;
Pcur = pcur-> next;
}
Return;
}
Void delete_unvalid_line_from_list (DIR_LINE ** ppHead, MINI_GENERATE_TREE * pMiniTree)
{
DIR_LINE * prev;
DIR_LINE * pcur;
STATUS result;
Prev = NULL;
Pcur = * ppHead;
While (pcur ){
If (! Check_valid_for_line (pcur, pMiniTree )){
Result = delete_line_from_queue (ppHead, pcur );
Assert (TRUE = result );
If (NULL = prev)
Pcur = * ppHead;
Else
Pcur = prev-> next;
Continue;
}
Prev = pcur;
Pcur = pcur-> next;
}
Return;
}
G) f) The function used to determine the validity of DIR_LINE needs to be improved.
Int check_valid_for_line (DIR_LINE * pDirLine, MINI_GENERATE_TREE * pMiniTree)
{
Int index;
Int flag_start;
Int flag_end;
Flag_start = 0;
Flag_end = 0;
For (index = 0; index <pMiniTree-> node_num; index ++ ){
If (pDirLine-> start = pMiniTree-> pNode [index]) {
Flag_start = 1;
Break;
}
}
For (index = 0; index <pMiniTree-> node_num; index ++ ){
If (pDirLine-> end = pMiniTree-> pNode [index]) {
Flag_end = 1;
Break;
}
}
Return (1 = flag_start & 1 = flag_end )? 0: 1;
}
Int check_valid_for_line (DIR_LINE * pDirLine, MINI_GENERATE_TREE * pMiniTree)
{
Int index;
Int flag_start;
Int flag_end;
Flag_start = 0;
Flag_end = 0;
For (index = 0; index <pMiniTree-> node_num; index ++ ){
If (pDirLine-> start = pMiniTree-> pNode [index]) {
Flag_start = 1;
Break;
}
}
For (index = 0; index <pMiniTree-> node_num; index ++ ){
If (pDirLine-> end = pMiniTree-> pNode [index]) {
Flag_end = 1;
Break;
}
}
Return (1 = flag_start & 1 = flag_end )? 0: 1;
}
H) The last step is to sort the DIR_LINE data that is already in the queue, which is actually the chain table sorting.
Void insert_for_sort_operation (DIR_LINE ** ppNode, DIR_LINE * pNode)
{
DIR_LINE * prev;
DIR_LINE * cur;
/* Insert pNode before the first data */
If (pNode-> weight <(* ppNode)-> weight ){
PNode-> next = * ppNode;
* PpNode = pNode;
Return;
}
Cur = * ppNode;
While (cur ){
If (pNode-> weight <cur-> weight)
Break;
Prev = cur;
Cur = cur-> next;
}
PNode-> next = prev-> next;
Prev-> next = pNode;
Return;
}
Void sort_for_line_list (DIR_LINE ** ppNode)
{
DIR_LINE * prev;
DIR_LINE * curr;
If (NULL = ppNode | NULL = * ppNode)
Return;
Curr = (* ppNode)-> next;
(* PpNode)-> next = NULL;
While (curr ){
Prev = curr;
Curr = curr-> next;
Insert_for_sort_operation (ppNode, prev );
}
}
Void insert_for_sort_operation (DIR_LINE ** ppNode, DIR_LINE * pNode)
{
DIR_LINE * prev;
DIR_LINE * cur;
/* Insert pNode before the first data */
If (pNode-> weight <(* ppNode)-> weight ){
PNode-> next = * ppNode;
* PpNode = pNode;
Return;
}
Cur = * ppNode;
While (cur ){
If (pNode-> weight <cur-> weight)
Break;
Prev = cur;
Cur = cur-> next;
}
PNode-> next = prev-> next;
Prev-> next = pNode;
Return;
}
Void sort_for_line_list (DIR_LINE ** ppNode)
{
DIR_LINE * prev;
DIR_LINE * curr;
If (NULL = ppNode | NULL = * ppNode)
Return;
Curr = (* ppNode)-> next;
(* PpNode)-> next = NULL;
While (curr ){
Prev = curr;
Curr = curr-> next;
Insert_for_sort_operation (ppNode, prev );
}
}
Algorithm summary:
1) there is still room for improvement in the algorithm itself. For example, whether the DIR_LINE queue needs to be rebuilt every time the memory is allocated remains to be discussed.
2) algorithm writing is not in place. It is normal to write algorithms for four or five times.
3) it is best to modify and test code while writing it. This increases the robustness of the code and improves your confidence.
4) If possible, you can reuse the previously written and stable algorithm code, such as sorting, searching, stack, binary tree, and other code.