用C语言设计一个算法,查找带头结点的非空单链表L中第一个最大结点(最大结点可能有多个),并返回该结点的逻辑序号。
时间: 2024-05-05 09:17:09 浏览: 87
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
void createList(Node** L) {
*L = (Node*)malloc(sizeof(Node));
(*L)->next = NULL;
Node* p = *L;
int x;
while (scanf("%d", &x) != EOF) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = x;
newNode->next = NULL;
p->next = newNode;
p = newNode;
}
}
int findMax(Node* L) {
int max = L->next->data;
int index = 1;
int maxIndex = 1;
Node* p = L->next;
while (p != NULL) {
if (p->data > max) {
max = p->data;
maxIndex = index;
}
index++;
p = p->next;
}
return maxIndex;
}
int main() {
Node* L;
createList(&L);
int maxIndex = findMax(L);
printf("%d", maxIndex);
return 0;
}
```
阅读全文