代码生成(链表寻找最大值并返回位序)
时间: 2023-04-04 13:02:56 浏览: 92
关于链表的相关运算的代码
好的,我可以回答这个问题。以下是代码示例:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
int findMax(Node* head, int* pos) {
int max = head->data;
int index = 1;
Node* p = head->next;
while (p != NULL) {
if (p->data > max) {
max = p->data;
*pos = index;
}
p = p->next;
index++;
}
return max;
}
int main() {
Node* head = (Node*)malloc(sizeof(Node));
head->data = 1;
head->next = NULL;
Node* p = head;
for (int i = 2; i <= 10; i++) {
Node* node = (Node*)malloc(sizeof(Node));
node->data = i;
node->next = NULL;
p->next = node;
p = node;
}
int pos = 1;
int max = findMax(head, &pos);
printf("最大值为:%d,位序为:%d\n", max, pos);
return 0;
}
阅读全文