单链表反转:
比如原链表为 head->1->2->3->NULL;
反转后:head->3->2->1->NULL;
#include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node *next; }*List; #define node struct Node List creat(void); List re( List head ,List p,List q); void Outlist( List q ); int main(int argv, char *argc[]) { List head; List p; List q; head = NULL; head = creat(); p = head->next; q = head; re(head,p,q); q = head->next; Outlist(q); system("pause"); return 0; } List creat(void) { int data; List head; List p; List q; int flag = 1; head = (List)malloc(sizeof(node)); q = head; do { printf("请输入正数(<MAX_INT输入0表示输入结束):"); scanf("%d",&data); fflush(stdin); if ( 0 == data ) { q->next = NULL; flag = 0; } else { p = (List)malloc(sizeof(node)); p->data = data; q->next = p; q = p; } }while(flag); q = head->next; Outlist(q); return head; } void Outlist( List q ) { while(NULL != q) { printf("%d ",q->data); q = q->next; } printf("\n"); return; } List re(List head,List p,List q) { if(NULL == p) { head->next = q; } else { p=re(head,p->next,q->next); p->next = q; if( head == q) { p->next = NULL; } } return q; }