#include
#include
#define N 4
struct node{
int score;
};
struct node node;
struct node*list,*p,*r,*q;
void input(struct node*list)
{
int i;
int ch;
for(i=0;i {
printf("please input the score of %d\t",i+1);
p=(struct node*)malloc(sizeof(struct node*));
scanf("%d",&p->score);
if(list==NULL)
list=p;
else
r=p;
printf("数据为%d",p->score);
}
}
void average(struct node*list)
{
int s=0,i;
double average2;
if(list==NULL)
printf("链表为空");
for(i=0;i {
s+=list->score;
}
average2=(double)s/N;
printf("%f",average2);
}
void print(struct node*list)
{
int i;
printf("顺利进入");
if(list==NULL)
printf("这是空链表");
else
{
q=list;
{
printf("%d",q->score);
}
}}
int main()
{
void input(struct node*list);
void average(struct node*list);
void print(struct node*list);
int ah;
system("cls");
while(1){
printf("链表操作系统:");
printf("\n1.建立链表\n2.输出数据\n3.求平均数\n");
scanf("%d",&ah);
switch(ah){
case 1:input(list);break;
case 2:print(list);break;
case 3:average(list);break;
default:printf("wrong!");break;
}}}

3个回答

``````#include<iostream>
#include<stdio.h>
using namespace std;
#define N 4
struct node{
int score;
};
struct node node;
struct node*list,*p,*r,*q;
void input()
{
int i;
int ch;
list = NULL;
for(i=0;i < N; i++)
{
p=(struct node*)malloc(sizeof(struct node));
printf("please input the score of %d\t",i+1);
scanf("%d",&p->score);
if(list==NULL)
list=p;
else
r=p;
printf("数据为%d",p->score);
}
}
void average()
{
int s=0,i;
double average2;

if(list==NULL)
printf("链表为空");
for(i=0;i < N; i++)
{
s+=list->score;
}
average2=(double)s/N;
printf("平均值为:%lf\n",average2);
}
void print()
{
int i;
printf("顺利进入\n");
if(list==NULL)
printf("这是空链表");
else
{
q=list;
while(q!=NULL)//这里只需要判断q就行
{
printf("%d\n",q->score);
}
}}
int main()
{
void input();
void average();
void print();
int ah;
system("cls");
while(1){
printf("链表操作系统:");
printf("\n1.建立链表\n2.输出数据\n3.求平均数\n");
scanf("%d",&ah);
switch(ah){
case 1:input();break;
case 2:print();break;
case 3:average();break;
default:printf("wrong!");break;
}
}
}
``````

1.既然list指针已经变成全局的了，就不要传参了，楼主那样传一级指针是没有用的，操作的都是list的一个副本，所以就算插入的代码没错，

2.输出的函数里while循环只要判断while(q!=NULL)就行了