-1

Hello so it seems that my code will execute but will not return the max node or anything, extremely frustrating because I cant seem to figure this out if you need additional code please ask.

int LinkedList::maxNode(listNode *p)
{
    int current = p->val;
    int next;

    if (p->next == NULL) 
    {
        return current;
    }
    else 
    {
        next = maxNode(p->next);
    }
    if (current > next) {
        return current;
    }
    else {
        return next;
    }
}
int LinkedList::findMaxNode()
{
    return maxNode(head);
}

that was the function and this is the main

case 'M':
case 'm':
Mylist.findMaxNode();
break;

and this is the header file please don't give me the answer just show me my error

class LinkedList
{
protected:
    struct listNode
    {
        struct listNode* next;
        double val;
    };
public:
    listNode* head;
    LinkedList();
    LinkedList(const LinkedList&);
    ~LinkedList();

    void appendNode(double);
    void insertNode(double);
    void insertNodeByPosistion(double, int);
    void deleteNodeByPosition(int pos);
    void destroyNode(double);
    void reverseList(listNode*);
    bool searchList(int);
    int maxNode(listNode *p);
    int findMaxNode();
    void displayList(listNode*);
    int operator[](int);
};
  • 1
    I don't see any printing statements. Where are you trying to print stuff? – cigien Apr 16 '20 at 19:32
  • sorry badly worded I meant return the max node – ThanosCheeks Apr 16 '20 at 19:37
  • A [mcve] would help quite a bit. If your code is executing perfectly then you wouldn't be here with the question, so show us the code and any input necessary, explain the desired output and what is incorrect and perhaps we can figure it out together. If your list stores doubles why are you returning an int? – Retired Ninja Apr 16 '20 at 19:38
  • 1
    How do you know it's not returning anything? You are not looking at the value returned by `findMaxNode`. – cigien Apr 16 '20 at 19:40
  • when I compile it max node is not returned and nothing appears in the compiler – ThanosCheeks Apr 16 '20 at 19:41
  • @cigien this code it 400 lines that is why I only showed certain parts of it – ThanosCheeks Apr 16 '20 at 19:42
  • The code you have shown does not indicate the `findMaxNode` doesn't work. Maybe try printing it out. – cigien Apr 16 '20 at 19:43
  • @cigien thanks man teaching myself c++ has been long and ardous journey but after binary trees I should be done thanks a lot for your help – ThanosCheeks Apr 16 '20 at 19:45
  • No problem. And I don't mean to be a downer, but there's a *lot* more to learn after BSTs. – cigien Apr 16 '20 at 19:46

2 Answers2

2

Not sure if you're planning to print later after the switch in your main, but you're not doing anything with the value when you find the value.

probably mean to do something like:

case 'M':
case 'm':
 std::cout << Mylist.findMaxNode() << std::endl;
break;

side note, you probably want to be comparing you pointer to nullptr instead of NULL. Take a look at NULL vs nullptr (Why was it replaced?)

Noah Guld
  • 36
  • 3
0

You do not use the returned value from the function.

case 'M':
case 'm':
Mylist.findMaxNode();
break;

For starters the function maxNode should be a static member function and declared in the private section of the class. It is an auxiliary function that called under the hood by the function findMaxNode.

private:
    static int maxNode(listNode *p);

In turn the function findMaxNode should be a constant member function

int findMaxNode() const;

You should decide what to return from the function when the list is empty. Otherwise the function will invoke undefined behavior when it will called for an empty list.

Let's assume that in this case the function returns 0.

Then the function can look the following way

int LinkedList::maxNode( const listNode *p )
{
    return listNode == nullptr ? 0 : std::max( { listNode->val, maxNode( listNode->next ) } );
}

You will need to include the header

#include <algorithm>

Then you can write for example

case 'M':
case 'm':
    std::cout << "The maximum value is " << Mylist.findMaxNode() << '\n';
    break;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335