通过一趟遍历在单链表中确定值最大的节点
设计一个算法,通过一趟遍历确定单链表中值最大的结点:funk::funk::funk::funk::funk::funk: 设置一个变量max,在用指针操作链表的时候,用这个值和链表里面的值对比,比他大的话,记下位置并把值赋给max.一遍遍历就出来咯 #include <stdio.h>#include <stdlib.h>
typedef struct node
{
int data;
struct node *link;
}Node;
Node *MaxDataNode(Node *p)
{
Node *q,*r;
int max;
if(p==NULL)
return NULL;
else
{
r=p;
max=r->data;
q=r;
while(r)
{
if(r->data>max)
{
max=r->data;
q=r;
}
r=r->link;
}
return q;
}
}
int main()
{
return 0;
}
页:
[1]