Skip to content

Instantly share code, notes, and snippets.

@hayeah
Created April 5, 2013 12:42
Show Gist options
  • Save hayeah/5319012 to your computer and use it in GitHub Desktop.
Save hayeah/5319012 to your computer and use it in GitHub Desktop.
#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) {
}
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);
// l = build_list(a,2);
// print_list(l);
// free(l);
// l = build_list(a,0);
// print_list(l);
// free(l);
// print_list(l);
//reverse_list(l);
//print_list(l); // 4\n3\n2\n1\n
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment