本文共 5521 字,大约阅读时间需要 18 分钟。
步骤:
函数设计如下:
01 | /* 单链表反转/逆序 */ |
02 | Status ListReverse(LinkList L) |
03 | { |
04 | LinkList current,pnext,prev; |
05 | if (L == NULL || L->next == NULL) |
06 | return L; |
07 | current = L->next; /* p1指向链表头节点的下一个节点 */ |
08 | pnext = current->next; |
09 | current->next = NULL; |
10 | while (pnext) |
11 | { |
12 | prev = pnext->next; |
13 | pnext->next = current; |
14 | current = pnext; |
15 | pnext = prev; |
16 | printf ( "交换后:current = %d,next = %d \n" ,current->data,current->next->data); |
17 | } |
18 | //printf("current = %d,next = %d \n",current->data,current->next->data); |
19 | L->next = current; /* 将链表头节点指向p1 */ |
20 | return L; |
21 | } |
01 | Status ListReverse2(LinkList L) |
02 | { |
03 | LinkList current, p; |
04 |
05 | if (L == NULL) |
06 | { |
07 | return NULL; |
08 | } |
09 | current = L->next; |
10 | while (current->next != NULL) |
11 | { |
12 | p = current->next; |
13 | current->next = p->next; |
14 | p->next = L->next; |
15 | L->next = p; |
16 | } |
17 | return L; |
18 | } |
这个是程序运行的结果。
01 | 整体创建L的元素(头插法): |
02 | // 原链表,current = 68, pnext = 55,68指向18,55指向18,头结点指向55 |
03 | -> 68 -> 55 -> 18 -> 45 -> 41 -> 43 -> 5 -> 28 -> 80 -> 67 |
04 |
05 | // 第一次交换后,原链表变成这样 |
06 | -> 55 -> 68 -> 18 -> 45 -> 41 -> 43 -> 5 -> 28 -> 80 -> 67 |
07 | // 进行第二次交换,pnext = 18,68指向45,18变成头结点 |
08 | -> 18 -> 55 -> 68 -> 45 -> 41 -> 43 -> 5 -> 28 -> 80 -> 67 |
09 | // 进行第三次交换,pnext = current->next = 45,68指向41,45变成头结点 |
10 | -> 45 -> 18 -> 55 -> 68 -> 41 -> 43 -> 5 -> 28 -> 80 -> 67 |
11 | // …… |
12 | -> 41 -> 45 -> 18 -> 55 -> 68 -> 43 -> 5 -> 28 -> 80 -> 67 |
13 |
14 | -> 43 -> 41 -> 45 -> 18 -> 55 -> 68 -> 5 -> 28 -> 80 -> 67 |
15 |
16 | -> 5 -> 43 -> 41 -> 45 -> 18 -> 55 -> 68 -> 28 -> 80 -> 67 |
17 |
18 | -> 28 -> 5 -> 43 -> 41 -> 45 -> 18 -> 55 -> 68 -> 80 -> 67 |
19 |
20 | -> 80 -> 28 -> 5 -> 43 -> 41 -> 45 -> 18 -> 55 -> 68 -> 67 |
21 | // current 68 没有后继,反转结束 |
22 | -> 67 -> 80 -> 28 -> 5 -> 43 -> 41 -> 45 -> 18 -> 55 -> 68 |
23 |
24 |
25 | 反转L后 |
26 | -> 67 -> 80 -> 28 -> 5 -> 43 -> 41 -> 45 -> 18 -> 55 -> 68 |
最后附上完整代码,反转有两个函数。
有两个方法可以实现单链表的反转:
方法一:
1 #include2 #include 3 4 typedef struct Node 5 { 6 int data; 7 struct Node *next; 8 }Node; 9 Node *head,*p; 10 11 Node * ReverseLink(Node *head)12 {13 Node *p1, *p2, *p3;14 if(head==NULL || head->next==NULL)15 return head;16 p1=head, p2=p1->next;17 while(p2)18 {19 p3=p2->next;20 p2->next=p1;21 p1=p2;22 p2=p3;23 }24 head->next=NULL;25 head=p1;26 return head;27 }28 29 void CreateList(int n)30 {31 Node *q; 32 int i;33 printf("Input %2d data: ",n); 34 head=(Node *)malloc(sizeof(Node)); 35 q=head;36 scanf("%d",&q->data);37 for(i=2;i<=n;i++)38 {39 q->next=(Node *)malloc(sizeof(Node));40 q=q->next;41 scanf("%d",&q->data);42 }43 q->next=NULL;44 } 45 46 void PrintList() 47 { 48 Node *q; 49 q=head; 50 while (q!=NULL) 51 { 52 printf("%-8d",q->data);53 q=q->next;54 }55 printf("\n");56 }57 58 void main()59 {60 CreateList(5);61 PrintList();62 head=ReverseLink(head);63 PrintList();64 }
方法二:
1 #include2 #include 3 using namespace std; 4 5 struct LNode{ 6 char data; 7 LNode * next; 8 }; 9 10 LNode * initList()11 { 12 LNode *head=new LNode; 13 LNode *curPtr, *newPtr; 14 curPtr=head; 15 int i=0; 16 char ch='A'; 17 while(i++<10) 18 { 19 newPtr=new LNode; 20 newPtr->data=ch++; 21 curPtr->next=newPtr;22 curPtr=newPtr;23 } 24 newPtr->next=NULL;25 return head;26 } 27 28 void print(LNode *head)29 { 30 LNode *ptr=head->next;31 while(ptr != NULL)32 { 33 cout << ptr->data << " ";34 ptr=ptr->next;35 } 36 cout << endl;37 } 38 39 void reverse(LNode *head)40 { 41 assert(head != NULL && head->next != NULL);42 LNode *ptr=head->next->next;43 head->next->next=NULL; 44 while(ptr != NULL) 45 { 46 LNode *tmp=ptr->next; 47 ptr->next=head->next; 48 head->next=ptr; 49 ptr=tmp;50 }51 } 52 53 int main()54 { 55 LNode *head=initList(); 56 print(head); 57 cout << "After reverse: " << endl; 58 reverse(head); 59 print(head); 60 system("PAUSE"); 61 return 0;62 }
参考:
本文转自夏雪冬日博客园博客,原文链接:http://www.cnblogs.com/heyonggang/p/3304838.html,如需转载请自行联系原作者