Test site for easy operations of a single-chain c-language table
// Header file # ifndef _ LINKLIST_H _ # define _ LINKLIST_H _ typedef int DataType; typedef struct LinkList {DataType _ data; struct LinkList * _ next;} LinkList, * pLinkList; void Merge (pLinkList & pHead); void PrintLinkList (pLinkList pHead); int GetListLength (pLinkList objects); void Merge (pLinkList & pHead); void PushBack (pLinkList & pHead, dataType Data); void PopBack (pLinkList & pHead); void PushFront (pLinkList & PHead, DataType Data); void BuyNode (pLinkList & pHead, DataType Data); pLinkList Find (pLinkList pHead, DataType Data); void Insert (pLinkList pos, DataType Data ); void Remove (pLinkList & pHead, DataType Data); void RemoveAll (pLinkList & pHead, DataType Data); void Erase (pLinkList & pHead, pLinkList pos); void BubbleSort (pLinkList pHead ); int Joseph phuscircle (pLinkList pHead, int num); void QuickSort (pLinkList pH Ead, pLinkList pEnd); void ReverseList (pLinkList & pHead); pLinkList IfCircleList (pLinkList pHead); pLinkList Merge (pLinkList pHead); # endif // function file # include <stdio. h> # include "linklist. h "# include <malloc. h> # include <assert. h> // initialize the node void InitLinkList (pLinkList & pNode) {assert (pNode); pNode-> _ next = NULL; pNode-> _ data = 0 ;} // traverse the data field void PrintLinkList (pLinkList pHead) {pLinkList tmp = pHead; If (pHead = NULL) {printf ("the linked list is empty! \ N ") ;}while (tmp) {printf (" % d ", tmp-> _ data); tmp = tmp-> _ next ;} printf ("\ n");} // evaluate the length int GetListLength (pLinkList pHead) {int length = 0; while (pHead) {pHead = pHead-> _ next; + + length;} return length;} // destroy the linked table void DestroyList (pLinkList & pHead) {pLinkList del = pHead; if (pHead = NULL) {printf ("the linked list is empty! \ N ") ;}while (pHead) {del = pHead-> _ next; if (pHead-> _ next = NULL) {free (pHead); pHead = NULL; return;} pHead-> _ next = pHead-> _ next; free (del) ;}// end void PushBack (pLinkList & pHead, DataType Data) {pLinkList tmp = pHead; if (pHead = NULL) {BuyNode (pHead, Data);} else {while (tmp-> _ next) {tmp = tmp-> _ next;} BuyNode (tmp-> _ next, Data) ;}// Delete void PopBack (pLinkList & pHead) {pLinkList tm P = pHead; if (tmp = NULL) {printf ("the linked list is empty! \ N "); return;} if (pHead-> _ next = NULL) {free (pHead); pHead = NULL; return ;} while (tmp-> _ next! = NULL) {tmp = tmp-> _ next;} free (tmp-> _ next); tmp-> _ next = NULL ;} // void PushFront (pLinkList & pHead, DataType Data) {pLinkList tmp; BuyNode (tmp, Data); tmp-> _ next = pHead; pHead = tmp ;} // create a node void BuyNode (pLinkList & pNode, DataType Data) {pNode = (pLinkList) malloc (sizeof (LinkList); pNode-> _ data = Data; pNode-> _ next = NULL;} // Find pLinkList Find (pLinkList pHead, DataType Data) {pLinkList tmp = pHead; Ssert (pHead); while (tmp) {if (tmp-> _ data = Data) return tmp; tmp = tmp-> _ next;} return NULL ;} // Insert void Insert (pLinkList pos, DataType Data) {assert (pos); pLinkList tmp = pos-> _ next; BuyNode (pos-> _ next, Data ); pos-> _ next = tmp;} // Delete the given Data node void Remove (pLinkList & pHead, DataType Data) {pLinkList del = Find (pHead, Data ); pLinkList tmp = pHead; if (pHead = NULL) {printf ("the linked list is empty! \ N "); return;} if (pHead = del) {pHead = pHead-> _ next; free (tmp); return;} while (tmp-> _ next) {if (tmp-> _ next = del) {tmp-> _ next = tmp-> _ next; free (del); return ;} tmp = tmp-> _ next; }}// delete all given Data nodes void RemoveAll (pLinkList & pHead, DataType Data) {pLinkList del = Find (pHead, Data ); pLinkList tmp = pHead; if (pHead = NULL) {printf ("the linked list is empty! \ N "); return;} while (tmp-> _ next & del! = NULL) {del = Find (pHead, Data); if (pHead = del) {pHead = pHead-> _ next; free (tmp); tmp = pHead ;} else if (tmp-> _ next = del) {tmp-> _ next = tmp-> _ next; free (del );} else tmp = tmp-> _ next; }}// Delete the location node void Erase (pLinkList & pHead, pLinkList pos) {pLinkList tmp = pHead; assert (pHead ); assert (pos); if (pHead = pos) {pHead = pHead-> _ next; return;} while (tmp) {if (tmp-> _ next = pos) {tmp- >_Next = tmp-> _ next; free (pos); break;} tmp = tmp-> _ next ;}} // bubble Sort Ascending void BubbleSort (pLinkList pHead) {pLinkList tmp = pHead, index = pHead; int count = 0; assert (pHead); while (index) {tmp = pHead; while (tmp-> _ next) {if (tmp-> _ data)> (tmp-> _ next-> _ data )) {DataType change = tmp-> _ data; tmp-> _ data = tmp-> _ next-> _ data; tmp-> _ next-> _ data = change; + + count;} tmp = tmp-> _ next;} if (count <= 1) return; else {tmp = pHead; index = index-> _ next ;}}// assume that the linked list is ring num = 3int Joseph phuscircle (pLinkList pHead, int num) {pLinkList tmp = pHead; assert (pHead); while (tmp-> _ next! = Tmp) {pLinkList del = NULL; tmp = tmp-> _ next; tmp-> _ data = tmp-> _ next-> _ data; del = tmp-> _ next; tmp-> _ next = tmp-> _ next; free (del);} return tmp-> _ data ;} // fast-forward void QuickSort (pLinkList pHead, pLinkList pEnd) {pLinkList cur; pLinkList prev = pHead; pLinkList key = pHead; DataType tmp = 0; if (pHead = NULL | pHead-> _ next = NULL) {return;} cur = pHead-> _ next; if (pEnd = pHead-> _ next | pEn D = pHead) {return;} while (cur! = PEnd) {if (cur-> _ data <key-> _ data) {prev = prev-> _ next; if (cur! = Prev) {DataType tmp = cur-> _ data; cur-> _ data = prev-> _ data; prev-> _ data = tmp ;}} cur = cur-> _ next;} tmp = prev-> _ data; prev-> _ data = key-> _ data; key-> _ data = tmp; quickSort (pHead, prev); QuickSort (prev-> _ next, pEnd);} // reverse void ReverseList (pLinkList & pHead) {pLinkList NewHead = NULL; pLinkList tmp = NULL; if (! PHead |! PHead-> _ next) return; while (pHead) {NewHead = pHead-> _ next; pHead-> _ next = tmp; tmp = pHead; pHead = NewHead ;} pHead = tmp;} // determines whether a single-linked table contains pLinkList IfCircleList (pLinkList pHead) {pLinkList fast = pHead; pLinkList slow = pHead; assert (pHead ); while (fast & fast-> _ next) {fast = fast-> _ next; slow = slow-> _ next; if (fast = slow) {return fast ;}} return NULL ;}// determine the entry point of the ring. pLinkList CircleListEntra Nce (pLinkList pHead) {pLinkList head = pHead; pLinkList tmp = IfCircleList (pHead); if (pHead = NULL & tmp = NULL) {return NULL ;} while (tmp! = Head) {tmp = tmp-> _ next; head = head-> _ next;} return tmp ;}