/*linked_list.c -- 鏈表實現檔案*/<br />#include <stdio.h><br />#include <stdlib.h><br />#include "linked_list.h"</p><p>/*局部函式宣告*/</p><p>static Linked_List_Node * Make_Node (const Linked_List_Item li1, const Linked_List_Item li2) ;</p><p>/*介面函數定義*/</p><p>int Initialize_L (List * const pli)<br />{<br />*pli = NULL ;</p><p>return 1 ;<br />}</p><p>int IsEmpty_L (const List * const pli)<br />{<br />return NULL == *pli ;<br />}</p><p>int Insert_L (List * const pli, const Linked_List_Item li1, const Linked_List_Item li2)<br />{<br />Linked_List_Node * new_node ;</p><p>new_node = Make_Node (li1, li2) ;<br />if (NULL == new_node)<br />return 0 ;<br />new_node -> next = *pli ;<br />*pli = new_node ;</p><p>return 1 ;<br />}</p><p>int Find_L (const List * const pli, const Linked_List_Item li1, const Linked_List_Item li2)<br />{<br />Linked_List_Node * scan ;</p><p>scan = *pli ;<br />while (scan)<br />{<br />if (li1 == scan -> v && li2 == scan -> w)<br />return 1 ;<br />else<br />scan = scan -> next ;<br />}</p><p>return 0 ;<br />}</p><p>int Delete_L (List * const pli, const Linked_List_Item li1, const Linked_List_Item li2)<br />{<br />Linked_List_Node * scan, * temp ;</p><p>if (IsEmpty_L (pli))<br />return 0 ;<br />scan = *pli ;<br />/*There is nly one node in the list and hit the target*/<br />if (NULL == scan -> next && li1 == scan -> v && li2 == scan -> w)<br />{<br />free (scan) ;<br />*pli = NULL ;<br />return 1 ;<br />}<br />while (scan -> next && scan -> next -> v != li1 && scan -> next -> w != li2)<br />scan = scan -> next ;<br />if (scan -> next)<br />{<br />temp = scan -> next ;<br />scan -> next = temp -> next ;<br />free (temp) ;<br />return 1 ;<br />}<br />else<br />return 0 ;<br />}</p><p>void Traversal_L (const List * const pli, void (* pfun) (const Linked_List_Node * const pln))<br />{<br />Linked_List_Node * scan ;</p><p>scan = *pli ;<br />while (scan)<br />{<br />(* pfun) (scan) ;<br />scan = scan -> next ;<br />}<br />}</p><p>void Release_L (const List * const pli)<br />{<br />Linked_List_Node * scan, * temp ;</p><p>scan = *pli ;<br />while (scan)<br />{<br />temp = scan ;<br />scan = scan -> next ;<br />free (temp) ;<br />}</p><p>}</p><p>/*局部函數定義*/</p><p>static Linked_List_Node * Make_Node (const Linked_List_Item li1, const Linked_List_Item li2)<br />{<br />Linked_List_Node * new_node ;</p><p>new_node = (Linked_List_Node *) malloc (sizeof (Linked_List_Node)) ;<br />if (NULL == new_node)<br />return NULL ;<br />new_node -> v = li1 ;<br />new_node -> w = li2 ;</p><p>return new_node ;<br />}