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

2024-11-22 12:21:42
推荐回答(2个)
回答1:

#include
#include

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:

这个就是顺序查找,用一个指针往后走