C语言反转单链表

1、反转单链表

        定义一个函数,输入一个链表的头结点,反转该链表并输出反转后链表的头结点。链表结点定义如下:

        struct node //结点类型定义
        {
                int val; //结点的数据域
                struct node *pNext;//结点的指针域(存放结点的直接后继的地址
      };

2、链表的结点结构

┌───┬───┐
data next
└───┴───┘
data域 --  存放结点值的数据域
next域 --  存放结点的直接后继的地址 (位置)的 指针 域(链域)

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3.   
  4. struct node  
  5. {  
  6.     int val;  
  7.     struct node *pNext;  
  8. };  
  9.   
  10. struct node *gen()  
  11. {  
  12.     struct node *pHead = NULL;  
  13.     printf("反转前::");
  14.     for(int i = 10; i > 0; i--){  
  15. // 创建了一个空节点并分配内存,此时此节点还没有数据,为空节点
  16.         struct node * p = (struct node *)malloc(sizeof(struct node)); 
  17.        //在空节点的data域赋值
  18.         p -> val = i;  
  19.        //因为pHead初始化时为NULL,第一次for循环表示是第一个节点(后面无其他节点),p->pNext的下个节点为NULL空
  20.         p -> pNext = pHead;  
  21.         //将已填充好的第一次分配的节点p赋值给pHead,此时pHead不为空,以此循环翻转链表
  22.         pHead = p;  
  23.         printf("%d ",i);
  24.     }     
  25.         printf("\n");

  26.     return pHead;  
  27. }  
  28.   
  29. void display(struct node *pHead)  
  30. {  
  31.     printf("反转后::");
  32.     while( pHead != NULL)  
  33.     {     
  34.         printf("%d ", pHead->val);  
  35.         pHead = pHead->pNext;  
  36.     }     
  37.     printf("\n");  
  38. }  
  39.   
  40. //递归实现  
  41. struct node * reverse(struct node *pHead)  
  42. {  
  43.     if (pHead == NULL || pHead -> pNext == NULL)  
  44.     {     
  45.         return pHead;  
  46.     }     
  47.     struct node *p = pHead -> pNext;  
  48.     struct node *pNewHead =  reverse(p);  
  49.     p -> pNext = pHead;  
  50.     pHead ->pNext = NULL;  
  51.     return pNewHead;  
  52. }  
  53.   
  54. //尾递归实现  
  55. struct node * do_reverse_tail(struct node *pHead, struct node *pNewHead)  
  56. {  
  57.     if(pHead == NULL)  
  58.     {  
  59.         return pNewHead;  
  60.     }  
  61.     else  
  62.     {  
  63.         struct node *pNext = pHead->pNext;  
  64.         pHead->pNext = pNewHead;  
  65.         return do_reverse_tail(pNext, pHead);  
  66.     }  
  67. }  
  68.   
  69. struct node * reverse_tail(struct node *pHead)  
  70. {  
  71.     return do_reverse_tail(pHead, NULL);  
  72. }  
  73.   
  74. //迭代实现  
  75. struct node * reverse_it(struct node *pHead)  
  76. {  
  77.     struct node *pNewHead = NULL;  
  78.     struct node *pPrev = NULL;  
  79.     struct node *pCur = pHead;  
  80.     while(pCur != NULL)  
  81.     {  
  82.         struct node *pTmp = pCur->pNext;  
  83.         if(pTmp == NULL)  
  84.         {  
  85.             pNewHead = pCur;  
  86.         }  
  87.         pCur->pNext = pPrev;  
  88.         pPrev = pCur;  
  89.         pCur = pTmp;  
  90.     }  
  91.   
  92.     return pNewHead;  
  93. }  
  94.   
  95. int main()  
  96. {  
  97.     struct node *pHead = gen();  
  98.     display(pHead);  
  99.     pHead = reverse_it(pHead);  
  100.     display(pHead);  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值