你好,曾桑 发表于 2015-4-1 16:52:56

通过一趟遍历在单链表中确定值最大的节点

设计一个算法,通过一趟遍历确定单链表中值最大的结点:funk::funk::funk::funk::funk::funk:

wl031113 发表于 2015-4-1 17:07:25

设置一个变量max,在用指针操作链表的时候,用这个值和链表里面的值对比,比他大的话,记下位置并把值赋给max.一遍遍历就出来咯

愣头小兵 发表于 2015-8-29 14:22:59

#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]
查看完整版本: 通过一趟遍历在单链表中确定值最大的节点