递归求解单链表中的最大值

 1 #include<iostream>
 2 using namespace std;
 3 
 4 typedef struct LNode {
 5     int data;
 6     LNode *next;
 7 }Lnode, *LinkList;
 8 
 9 bool InitList(LinkList &L)
10 {
11     L = new LNode;
12     L->next = NULL;
13     return true;
14 }
15 
16 void Create_List_R(LinkList &L, int n)
17 {
18     LinkList r, p;
19     r = L;
20     while (n--)
21     {
22         p = new LNode;
23         cin >> p->data;
24         p->next = NULL;
25         r->next = p;
26         r = p;
27     }
28 }
29 
30 int Find_Max(LinkList L)//递归求解单链表中的最大值
31 {
32     int max;
33     LinkList p = L;
34     if (p->next == NULL)
35         return p->data;
36     else {
37         max = Find_Max(p->next);
38         return p->data >= max ? p->data : max;
39     }
40 }
41 
42 int main()
43 {
44     LinkList L;
45     int n;
46     while (cin >> n)
47     {
48         if (n == 0) break;
49         InitList(L);
50         Create_List_R(L, n);
51         cout << Find_Max(L) << endl;
52     }
53     return 0;
54 }
原文地址:https://www.cnblogs.com/christy99cc/p/9936160.html