#include struct Num
{
int num;
struct Num *next;
};
struct Num *create() //建立链表
{
struct Num *head,*tail,*p;
head = tail = NULL;
while(1)
{
p = (struct Num *)malloc(sizeof(struct Num));
scanf_s("%d",&p->num);
if(p->num == -1)
{
tail->next = NULL;
free(p);
break;
}
if(head == NULL)
head = p;
else
tail->next = p;
tail = p;
}
return head;
}
int Max(struct Num *head) //求链表最大值
{
struct Num *p;
int mx = 0;
p = head;
mx = p->num;
while(1)
{
if(p == NULL)
break;
else
{
p = p->next;
if(p->num > mx)
mx = p->num;
}
}
return mx;
}
int main()
{
struct Num *head,*p;
int m;
printf("请输入整数,输入-1结束\n");
head = create();
p = head;
printf("整数串是\n");
while(p != NULL)
{
printf("%d",p->num);
printf("\t");
p = p->next;
}
m = Max(head);
printf("最大值为%d\n",m);
system("pause");
return 0;
}
如果不使用Max函数可以正常运行,但是加入Max函数就会报错...
求解