已知head指向一个单链表,链表的每一个结点包含数据域(date)和指针域(next),数据域为整

已知head指向一个单链表,链表的每一个结点包含数据域(date)和指针域(next),数据域为整型。编程在链表中查找数据域值最大的结点。

第1个回答  推荐于2017-09-19
#include <stdio.h>
#include <stdlib.h>

typedef struct node {
int data;
struct node *next;
}Node, *LinkList;

LinkList head = NULL;

LinkList addNode(int data){
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
node->next = head;
head = node;
return head;
}

int main()
{
addNode(5);
addNode(8);
addNode(10);
addNode(3);
LinkList ll = head;
int index = 1;
int max = 0, the_index=1;
while(ll){
if(max < ll->data){
max = ll->data;
the_index = index;
}
index++;
ll=ll->next;
}
printf("max data is %d and the index is %d", max, the_index);

return 0;
}本回答被网友采纳
第2个回答  2016-05-24
这个就是顺序查找,用一个指针往后走
相似回答