I have the following code for finding the max value in a stack. It’s working but should I use another approach for finding the max value because after calling the getMax() function, I’m not able to display the stack a.
int Sorter:: getMax(){
c.push(a.top());
a.pop();
while(!a.empty()){
if(a.top() > c.top()){
c.pop();
c.push(a.top());
a.pop();
}
else
a.pop();
}
return c.top();
}
Keep the maximum value in a side variable: