Example: The grades of N students have been placed in a linked list structure with a leading node in the main function, with A pointing to the head of the list. Write a function called fun, which finds the student’s highest score and returns it.
Do not change anything in main or the other functions; just put some statements you wrote in curly braces for fun.
The code is as follows:
#include<stdio.h>
#include<stdlib.h>
#define N 5
struct list
{
double s;
struct list*next;
};
typedef struct list STR;
double fun(STR*a)
{
double max;
STR*q=a;
max=a->s;
do
{
if(q->s>max)
max=q->s;
q=q->next;
}while(q! =0);
return max;
}
STR*creat(double*s)
{
STR*a,*p,*q;
int i=0;
a=p=(STR*)malloc(sizeof(STR));
p->s=0;
while(i<N)
{
q=(STR*)malloc(sizeof(STR));
q->s=s[i];
i++;
p->next=q;
p=q;
}
p->next=0;
return a;
}
printlist(STR*a)
{
STR*p;
p=a->next;
printf("head");
do
{
printf("- > % 2.0 f",p->s);
p=p->next;
}
while(p! =0);
printf("\n\n");
}
main()
{
double s[N]={69.72.85.80.68},max;
STR*a;
FILE*out;
a=creat(s);
printlist(a);
max=fun(a);
printf("Max = % 6.1 f \ n",max);
out=fopen("oufile.dat"."w");
fprintf(out,"Max = % 6.1 f",max);
fclose(out);
}
Copy the code
The output running window is as follows:
Other exercises this week
C programming column
① Please write the function fun, its function is: STR in the string of even subscript characters deleted, the remainder of the string formed by the new string is placed in the array of S.
② The results of N students have been put into a linked list structure of the head node in the main function. A points to the head node of the linked list. Write a function called fun, which finds the student’s highest score and returns it.
③ Assume that the input string contains only letters and “#” signs. Write fun, which moves all leading “#” signs to the end of the string.
C programming > 11 week ④ Please write the function fun, the function is: determine whether the string is palindrome? If it returns 1, the main function prints YES, otherwise it returns 0, and the main function prints NO. Palindromes are strings that are read the same backwards and forwards.
⑤ Write a function to remove all Spaces from the string.
⑥ A student’s record consists of a student number, 5 course scores and average scores. The student number and 5 course scores are given in the main function. Write a function called fun, whose function is to find the average score of the student and put it in the recorded AVE members.
Find the number of characters STR refers to in the string, and return this value.
If there are m integers in the array, it is required to move the array elements with subscripts from 0 to t(t≤m-1) to the end of the array.
The harder you work, the luckier you get! Come on, Aoli!!