1, linked list definition
typedef struct LISTELEMENT_T_ { void *data; struct Listelement_t_ *next;} listelement_t;typedef struct list_t_{ int size; int capacity; listelement_t *head; listelement_t *tail;} list_t;
2. Find the K node data of the linked list
Set the speed pointer, the fast pointer ahead of the slow pointer K-1 nodes, and then the speed pointer and then traverse the linked list, when the fast pointer over the end of the list, the slow pointer is the bottom of the K node.
void *searchrkthnode (list_t *list, unsigned int k) { if (List = = NULL | | list->head = NULL | | k = = 0)//k is an unsigned number , so it is necessary to determine whether it is 0 return NULL; listelement_t *pfast = list->head; while (K > 1 && pfast! = NULL) { --k; Pfast = pfast->next; } K>1 indicates that the number of nodes is less than K, pfast=null indicates that the list is k-1 node if (k > 1 | | pfast = = NULL) return null; listelement_t *pslow = list->head; while (pfast->next! = NULL) { pfast = pfast->next; Pslow = pslow->next; } return pslow->data;}
Linked list (5)----Find the penultimate node of the linked list