单链表逆置
#include "stdafx.h" #include "malloc.h" typedef struct node { int data; struct node *next; }nnode; void initiate(nnode **head) { *head=(nnode *)malloc(sizeof(nnode)); (*head)->next=NULL; } void insert(nnode *head,int n) { nnode *p=head; nnode *s; while(p->next!=NULL) { p=p->next; } s=(nnode *)malloc(sizeof(nnode)); s->data=n; s->next=p->next; p->next=s; } void print(nnode *head) { nnode *p=head->next; while(p!=NULL) { printf("%d ",p->data); p=p->next; } } void reverse(nnode *head) { nnode *pre=head->next; nnode *tmp=pre->next; nnode *curr=tmp->next; pre->next=NULL; while(curr!=NULL) { tmp->next=pre; pre=tmp; tmp=curr; curr=curr->next; } tmp->next=pre; head->next=tmp; } int main(int argc, char* argv[]) { nnode *head; initiate(&head); for(int i=0;i<10;i++) { insert(head,i+1); } /*打印单链表*/ print(head); /*打印逆置后的单链表*/ reverse(head); printf("\n打印逆置后的单链表\n"); print(head); printf("Hello World!\n"); return 0; }