现在不说废话了.第一个取下的结点next指针指向NULL,然后依次取下的结点的next指针指向前一个取下的结点,而前一个取下的结点prev指针指向当前取下的结点就行了,一次遍历链表就搞定了.
#include <stdio.h> #include <stdlib.h>
typedef struct node{ int num; struct node* prev; struct node* next; }NODE; NODE* create_list(int n) { int i = 0; NODE* p = NULL; NODE* q = NULL; NODE* head = (NODE*)malloc(sizeof(NODE)); p = head; for(i=0;i<n;i++) { q = (NODE*)malloc(sizeof(NODE)); q->num = i; p->next = q; q->next = NULL; p = q; } return head; }
NODE* revert(NODE* head) { NODE* p1 = head->next; NODE* p2 = NULL; NODE* p3 = NULL; //p3->next =NULL; while(p1!=NULL) { printf("now %d\n",p1->num); p2 = p1; p1 = p1->next; p2->next = p3; p3 = p2; } head->next = p3; } int my_print(NODE* head) { NODE* p = head->next; while(p!=NULL) { printf("%d\t",p->num); p = p->next; } printf("\n"); } int main(int argc, char *argv[]) { NODE* head = create_list(5); my_print(head); revert(head); my_print(head); system("PAUSE"); return 0; } 代码是在dev c++上写的,gcc估计system那句要改^_^
|
阅读(2252) | 评论(1) | 转发(0) |