#include
#include
typedef 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;
}