1 struct node *create(int n)//顺序建立链表 2 { 3 int i,num; 4 struct node *head,*tail,*p; 5 head = (struct node*)malloc(sizeof(struct node)); 6 head -> next = NULL; 7 tail = head; 8 for(i = 0;i < n;i ++) 9 { 10 scanf("%d",&num); 11 p = (struct node*)malloc(sizeof(struct node)); 12 p -> data = num; 13 p -> next = NULL; 14 tail -> next = p; 15 tail = p; 16 } 17 return head; 18 } 19 struct node *create(int n) //逆序 20 { 21 struct node *head,*tail,*p; 22 int i,num; 23 head = (struct node*)malloc(sizeof(struct node)); 24 head -> next = NULL; 25 tail = NULL; 26 for(i = 0;i < n;i ++) 27 { 28 scanf("%d",&num); 29 p = (struct node*)malloc(sizeof(struct node)); 30 p -> data = num; 31 p -> next = tail; 32 tail = p; 33 } 34 head -> next = tail; 35 return head; 36 } 37 struct node *reverse(struct node *head)//反转 38 { 39 struct node *s,*p,*tail; 40 s = head -> next; 41 tail = NULL; 42 while(s) 43 { 44 p = s -> next; 45 s -> next = tail; 46 tail = s; 47 s = p; 48 } 49 head -> next = tail; 50 return head; 51 } 52 void show(struct node *head)//显示 53 { 54 int flag = 1; 55 struct node *s; 56 s = head -> next; 57 while(s) 58 { 59 if(flag) flag = 0; 60 else printf(" "); 61 printf("%d",s->data); 62 s = s -> next; 63 } 64 }
原文:http://www.cnblogs.com/local8080/p/3579217.html