//遍历查找长度为n的单链表的最大值的结点
#include<iostream>
using namespace std;
typedef int Status;
typedef int elemtype;
#define OK 1
#define ERROR 0
typedef struct LNode
{
elemtype data;
struct LNode* next;
}LNode, * LinkList;
void CreateList_L(LinkList& L, int n)
{
int i;
LNode* r, * p;
L = new LNode;
L->next = NULL;
r = L;
cout << "请输入元素:\n";
for (i = 0; i < n; i++)
{
cout << "第" << i+1 << "个结点为:" << endl;
p = new LNode;
cin >> p->data;
p->next = NULL;
r->next = p;
r = p;
}
}
void Maxelem_L(LinkList& L)
{
int max = 0;
int i = 0;
LNode* p = L->next;
max = p->data;
while (p)
{
if (p->data > max)
max = p->data;
p = p->next;
}
p = L->next;
while (p)
{
i++;
if (p->data == max)
cout << "第" << i << "个结点," << "值为" << p->data << endl;
p = p->next;
}
cout << endl;
}
int main()
{
int n1;
LNode* la;
cout << "输入线性表元素个数:" << endl;
cin >> n1;
CreateList_L(la, n1);
cout << "链表中最大的元素是:" << endl;
Maxelem_L(la);
return 0;
}
11-10
3679
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
10-24
1448
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)