Battling to print out the largest element in the list

I get the following warnings:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
49. 49 declaration shadows a field of linkedListForward
60. 57 will never be executed
81. 28 variable 'first' is uninitialized when used here

[code]
  #include<iostream>
#include<cassert>
using namespace std;

struct nodeType
{
    int info;
    nodeType *link;
};

class linkedListForward
{
public:
      nodeType *buildListForward();
     nodeType *first, *last, *newNode;
      void printList();
    int largestElement(nodeType *first) ;
};

nodeType *linkedListForward::buildListForward()
{
    int num;
    cout<<"Enter a list of integers ending with -999.\n";
    cin >> num;
    first = NULL;
    
    while(num != -999)
    {
        newNode = new nodeType;
        assert(newNode != NULL);
        
        newNode->info = num;
        newNode->link = NULL;
        
        if(first == NULL)
        {
            first = newNode;
            last = newNode;
        }
        else
        {
            last->link = newNode;
            last = newNode;
        }
        cin >> num;
    }//end while
    return first;
}//end buildListForward
int linkedListForward::largestElement(nodeType *first)
{
    int max = INT_MIN;
    while(first != NULL)
    {
        if(max < first->info)
           max = first->info;
           first = first->link;
}
    return max;
//   }
     cout<<endl<<"Largest element in myList is : "<<max <<endl;
}
void linkedListForward::printList()
{
    cout<<"Linked list printed out: " <<endl;
     nodeType *current;
     current = first;

     while(current != NULL)
     {
          cout << current->info << " ";
          current = current->link;
      }
}

int main()
{
     linkedListForward myList;
     nodeType *first;
     myList.buildListForward();
     myList.printList();
    myList.largestElement( first);
   //{cout <<"LargestElement in myList is: "<<max;}
}

The program compiles & runs fine.
It displays the linked list but does not print the largest element in the list
Hello Bopaki,

I was not able to duplicate your errors using the shell program here, but I did get others.

In "main"
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
int main()
{
    linkedListForward myList;
     
    nodeType *first{nullptr};

    //myList.buildListForward();  // Original code.
    first = myList.buildListForward();
     
    myList.printList();
     
    myList.largestElement(first);
    
   //{cout <<"LargestElement in myList is: "<<max;}
}

Please add some blank lines in your code. It makes it much easier to read and see mistakes.

Line 86 calls the function, but the function returns a value that is never captured.

In this function:
28
29
30
31
32
33
34
35
36
nodeType *linkedListForward::buildListForward()
{
    int num{};
    nodeType* first{nullptr};  // <--- Never defined and caused an error.
    
    cout<<"Enter a list of integers ending with -999.\n";
    cin >> num;
    
    //first = NULL;  // <--- Not needed if initialized when defined. 


In this function:
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
int linkedListForward::largestElement(nodeType *first)
{
    //int INT_MIN{};
    
    //int max = INT_MIN;  // INT_MIN was never defined.
    // <--- Or better choice. As long as first is not empty.
    int max{first->info};
    // <--- Or just.
    //int max{};
    
    while(first != NULL)
    {
        if(max < first->info)
           max = first->info;
           first = first->link;  // <--- Could be a problem if this changes the value in "main".
    }
    
    return max;

    cout<<endl<<"Largest element in myList is : "<<max <<endl;  //<--- Never reached because of return statement.
}

Although for line 69 you should make sure "first" is not "nullptr" before you try to use it.

From C++11 on "nullptr" is the better choice over "NULL", but "NULL" will still work.

Line 82 is never reached. Not sure if you did this for testing, but it should not be in the function.

I did not get far enough to test the "printList" function, but I did notice this line current = first;. This did not produce any errors, but where is "first" defined or where does it come from?

Andy
Thank you Andy.
This line did give the expected output:
1
2
3
4
5
   nodeType *first;
  first = myList.buildListForward();
     myList.printList();
   cout <<endl<<"LargestElement in myList is: "<<myList.largestElement(first);
 
A question about logic:
1
2
3
4
5
6
7
linkedListForward foo;
foo.buildListForward();

linkedListForward bar;
nodeType *snafu = bar.buildListForward();

int result = foo.largestElement( snafu );

Is the result the largest value in foo or largest value in bar?

If it is the largest of foo, then why don't you have:
1
2
3
4
5
int linkedListForward::largestElement() const { /*code*/ }

linkedListForward foo;
foo.buildListForward();
int result = foo.largestElement();

If it is the largest of bar, then why don't you have:
1
2
3
4
5
int largestElement( nodeType* );

linkedListForward bar;
nodeType *snafu = bar.buildListForward();
int result = largestElement( snafu );



Shadows, masks, hides ...
1
2
3
4
5
6
7
8
9
10
class linkedListForward
{
public:
  nodeType *first; // list has member 'first'
  int largestElement( nodeType *first ) {
    // function has parameter 'first'
    // parameter 'first' hides member 'first'
    // you have to use 'this->first' to access the member here
  }
};
Last edited on
Topic archived. No new replies allowed.