c语言链表编程题

已知一个正整数序列,个数未知,但至少有一个元素,你的任务是建立一个单链表,并使用该链表存储这个正整数序列,然后统计这个序列中元素的最大值与最小值,计算序列全部元素之和。正整数的输入用-1作为结束标志,注意-1不算这个正整数序列中的元素(不要统计... 已知一个正整数序列,个数未知,但至少有一个元素,你的任务是建立一个单链表,并使用该链表存储这个正整数序列,然后统计这个序列中元素的最大值与最小值,计算序列全部元素之和。正整数的输入用-1作为结束标志,注意-1不算这个正整数序列中的元素(不要统计-1)。 输入与输出要求:输入一个正整数序列,正整数序列元素的个数未知,但以输入“-1”结束,输入“-1”前至少输入一个正整数。序列中的元素范围在1—999999999之间。输出三个正整数,即最大值、最小值、所有元素之和。 程序运行效果: Please input a series of integers:1 4 99 21 50 61 324 -1↙ The maximum,minmum and the total are:99 1 272 我是菜鸟 求大神 是c啊

第1个回答  2019-09-22
上面的那个运行结果是不是错了?我手算和程序算的total都是560啊
把程序给你吧,纯手写,希望有点用
#include<stdio.h>
#include<stdlib.h>
typedef
struct
sss{
int
num;
struct
sss
*next;
}elem;
void
bigadd(elem
*sum,int
n);
void
output(elem
*a);
void
clear(elem
*a){
elem
*temp;
while(a!=NULL){
temp=a;
a=a->next;
free(temp);
}
}
int
main()
{
int
temp,max,min;
elem
*sum;
elem
*recent;
elem
*head;
head=(elem*)calloc(1,sizeof(elem));
recent=head;
max=1;
min=999999999;
sum=(elem*)calloc(1,sizeof(elem));
sum->num=0;
sum->next=NULL;
printf("Please
input
a
series
of
integers:");
scanf("%d",&head->num);
scanf("%d",&temp);
while(temp!=-1){
recent->next=(elem*)calloc(1,sizeof(elem));
recent=recent->next;
recent->num=temp;
if(temp>max)
max=temp;
if(temp<min)
min=temp;
bigadd(sum,temp);
scanf("%d",&temp);
}
recent->next=NULL;
clear(head);
printf("The
maximum,minmum
and
the
total
are:%d
%d
",max,min);
output(sum);
printf("\n");
return
0;
}
void
bigadd(elem
*a,int
n){
int
temp;
elem
*t;
a->num+=n;
temp=a->num/1000000000;
a->num=a->num%1000000000;
if(temp!=0){
if(a->next==NULL){
a->next=(elem*)calloc(1,sizeof(elem));
t=a->next;
t->next=NULL;
t->num=temp;
}
else
bigadd(a->next,temp);
}
}
void
output(elem
*a){
if(a->next==NULL)
printf("%d",a->num);
else{
output(a->next);
printf("%09d",a->num);
}
free(a);
}
相似回答