• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
乐碎碎
程序媛想的事儿
博客园    首页    新随笔    联系   管理    订阅  订阅
反转一个单链表,分别以迭代和递归的形式来实现

迭代法:

 1 // 反转单链表.cpp : 定义控制台应用程序的入口点。
 2 
 3 #include "stdafx.h"
 4 #include <stdlib.h>
 5 
 6 typedef struct node
 7 {
 8     int data;
 9     struct node *next;
10 }linknode,*linklist;
11 
12 linknode *reverse(linknode *head)//带有头结点的单链表迭代反转,非递归方法
13                                  //思路:将当前结点的next指向前驱;当前结点指向next
14 {
15     if(head==NULL)
16         return 0;
17     linknode *pre,*cur,*ne;//pre 前驱结点指针;cru当前结点指针;ne后继指针
18     pre=head;
19     cur=head->next;
20     while(cur!=NULL)
21     {
22         ne=cur->next;
23         cur->next=pre;
24         pre=cur;
25         cur=ne;
26     }
27     linknode *q;
28     q=head->next;
29     q->next=NULL;
30     head->next=pre;
31     return head;
32 }
33 
34 void printLinkList_H(linklist L) //打印
35 {
36     linklist p;
37     p=L->next;
38     while(p)
39     {
40         printf("%4d", p->data);
41         p = p->next;
42     }
43     printf("\n");
44 }
45 
46 void main()
47 {
48     int i;
49     linklist L = (linklist)malloc(sizeof(linknode));
50     L->next = NULL; //头结点
51     for(i = 1;i<=5;i++)//新建一个带头结点的单链表
52     {
53         linklist p = (linklist)malloc(sizeof(linknode));
54         p->data = i;
55         p->next = L->next;
56         L->next = p;
57     }
58     printLinkList_H(L);
59     reverse(L);
60     printLinkList_H(L);
61 }

 

递归法:

 

 1 // 反转单链表.cpp  递归法
 2 
 3 #include "stdafx.h"
 4 #include <stdlib.h>
 5 
 6 typedef struct node
 7 {
 8     int data;
 9     struct node *next;
10 }linknode,*linklist;
11 
12 
13 linknode *RecReverseList(linknode *head)
14 {
15    if (head==NULL)  
16        return 0;
17    linknode *curr , *reverse_head , *temp;
18    if(head->next==NULL)
19        return head;
20    else 
21    {
22         curr=head;
23         temp=head->next;
24         reverse_head=RecReverseList(temp);
25    }
26     temp->next=curr;
27     curr->next=NULL;
28     return reverse_head;
29 }
30 
31 void printLinkList_H(linklist L) //打印
32 {
33     linklist p;
34     p=L->next;
35     while(p)
36     {
37         printf("%4d", p->data);
38         p = p->next;
39     }
40     printf("\n");
41 }
42 
43 
44 void main()
45 {
46     int i;
47     linklist L = (linklist)malloc(sizeof(linknode));
48     L->next = NULL; //头结点
49     for(i = 1;i<=5;i++)//新建一个带头结点的单链表
50     {
51         linklist p = (linklist)malloc(sizeof(linknode));
52         p->data = i;
53         p->next = L->next;
54         L->next = p;
55     }
56     printLinkList_H(L);
57     linklist q;
58     q=RecReverseList(L->next);
59     while(q)
60     {
61         printf("%4d", q->data);
62         q = q->next;
63     }
64 }

 

posted on 2012-10-04 19:08  xingle0917  阅读(715)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3