Abstract: Please indicate the source for reprinting. Http://www.cnblogs.com/dave_cn/
I encountered several times of unified processing of these values during encoding. I didn't think of any good tricks at the time, so I wrote the same code for each volume, and I always felt very earthy, in addition,... in... then I wrote a for_each macro.
The for_each macro can easily traverse a group of scattered elements and release the space that is no longer needed after the traversal.
Set_list_m adds an element each time, and set_list_f adds all the elements to be traversed at a time.
The lazy set_list_m macro should add variable brackets so it will lazy.
If you have any questions, please correct them!
# Include <stdio. h> # include <stdlib. h> # include <string. h> # include <stdarg. h> struct list_t {void * value; struct list_t * Next;}; # define set_list_m (_ p, _ list) \ do {\ If (_ list = NULL) {\ _ list = (struct list_t *) malloc (sizeof (struct list_t); \ _ list-> next = NULL; \ _ list-> value = _ P; \} \ else {\ struct list_t * _ TMP = _ list; \ while (_ TMP-> next! = NULL) {\ _ TMP = _ TMP-> next; \} \ _ TMP-> next = (struct list_t *) malloc (sizeof (struct list_t )); \ _ TMP = _ TMP-> next; \ _ TMP-> next = NULL; \ _ TMP-> value = _ p; \}\}\ while (0) /** write the for_each macro for convenience of traversing a group of scattered elements (these elements are not organized together through arrays/lists) * and... in... also keep tempted me */# define for_each (_ p, _ list) \ for (struct list_t * _ TMP = (_ list), * _ front; \ (_ TMP! = NULL) & (_ p) = _ TMP-> value) | 1); \ _ front = _ TMP, _ TMP = _ TMP-> next, free (_ front)/** struct list_t * set_list_f (void * P ,...) * @ parameter: end with null * @ return: returns the list available for for_each */struct list_t * set_list_f (void * P ,... /* null */) {va_list arg_ptr; va_start (arg_ptr, P); struct list_t * _ tmpnode = NULL; struct list_t * List = NULL; void * _ tmpval = P; while (_ tmpval! = NULL) {If (_ tmpnode = NULL) {_ tmpnode = (struct list_t *) malloc (sizeof (struct list_t); List = _ tmpnode ;} else {_ tmpnode-> next = (struct list_t *) malloc (sizeof (struct list_t); _ tmpnode = _ tmpnode-> next;} _ tmpnode-> next = NULL; _ tmpnode-> value = _ tmpval; _ tmpval = va_arg (arg_ptr, void *);} va_end (arg_ptr); return list;} int main () {/////////////////////////////////////// /// // int A = 1, B = 2, c = 3; struct list_t * L1 = NULL; set_list_m (& A, L1); set_list_m (& B, L1); set_list_m (& C, L1 ); int * P1 = NULL; for_each (P1, L1) {printf ("for_each \ n"); * P1 = 4;} printf ("A = [% d], B = [% d], c = [% d] \ n ", A, B, C ); //////////////////////////////////////// /// // A = 1, B = 2, c = 3; struct list_t * L2 = set_list_f (& A, & B, & C, null); int * P2 = NULL; for_each (P2, L2) {printf ("for_each \ n"); * P2 = 5;} printf ("A = [% d], B = [% d], C = [% d] \ n ", a, B, c); Return 0 ;}