#include<iostream>
using namespace std;
typedef struct LNode
{
int data;
struct LNode* next;
int num;
}LNode,*LinkList;
LinkList& createList(LinkList& L, int n) //创建链表
{
L = new LNode;
L->next = NULL;
L->num = 0;
LinkList r = L;
for (int i = 0; i < n; i++)
{
LNode* p = new LNode;
cin >> p->data;
p->next = NULL;
r->next = p;
r = p;
L->num++;
}
return L;
}
void showList(LinkList L) //输出链表
{
if (L->num == 0)
cout << "链表为空!" << endl;
else
{
int q = L->num;
for (int i = 0; i < q; i++)
{
if (L->next->data != NULL)
{
cout << L->next->data << " ";
L = L->next;
}
}
cout << endl;
}
}
int maxData(LinkList L) //求链表确定值最大的节点
{
if (L->num == 0)
cout << "链表为空!" << endl;
else
{
int max = 0xc0c0c0c0; //先初始化max为无穷小
int q = L->num;
for (int i = 0; i < q; i++)
{
if (L->next->data != NULL)
{
if (L->next->data > max)
{
int data = L->next->data;
int temp = data;
data = max;
max = temp;
L = L->next;
}
}
}
return max;
}
}
int main()
{
LinkList L1;
int n1;
cout << "请输入单链表的长度:" << endl;
cin >> n1;
cout << "请输入单链表的内容:" << endl;
L1 = createList(L1, n1);
cout << "单链表为:" << endl;
showList(L1);
cout << "该链表中确定值最大的节点为:" << endl;
cout << maxData( L1)<<endl;
return 0;
}
数据结构:找出链表确定值最大的节点
最新推荐文章于 2024-10-09 18:39:46 发布