/* linked_list.c -- 链表实现文件 */ #include <stdio.h> #include <stdlib.h> #include "linked_list.h" /* 局部函数声明 */ static Linked_List_Node * Make_Node (const Linked_List_Item li1, const Linked_List_Item li2) ; /* 接口函数定义 */ int Initialize_L (List * const pli) { *pli = NULL ; return 1 ; } int IsEmpty_L (const List * const pli) { return NULL == *pli ; } int Insert_L (List * const pli, const Linked_List_Item li1, const Linked_List_Item li2) { Linked_List_Node * new_node ; new_node = Make_Node (li1, li2) ; if (NULL == new_node) return 0 ; new_node -> next = *pli ; *pli = new_node ; return 1 ; } int Find_L (const List * const pli, const Linked_List_Item li1, const Linked_List_Item li2) { Linked_List_Node * scan ; scan = *pli ; while (scan) { if (li1 == scan -> v && li2 == scan -> w) return 1 ; else scan = scan -> next ; } return 0 ; } int Delete_L (List * const pli, const Linked_List_Item li1, const Linked_List_Item li2) { Linked_List_Node * scan, * temp ; if (IsEmpty_L (pli)) return 0 ; scan = *pli ; /* There is nly one node in the list and hit the target */ if (NULL == scan -> next && li1 == scan -> v && li2 == scan -> w) { free (scan) ; *pli = NULL ; return 1 ; } while (scan -> next && scan -> next -> v != li1 && scan -> next -> w != li2) scan = scan -> next ; if (scan -> next) { temp = scan -> next ; scan -> next = temp -> next ; free (temp) ; return 1 ; } else return 0 ; } void Traversal_L (const List * const pli, void (* pfun) (const Linked_List_Node * const pln)) { Linked_List_Node * scan ; scan = *pli ; while (scan) { (* pfun) (scan) ; scan = scan -> next ; } } void Release_L (const List * const pli) { Linked_List_Node * scan, * temp ; scan = *pli ; while (scan) { temp = scan ; scan = scan -> next ; free (temp) ; } } /* 局部函数定义 */ static Linked_List_Node * Make_Node (const Linked_List_Item li1, const Linked_List_Item li2) { Linked_List_Node * new_node ; new_node = (Linked_List_Node *) malloc (sizeof (Linked_List_Node)) ; if (NULL == new_node) return NULL ; new_node -> v = li1 ; new_node -> w = li2 ; return new_node ; }