• 反转一个单链表,分别以迭代和递归的形式来实现


    迭代法:

     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 }
  • 相关阅读:
    Pascal's Triangle II
    Pascal's Triangle
    Best Time to Buy and Sell Stock II
    Best Time to Buy and Sell Stock
    Populating Next Right Pointers in Each Node
    path sum II
    Path Sum
    [转载]小波时频图
    [转载]小波时频图
    [转载]Hilbert变换及谱分析
  • 原文地址:https://www.cnblogs.com/xingele0917/p/2711641.html
Copyright © 2020-2023  润新知