Created
          May 10, 2013 07:41 
        
      - 
      
- 
        Save hayeah/5552987 to your computer and use it in GitHub Desktop. 
    linked list
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #include <stdio.h> | |
| #include <assert.h> | |
| #include <stdlib.h> | |
| typedef struct node{ | |
| int e; | |
| struct node *next; | |
| } node; | |
| node *list_build(int* a,int size) { | |
| if(size == 0) | |
| return NULL; | |
| node* l = malloc(size*sizeof(node)); | |
| assert(l != NULL); | |
| int i; | |
| for(i = 0; i < size; i++) { | |
| node* c = l+i; | |
| c->e = *(a+i); | |
| if(i+1 == size) { | |
| c->next = NULL; | |
| } else { | |
| c->next = c+1; | |
| } | |
| // printf("c: %p\n",c); | |
| // printf("e: %d\n",c->e); | |
| // printf("n: %p\n",c->next); | |
| } | |
| return l; | |
| } | |
| void list_print(node *list) { | |
| node* c = list; | |
| while(c) { | |
| printf("%d\n",c->e); | |
| c = c->next; | |
| } | |
| } | |
| node *list_reverse(node *l) { | |
| return l; | |
| } | |
| int main() { | |
| int a[4] = {1,2,3,4}; | |
| node* l; | |
| l = list_build(a,4); | |
| list_print(l); // 1\n2\n3\n4\n | |
| l = list_reverse(l); | |
| list_print(l); | |
| l = list_reverse(l); | |
| list_print(l); | |
| free(l); | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment