DJYLSY
DJYLSY
2016-03-30 11:36

程序中定义的全局变量,list 在input中已经赋值,为什么print函数中List=null?

  • 数据
  • 链表
  • 全局变量

#include
#include
#define N 4
struct node{
int score;
struct node*link;
};
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);
p->link=NULL;
if(list==NULL)
list=p;
else
r->link=p;
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;
list=list->link;
}
average2=(double)s/N;
printf("%f",average2);
}
void print(struct node*list)
{
int i;
printf("顺利进入");
if(list==NULL)
printf("这是空链表");
else
{
q=list;
while(q->link!=NULL)
{
printf("%d",q->score);
q=q->link;
}
}}
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条回答