Reverse a linked list.

  1. static void Reverse(struct node** headRef)   
  2. {  
  3.     struct node* target = NULL;  
  4.     struct node* current = *headRef;  
  5.     struct node* next;  
  6.     while (current)  
  7.     {  
  8.         next = current->next; // tricky: save the next node  
  9.         current->next = target; 
  10.         target = current;  
  11.         current = next;  
  12.     }  
  13.     *headRef = target;  

 

  1. // new head in headRef  
  2. // return the end node  
  3. struct node* recursiveReverse(struct node** headRef)  
  4. {  
  5.     struct node *current = *headRef;  
  6.     struct node *ret = NULL;  
  7.  
  8.     if (current)  
  9.     {  
  10.         if (current->next)  
  11.         {  
  12.             struct node *tail = recursiveReverse(&current->next);  
  13.             tail->next = current;  
  14.             *headRef = current->next;  
  15.             current->next = NULL;  
  16.         }  
  17.           
  18.         ret = current;  
  19.     }  
  20.  
  21.     return ret;  

 Dynamic two dimension array

Using int as an example

  1. int **malloc2D(int m, int n) 
  2.     int **p = (int **)malloc(m * sizeof(int *)); 
  3.  
  4.     for (int i = 0; i < m; i++) 
  5.     { 
  6.             p[i] = (int *)malloc(n * sizeof(int)); 
  7.     } 
  8.  
  9.     return p; 
  10.  
  11. void free2D(int **p, int m) 
  12.     for (int i = 0; i < m; i++) 
  13.     { 
  14.             free(p[i]); 
  15.     } 
  16.  
  17.     free(p); 

 


Function with a variable number of arguments

  1. // Printf to a char buffer and return the pointer. 
  2. char *formatString(char *dstStr, const char *fmt, ...) 
  3.     va_list argp; 
  4.  
  5.     va_start(argp, fmt); 
  6.     vsprintf(dstStr, fmt, argp); 
  7.     va_end(argp); 
  8.  
  9.     return dstStr;