#ifndef PCH_H
#define PCH_H
#include<stdio.h>
#include<iostream>
#include<malloc.h>
#include<cstdlib>
#include<math.h>
#include<string>
#include<time.h>
constexpr auto MAXSIZE = 100;
constexpr auto OK = 1;
constexpr auto ERROR= 0;
template<typename T>
constexpr auto random(T x) { return (rand()%x); }
typedef int Status;
typedef struct LNode
{
int data;
struct LNode *next;
}LNode,*LinkList;
Status InitList(LinkList &f);
Status CreateList(LinkList &f,int j);
Status TravelList(LinkList f);
int GetMax(LinkList f,int max);
int GetLength(LinkList f,int length);
double GetAverage(LinkList f,double sum,int c);
#endif
-----------------------------------------------------
#include "pch.h"
Status InitList(LinkList & f)
{
f = (LinkList)malloc(sizeof(LNode));
if (!f) exit(OVERFLOW);
f->next = NULL;
return OK;
}
Status CreateList(LinkList & f,int j)
{
LNode *p;
p = f;
f->data = random(100);
srand((unsigned)time(0));
for(int i=0;i<j-1;i++)
{
p->next = (LinkList)malloc(sizeof(LNode));
p = p->next;
p->data = random(100);
p->next = NULL;
}
return OK;
}
Status TravelList(LinkList f)
{
LNode *p;
p = f;
while (p)
{
std::cout << p->data<<" ";
p = p->next;
}
return OK;
}
int GetMax(LinkList f,int max)
{
if (f==NULL)
return max;
else if (max<f->data)
{
max = f->data;
return GetMax(f->next, max);
}
else
{
return GetMax(f->next, max);
}
}
int GetLength(LinkList f,int length)
{
if (f==NULL) return length;
else
{
length++;
return GetLength(f->next, length);
}
}
double GetAverage(LinkList f,double sum,int c)
{
if(f==NULL)
return sum/c;
sum +=f->data ;
c++;
return GetAverage(f->next,sum,c);
}
------------------------------------------------
#include "pch.h"
#include <iostream>
int main()
{
LinkList f;
InitList(f);
CreateList(f, 5);
TravelList(f);
std::cout<<"\n看,是最大值:"<<GetMax(f, -99999)<<"\n";
std::cout<<"看,是表长:"<<GetLength(f, 0)<<"\n";
std::cout<<"看,是平均值:"<<GetAverage(f, 0, 0);
}