#include
typedef struct slist{
int entry;
struct slist *next;
}NODE;
NODE *createNode(int item)
{
NODE *newNode=NULL;
newNode=(NODE *)malloc(sizeof(NODE));
newNode->next=NULL;
newNode->entry =item;
return newNode;
}
NODE *insertNodeHead(NODE *head,int item)
{
NODE *newNode=NULL;
newNode=createNode(item);
if(head!=NULL)
newNode->next=head;
return newNode;
}
float calMean(NODE *head)
{
NODE *runNode=head;
int sum=0,count=0;
float ret=0;
while(runNode!=NULL)
{
sum = sum + runNode->entry;
count++;
runNode=runNode->next;
}
ret = (float)sum/count;
return ret;
}
void main(void)
{
int input;
float mean=0;
NODE *head=NULL;
// insert head node
do{
printf("input data (if exit input = 999):");
scanf("%d",&input);
if(input!=999)
head=insertNodeHead(head,input);
}while(input != 999);
if (head!=NULL)
mean=calMean(head);
printf("mean = %f\n",mean);
}
No comments:
Post a Comment