?? listutilities.c
字號:
#include "main.h"/* =============================================================================== Author: Hammer, May 2002, For www.cprogramming.com/cboard/ File: ListUtilities.c Contents: li_Create nd_Create nd_Destroy li_Traverse li_Insert li_Destroy li_GetDataPtrByID li_DeleteNodeAndData li_Count ===============================================================================*//* =============================================================================== Function: li_Create Args: None Returns: NULL pointer. Purpose: Dummy function to start off a list. =============================================================================== */struct Node *li_Create(void){ return (NULL);} /* =============================================================================== Function: nd_Create Args: A void ptr to the data to be referenced (A record). Returns: Pointer to new node, ready for insertion into the tree. NULL if malloc fails Purpose: Creates a single node, containing a pointer to some data (Record) =============================================================================== */struct Node *nd_Create(void *ptr, struct Node *NextNode){ struct Node *newptr; if ((newptr = malloc(sizeof(struct Node))) == NULL) { perror("nd_create, malloc"); return (NULL); } newptr->DataPtr = ptr; newptr->Next = NextNode; return(newptr);}/* =============================================================================== Function: nd_Destroy Args: A pointer to a node to be free()'d Returns: Nothing Purpose: The data the node points to is not freed by the function, only the node itself is. =============================================================================== */void nd_Destroy(struct Node *ptr){ free(ptr);}/* =============================================================================== Function: li_Traverse Args: A pointer to the first node in the list A pointer to a function used to process each node. Returns: Number of Nodes processed Purpose: Traverse the list, sending each nodes data to a function =============================================================================== */int li_Traverse(struct Node *List, FPTR_Action fptr_Action){ struct Node *Current = List; int Count = 0; while (Current) { fptr_Action(Current->DataPtr); Count++; Current = Current->Next; } return (Count);}/* =============================================================================== Function: li_Insert Args: A pointer to the first node in the list A pointer to the data to be referenced by the node A pointer to a function to be used for comparison. Returns: Pointer to the first node in the list NULL is nd_Create fails. Notes: The caller should use caution to ensure the List pointer is not lost if this function returns NULL. Purpose: Inserts a new node into the list in the correct sorted order. =============================================================================== */struct Node *li_Insert(struct Node *List, void *NewData, FPTR_Compare fptr_Compare){ struct Node *newptr; struct Node *Current = List; struct Node *Previous = NULL; while (Current) { if (fptr_Compare(Current->DataPtr, NewData) > 0) break; /* Found the entry point */ Previous = Current; Current = Current->Next; } if ((newptr = nd_Create(NewData, Current)) == NULL) { /* malloc failure in nd_Create */ return (NULL); } if (Previous == NULL) List = newptr; else Previous->Next = newptr; gl_HighestID++; return (List);}/* =============================================================================== Function: li_Destroy Args: A pointer to the first node in the list Returns: Nothing Purpose: This runs through the list, destroying nodes. It does not free the data that each node points to. =============================================================================== */struct Node *li_Destroy(struct Node *List){ struct Node *Current = List; struct Node *tmp; while (Current) { tmp = Current->Next; nd_Destroy(Current); Current = tmp; } gl_HighestID = 0; return (NULL);}/* =============================================================================== Function: li_GetDataPtrByID Args: A pointer to the first node in the list An int for the ID field. Returns: Pointer to a Record struct, allowing direct access by the caller Purpose: Gets the address of a Record structure, as *owned* by the list. ===============================================================================*/struct Record *li_GetDataPtrByID(struct Node *List, int ID){ struct Node *Current = List; struct Record *rec; while (Current) { rec = Current->DataPtr; if (rec->ID == ID) break; Current = Current->Next; } return ((Current)?Current->DataPtr:NULL);}/* =============================================================================== Function: li_DeleteNodeAndData Args: A pointer to the first node in the list A pointer to the data to be referenced by the node A pointer to a function to be used for comparison. Returns: Pointer to the first node in the list NULL is nd_Create fails. Notes: The caller should use caution to ensure the List pointer is not lost if this function returns NULL. Purpose: As the function name says! =============================================================================== */struct Node *li_DeleteNodeAndData(struct Node *List, void *CompareData, FPTR_CompareDelete fptr_Compare){ struct Node *Current = List; struct Node *Previous = NULL; struct Node *tmp; while (Current) { if (fptr_Compare(Current->DataPtr, CompareData) > 0) { /* Found a node to remove */ if (Current == List) List = Current->Next; tmp = Current->Next; if (Previous) { Previous->Next = tmp; } free (Current->DataPtr); free (Current); Current = tmp; } else { /* No match, move on */ Previous = Current; Current = Current->Next; } } return (List);}/* =============================================================================== Function: li_Count Args: A pointer to the first node in the list Returns: Number of Nodes in list Purpose: Counts the nodes. =============================================================================== */int li_Count(struct Node *List){ struct Node *Current = List; int Count = 0; while (Current) { Count++; Current = Current->Next; } return (Count);}/* =============================================================================== Function: li_Sort Args: A pointer to the first node in the list Function pointer used for comparisons Returns: None Purpose: Sorts the nodes, using the given function. =============================================================================== */void li_Sort(struct Node *List, FPTR_Compare fptr_Compare){ struct Node *Current, *Next; void *Temp; bool_t StillDoingSwaps = TRUE; while (StillDoingSwaps == TRUE) { StillDoingSwaps = FALSE; Current = List; while (Current) { if ((Next = Current->Next) != NULL) { if (fptr_Compare (Current->DataPtr, Next->DataPtr) > 0) { Temp = Current->DataPtr; Current->DataPtr = Next->DataPtr; Next->DataPtr = Temp; StillDoingSwaps = TRUE; } } Current = Next; } }}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -