此题为课本p51页,算法题第6题
目录
三、遍历单链表,找出最大的值得节点得位置,并输出该节点保存得数据
一、定义结构体
typedef struct LNode
{
int data;
struct LNode* next;
} LNode;
二、用尾插法创建单链表
LNode* CreateAtTail(int a[], int n)
{
int i;
LNode* first, * s, * r;
first = (LNode*)malloc(sizeof(LNode));
first->next = NULL;
r = first;
for (i = 0; i < 5; i++)
{
s = (LNode*)malloc(sizeof(LNode));
s->data = a[i];
r->next = s;
r = s;
}
r->next = NULL;
return first;
}
三、遍历单链表,找出最大的值得节点得位置,并输出该节点保存得数据
void COMList(LNode* first)
{
LNode *p;
int max = 0;
int count = 1;
int i = 0;
int biaoji = 1;
p = first->next;
max = p->data;
while (p != NULL)
{
i++;
if (p->data > max)
{
max = p->data;
count = count + i - biaoji;
biaoji = i;
}
p = p->next;
}
printf("单链表中值最大的节点为第 %d 个值为 %d ", count, max);
}
还有