/*DoubleCircularLinkedList.c -- 雙向迴圈鏈表實現檔案*/<br />#include "DoubleCircularDoubleCircularLinkedList.h"</p><p>/*局部函式宣告*/</p><p>static Node * makeNode (const Item * const pi) ;</p><p>/*介面函數定義*/</p><p>BOOL Create_B (DoubleCircularLinkedList * const pl)<br />{<br />*pl = (struct doublecircularlinkedlist *) malloc (sizeof (struct doublecircularlinkedlist)) ;<br />if (NULL == *pl)<br />return FALSE ;<br />(*pl) -> list = NULL ;<br />(*pl) -> current = 0 ;</p><p>return TRUE ;<br />}</p><p>BOOL IsEmpty_B (const DoubleCircularLinkedList * const pl)<br />{<br />if (0 == (*pl) -> current)<br />return TRUE ;<br />else<br />return FALSE ;<br />}</p><p>BOOL Insert_B (const DoubleCircularLinkedList * const pl, const Item * const pi)<br />{<br />Node * new_node ;</p><p>new_node = makeNode (pi) ;<br />if (NULL == new_node)<br />return FALSE ;<br />if (IsEmpty_B (pl))<br />{<br />new_node -> left = new_node -> right = new_node ;<br />(*pl) -> list = new_node ;<br />}<br />else<br />{<br />(*pl) -> list -> right -> left = new_node ;<br />new_node -> left = (*pl) -> list ;<br />new_node -> right = (*pl) -> list -> right ;<br />(*pl) -> list -> right = new_node ;<br />}<br />(*pl) -> current++ ;</p><p>return TRUE ;<br />}</p><p>Node * Find_B (const DoubleCircularLinkedList * const pl, const Item * const pi)<br />{<br />Node * scan ;<br />int i, num ;</p><p>if (IsEmpty_B (pl))<br />return NULL ;<br />for (i = 0, num = (*pl) -> current, scan = (*pl) -> list; i < num; i++)<br />{<br />if (scan -> item != *pi)<br />scan = scan -> right ;<br />else<br />return scan ;<br />}</p><p>return NULL ;<br />}</p><p>BOOL Delete_B (const DoubleCircularLinkedList * const pl, const Item * const pi)<br />{<br />Node * target ;</p><p>target = Find_B (pl, pi) ;<br />if (NULL == target)<br />return FALSE ;<br />if (1 == (*pl) -> current)<br />{<br />target = (*pl) -> list ;<br />(*pl) -> list = NULL ;<br />}<br />else<br />{<br />target -> left -> right = target -> right ;<br />target -> right -> left = target -> left ;<br />if (target == (*pl) -> list)<br />(*pl) -> list = target -> right ;<br />}<br />free (target) ;<br />(*pl) -> current-- ;</p><p>return TRUE ;<br />}</p><p>void Traversal_B (const DoubleCircularLinkedList * const pl, void (* pfun) (const Node * const pn))<br />{<br />Node * scan ;<br />int i, num ;</p><p>for (i = 0, num = (*pl) -> current, scan = (*pl) -> list; i < num; i++)<br />{<br />(* pfun) (scan) ;<br />scan = scan -> right ;<br />}<br />putchar ('/n') ;<br />}</p><p>void Release_B (const DoubleCircularLinkedList * const pl)<br />{<br />Node * scan ;<br />int i, num ;</p><p>for (i = 1, num = (*pl) -> current, scan = (*pl) -> list; i < num; i++)<br />{<br />scan = scan -> right ;<br />free (scan -> left) ;<br />}<br />free (*pl) ;<br />}</p><p>/*局部函數定義*/</p><p>static Node * makeNode (const Item * const pi)<br />{<br />Node * new_node ;</p><p>new_node = (Node *) malloc (sizeof (Node)) ;<br />if (NULL == new_node)<br />return NULL ;<br />new_node -> item = *pi ;</p><p>return new_node ;<br />}