#includetypedef struct _node
{
int data;
_node *next;
}node,*list;
int getmax_value(list head)
{
if (head->next == null)
{
printf("链表中没有节点.\n");
exit(-1);
}
node *ptr = head->next;
int max = ptr->data;
while (ptr->next != null)
{
ptr = ptr->next;
if (ptr->data > max)
{
max = ptr->data;
}
}
return max;
}
node* getmax_address(list head)
{
if (head->next == null)
{
printf("链表中没有节点.\n");
exit(-1);
}
node *ptr = head->next;
node *max_address = ptr;
while (ptr->next != null)
{
ptr = ptr->next;
if (ptr->data > max_address->data)
{
max_address = ptr;
}
}
return max_address;
}
list creatlist(int num)//num为创建节点个数
{
list head = (node*)malloc(sizeof(node));
head->data = 0;
head->next = null;
node *ptr = head;
node *node = null;
int data;
while (num)
{
printf("请输入节点则值:");
scanf("%d", &data);
node = (node*)malloc(sizeof(node));
node->data = data;
node->next = null;
ptr->next = node;
ptr = node;
head->data++;
num--;
}
return head;
}
void freelist(list head)
{
node* ptr = head;
while (head)
{
head = ptr->next;
free(ptr);
ptr = head;
}
}
int main()
{
list head=creatlist(5);
printf("链表的最大值为:%d\n", getmax_value(head));
printf("链表最大值的地址为:0x%x,%d\n", getmax_address(head), (getmax_address(head))->data);
freelist(head);
return 0;
}