请用c完成下面案例输入若干个不超过100的整数建立单链表然后通过一趟遍历在单链表中确定值最大的结点。输出该结点的值及其序号。输入格式首先输入一个整数T表示测试数据的组数然后是T组测试数据。每组测试数据在一行上输入数据个数n及n个不超过100的整数。输出格式对于每组测试输出单链表中值最大的结点的值和该结点的序号。输出格式如下:max=dmax num=dnum其中dmax表示最大的结点的值dnum表
#include <stdio.h> #include <stdlib.h>
struct node { int data; int order; struct node *next; };
int main() { int t; scanf("%d", &t); while (t--) { int n, max, num; scanf("%d", &n); struct node *head = (struct node *)malloc(sizeof(struct node)); head->next = NULL; struct node *p = head; for (int i = 1; i <= n; i++) { int x; scanf("%d", &x); struct node *newnode = (struct node *)malloc(sizeof(struct node)); newnode->data = x; newnode->order = i; newnode->next = NULL; p->next = newnode; p = p->next; } max = head->next->data; num = head->next->order; p = head->next; while (p != NULL) { if (p->data > max) { max = p->data; num = p->order; } p = p->next; } printf("max=%d num=%d\n", max, num); } return 0; }
原文地址: https://www.cveoy.top/t/topic/bbEM 著作权归作者所有。请勿转载和采集!