Find the Maximum Element of the Stack

C++C++Beginner
Practice Now

Introduction

In this tutorial, you will learn how to find the maximum element of a stack in the C++ programming language. We will provide you with a step-by-step guide on how to use a stack to find the maximum element, and we will also explain each line of code in detail.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL cpp(("`C++`")) -.-> cpp/BasicsGroup(["`Basics`"]) cpp(("`C++`")) -.-> cpp/ControlFlowGroup(["`Control Flow`"]) cpp(("`C++`")) -.-> cpp/FunctionsGroup(["`Functions`"]) cpp/BasicsGroup -.-> cpp/variables("`Variables`") cpp/BasicsGroup -.-> cpp/data_types("`Data Types`") cpp/BasicsGroup -.-> cpp/operators("`Operators`") cpp/ControlFlowGroup -.-> cpp/conditions("`Conditions`") cpp/ControlFlowGroup -.-> cpp/while_loop("`While Loop`") cpp/FunctionsGroup -.-> cpp/function_parameters("`Function Parameters`") subgraph Lab Skills cpp/variables -.-> lab-96137{{"`Find the Maximum Element of the Stack`"}} cpp/data_types -.-> lab-96137{{"`Find the Maximum Element of the Stack`"}} cpp/operators -.-> lab-96137{{"`Find the Maximum Element of the Stack`"}} cpp/conditions -.-> lab-96137{{"`Find the Maximum Element of the Stack`"}} cpp/while_loop -.-> lab-96137{{"`Find the Maximum Element of the Stack`"}} cpp/function_parameters -.-> lab-96137{{"`Find the Maximum Element of the Stack`"}} end

Create a new C++ file

Create a new file named main.cpp in the ~/project directory. In the main.cpp file, include the necessary C++ libraries:

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

Define the functions for the Stack

Define two functions: findMax() to find the maximum element of the stack, and show() to print the elements of the stack.

void findMax(stack<int> s) {
    int m = s.top();
    int a;

    while (!s.empty()) {
        a = s.top();
        if (m < a)
            m = a;
        s.pop();
    }
    cout << "\n\nThe maximum element of the stack is: " << m << endl;
}

void show(stack<int> s) {
    while (!s.empty()) {
        cout << "  " << s.top();
        s.pop();
    }
    cout << endl;
}

Define the main() function

In the main() function, create a new stack of type int and fill it with elements using the push() method.

Call the show() function to print the elements of the stack in LIFO order.

Finally, call the findMax() function to find the maximum element in the stack.

int main() {
    cout << "\n\nWelcome to the Stack Program in C++!\n\n\n";

    stack<int> s;
    s.push(4);
    s.push(2);
    s.push(20);
    s.push(12);
    s.push(52);
    s.push(14);

    cout << "The elements of the Stack in LIFO order are: ";
    show(s);

    findMax(s);

    return 0;
}

Compile and run the program

Compile the program using the following command in the terminal:

g++ main.cpp -o main && ./main

View the Output

The output of the program should be as follows:

Welcome to the Stack Program in C++!

The elements of the Stack in LIFO order are:   14  52  12  20  2  4
The maximum element of the stack is: 52

Summary

In this tutorial, we learned how to find the maximum element in a stack in the C++ programming language. We hope this tutorial was helpful to you!

Other C++ Tutorials you may like