微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用链表在参数中找到最大值?

如何解决如何使用链表在参数中找到最大值?

我的任务是在给定的bool List::largest_value(int &largest)中实现一个名为class List的新类函数。指令是:

如果列表不为空,则将最大值放在最大值中 参数并返回true。如果列表为空,则返回false。

我的问题是,如何找到参数中的最大值? 这是我到目前为止为bool List::largest_value(int &largest)准备的东西:

// Fill in the functions at the bottom of this file
//
#include <iostream>
#include <climits>
using namespace std;
#include "list.h"

// on some machines member variables are not automatically initialized to 0
List::List()
{
    m_head = NULL;
}

// delete all Nodes in the list
// since they are dynamically allocated using new,they won't go away
// automatically when the list is deleted
// Rule of thumb: destructor deletes all memory created by member functions
List::~List()
{
    while (m_head)
    {
        Node *tmp = m_head;
        m_head = m_head->m_next;
        delete tmp;
    }
}

// always insert at the front of the list
// Note: this works even in the SPECIAL CASE that the list is empty
void List::insert(int value)
{
    m_head = new Node(value,m_head);
}

// iterate through all the Nodes in the list and print each Node
void List::print()
{
    for (Node *ptr = m_head; ptr; ptr = ptr->m_next)
    {
        cout << ptr->m_value << endl;
    }
}

void List::compare(int target,int &less_than,int &equal,int &greater_than)
{
    Node *temp = m_head;
    less_than = 0;
    equal = 0;
    greater_than = 0;

    while(temp != NULL)
    {
        if(temp->m_value > target)
        {
            greater_than++;
        }
        else if(temp->m_value < target)
        {
           less_than++;
        }
        else if(temp->m_value == target)
        {
            equaL++;
        }

        temp = temp-> m_next;
    }

}

bool List::largest_value(int &largest)
{
   Node *temp = m_head;
   largest = INT_MIN;


    if(temp == NULL)
    {

        return false;
    }


    while(temp != NULL)
    {
        if(temp->m_value > largest)
        {
            largest = temp->m_value;
        }
        temp = temp->m_next;

    }
    return true;

}

以下是给定的class List

class List
{
    public:
        List();
        ~List();
        void insert(int value); // insert at beginning of list
        void print(); // print all values in the list
        void compare(int target,int &greater_than);
        bool largest_value(int &largest);

    private:
        class Node
        {
            public:
                Node(int value,Node *next)
                {m_value = value; m_next = next;}
                int m_value;
                Node *m_next;
        };
        Node *m_head;
};

Main.cpp:

#include <iostream>
using namespace std;
#include "list.h"

int main()
{
    List list;
    int value;

    // read values and insert them into list
    while (cin >> value)
    {
      list.insert(value);
    }

    int largest;
    bool result = list.largest_value(largest);

    if (result == false)
    {
        cout << "empty list" << endl;
        return 1;
    }
    else
    {
        cout << "The largest value you entered is:  " <<  largest << endl;
    }
}

我的代码可以编译并运行,但是我一直收到输出empty list。老实说,我不知道需要更改bool List::largest_value(int &largest)功能。我对链表仍然很陌生。任何帮助将不胜感激

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。