链表取最大值
题目描述
读入一组数据,构建一个链表,然后遍历链表取最大值。
输入描述
–>第一行:数据个数
–>第二行:多个整型数据
输出描述
最大值
样例输入
7
2 6 8 9 11 15 20
样例输出
20
完整代码
#include<bits/stdc++.h>
using namespace std;
typedef struct LBook{
int data;
LBook *next;
}LBook, *List;
int InitList(List &L)
{
L=new LBook;
L->next=NULL;
return 1;
}
void CreateList(List &L,int n)
{
L=new LBook;
L->next=NULL;
LBook *r;
r=L;
for(int i=0;i<n;i++)
{
LBook *t;
t=new LBook;
cin>>t->data;
t->next=NULL;
r->next=t;
r=t;
}
}
int GetMax(List L)
{
if (L->next==NULL)
return NULL;
LBook *max,*p;
max=L->next;
p=L->next->next;
while(p)
{
if(p->data>max->data)
max=p;
p=p->next;
}
return max->data;
}
int main()
{
List L1;
int a;
cin>>a;
CreateList(L1,a);
int b=GetMax(L1);
cout<<b;
return 0;
}