#include <stdio.h>
#include <stdlib.h>
struct node //结点数据类型
{
int data;
struct node *next;
};
//实现以下二个函数中的一个。
struct node * largestnode1(struct node *head);
void largestnode2(struct node *head,struct node **ppmax);
void destroy(struct node* head);
struct node* headinsert(struct node* head);
main()
{
struct node *head=NULL;
struct node * pmax;
head=headinsert(NULL); //用前插法创建链表
//找到最大结点,将其地址传给pmax
// pmax=largestnode1(head);
//largestnode2(head,&pmax);
pmax = largestnode1(head); //选择上面二个被注释掉的语句中1个,并实现相应的函数。建议有的同学,可以二种都尝试一下,并提交二次
printf("%d",pmax->data);
//释放资源
destroy(head);
}
}
return head;
struct node* headinsert(struct node* head)
{ struct node *p;
int n=0;
//采用相同的方式创建6个结点,并且前插
while(n<6)
{
//申请结点空间,创建新结点,并让p指向新结点
p = (struct node *)malloc(sizeof(struct node));
//初始化结点数据域
scanf("%d",&(p->data));
//挂链
p->next=head; //新结点的指针域,指向原来的第1个结点
head=p; //让新结点成为第1个结点
n++; //返回第1个结点的地址
}
struct node * largestnode1(struct node* head)
{ struct node *pmax=head; //先假定第1个结点最大
struct node *p=head->next; //p指向第2个结点
while (p!= NULL) {
if (p->data > pmax->data) {
pmax = p;
}
p = p->next;
}
return pmax;
}
void largestnode2(struct node *head,struct node **ppmax)
{
*ppmax=head; //先假定第1个结点最大
struct node *p=head->next; //p指向第2个结点
while (p!= NULL) {
if (p->data > (*ppmax)->data) {
*ppmax = p;
}
p = p->next;
}
}
void destroy(struct node* head)
{ struct node *p;
while(head!=NULL)
{
p = head; //p指向要销毁的结点
head = head ->next; //head指向再下一个要销毁的结点
free(p); //销毁p指向的结点
}
}