鏈表啊,我去年8月就認識你了.可是,居然到現在,我才寫出來逆置單鏈表.拓展得遠遠不夠啊.
前一陣寫過,但是沒有實現.
這次,昨晚構思完畢,剛才寫出來了,而且還有鏈表本身.全下來30分鐘,貌似比較快了,至少跟以前相比.
好了,貼吧!當然了,這個ADT比較簡潔.
/*10.2-7-2011-05-08-19.40.c -- 第十章第二節第七題*/<br />#include <stdio.h><br />#include <stdlib.h></p><p>/*明顯常量定義*/</p><p>#define FALSE (0)<br />#define TRUE (1)</p><p>/*資料類型定義*/</p><p>typedef int BOOL ;<br />typedef int Item ;<br />typedef struct node<br />{<br />Item item ;<br />struct node * next ;<br />} Node ;<br />typedef Node * LinkedList ;</p><p>/*介面函式宣告*/</p><p>void Initialize_L (LinkedList * const pl) ;<br />BOOL IsEmpty_L (const LinkedList * const pl) ;<br />BOOL Insert_L (LinkedList * const pl, const Item item) ;<br />void Traversal_L (const LinkedList * const pl, void (* pfun) (const Item item)) ;<br />void Release_L (LinkedList * const pl) ;</p><p>/*介面函數定義*/</p><p>void Initialize_L (LinkedList * const pl)<br />{<br />*pl = NULL ;<br />}</p><p>BOOL IsEmpty_L (const LinkedList * const pl)<br />{<br />if (NULL == *pl)<br />return TRUE ;<br />else<br />return FALSE ;<br />}</p><p>BOOL Insert_L (LinkedList * const pl, const Item item)<br />{<br />Node * newNode ;</p><p>newNode = (Node *) malloc (sizeof (Node)) ;<br />if (NULL == newNode)<br />return FALSE ;<br />newNode -> item = item ;<br />if (IsEmpty_L (pl))<br />{<br />newNode -> next = NULL ;<br />*pl = newNode ;<br />}<br />else<br />{<br />newNode -> next = *pl ;<br />*pl = newNode ;<br />}</p><p>return TRUE ;<br />}</p><p>void Traversal_L (const LinkedList * const pl, void (* pfun) (const Item item))<br />{<br />Node * scan ;</p><p>for (scan = *pl; scan != NULL; scan = scan -> next)<br />(* pfun) (scan -> item) ;<br />}</p><p>void Release_L (LinkedList * const pl)<br />{<br />Node * scan, * temp ;</p><p>scan = *pl ;<br />while (scan != NULL)<br />{<br />temp = scan ;<br />scan = scan -> next ;<br />free (temp) ;<br />}<br />*pl = NULL ;<br />}</p><p>/*主常式*/</p><p>int mian (void) ;<br />void printItem (const Item item) ;<br />void reverse (LinkedList * const pl) ;</p><p>int main (void)<br />{<br />LinkedList list ;<br />Item item ;</p><p>Initialize_L (&list) ;<br />item = 1 ;<br />Insert_L (&list, item) ;<br />item = 2 ;<br />Insert_L (&list, item) ;<br />item = 3 ;<br />Insert_L (&list, item) ;<br />item = 4 ;<br />Insert_L (&list, item) ;<br />item = 5 ;<br />Insert_L (&list, item) ;<br />Traversal_L (&list, printItem) ;<br />reverse (&list) ;<br />Traversal_L (&list, printItem) ;<br />Release_L (&list) ;</p><p>return 0 ;<br />}</p><p>void printItem (const Item item)<br />{<br />printf ("%-3d/n", item) ;<br />}</p><p>void reverse (LinkedList * const pl)<br />{<br />Node * current, * next, * temp ;</p><p>/*If linkedlist is empty or only has one node.*/<br />if (IsEmpty_L (pl) || NULL == (*pl) -> next)<br />return ;<br />current = *pl ;<br />next = current -> next ;<br />while (next -> next != NULL)<br />{<br />temp = next -> next ;<br />next -> next = current ;<br />current = next ;<br />next = temp ;<br />}<br />next -> next = current ;<br />(*pl) -> next = NULL ;<br />*pl = next ;<br />}