#include <stdio.h>
#include <stdlib.h>
//尾插法建立单链表
struct yo{
int data;
struct yo *next;
};
int main()
{
struct yo *head,*p,*q,*s;
p=(struct yo*)malloc(sizeof(struct yo));
p->data=10;
head=p;
q=(struct yo*)malloc(sizeof(struct yo));
q->data=20;
p->next=q;
s=(struct yo*)malloc(sizeof(struct yo));
s->data=30;
q->next=s;
s->next=NULL;
int sum=0;
int avg;
struct yo *x;
x=head;
while(x!=NULL)
{
sum=sum+x->data;
x=x->next;
printf("%d\t",sum);
}
printf("\n");
avg=sum/3;
printf("平均数=%d\n",avg);
return 0;
}
建立单链表,遍历并求其平均数
最新推荐文章于 2022-09-19 11:38:53 发布