#include <stdio.h> #include <stdlib.h> //单链表的逆序使用三个辅助指针 typedef struct Node { int data; struct Node *next; }Node; //从尾部添加节点 void AppendtoEnd(Node *head, int data) { Node *new_node=NULL; Node *temp=NULL; new_node=malloc(sizeof(Node)); new_node->data=data; new_node->next=NULL; //找到最后一个节点 temp=head; for(;temp->next!=NULL;temp=temp->next); temp->next=new_node; } //打印链表 void ShowList(Node *head) { Node* temp=NULL; if(head==NULL) { return ; } temp=head->next; for(;temp!=NULL;temp=temp->next) { printf("%d",temp->data); printf("-->"); } printf(" "); } //单链表的逆序 Node* ReverseList(Node *head) { Node* frist=NULL; Node* second=NULL; Node* third=NULL; if(head==NULL||head->next==NULL) { return head; } frist=head->next; second=frist->next; third=second->next; frist->next=NULL; while(third!=NULL) { second->next=frist; frist=second; second=third; third=third->next; } second->next=frist; head->next=second; return head; } int main() { int i=0; Node *head=malloc(sizeof(Node)); Node *newhead=NULL; head->next=NULL; for(i=1;i<11;i++) { AppendtoEnd(head, i); } ShowList(head); newhead=ReverseList(head); ShowList(newhead); free(head); return 0; }