w3resource

Java: Find the maximum and minimum elements in a stack


5. Find the maximum and minimum elements in a stack.

Write a Java program to find the maximum and minimum elements in a stack.

Sample Solution:

Java Code:

import java.util.Scanner;
public class Stack {
  private int[] arr;
  private int top;

  // Constructor to initialize the stack
  public Stack(int size) {
    arr = new int[size];
    top = -1;
  }

  // Method to push an element onto the stack
  public void push(int num) {
    if (top == arr.length - 1) {
      System.out.println("Stack is full");
    } else {
      top++;
      arr[top] = num;
    }
  }

  // Method to pop an element from the stack
  public int pop() {
    if (top == -1) {
      System.out.println("Stack Underflow");
      return -1;
    } else {
      int poppedElement = arr[top];
      top--;
      return poppedElement;
    }
  }

  // Method to get the top element of the stack
  public int peek() {
    if (top == -1) {
      System.out.println("Stack is empty");
      return -1;
    } else {
      return arr[top];
    }
  }

  // Method to check if the stack is empty
  public boolean isEmpty() {
    return top == -1;
  }
  // Method to get the maximum value
  public int get_Max() {
    if (top == -1) {
      System.out.println("Stack is empty");
      return -1;
    }

    int max = arr[0];

    for (int i = 1; i <= top; i++) {
      if (arr[i] > max) {
        max = arr[i];
      }
    }

    return max;
  }

  // Method to get the minimum value
  public int get_Min() {
    if (top == -1) {
      System.out.println("Stack is empty");
      return -1;
    }

    int min = arr[0];

    for (int i = 1; i <= top; i++) {
      if (arr[i] < min) {
        min = arr[i];
      }
    }

    return min;
  }

  public void display() {
    if (top == -1) {
      System.out.println("Stack is empty");
    } else {
      System.out.print("Stack elements: ");
      for (int i = top; i >= 0; i--) {
        System.out.print(arr[i] + " ");
      }
      System.out.println();
    }
  }

  public static void main(String[] args) {
    System.out.println("Initialize a stack:");
    Stack stack = new Stack(10);
    System.out.println("\nInput some elements on the stack:");
    stack.push(1);
    stack.push(3);
    stack.push(2);
    stack.push(0);
    stack.push(7);
    stack.push(5);
    stack.push(-1);
    stack.display();
    int max_val = stack.get_Max();
    System.out.println("\nMaximum value in stack: " + max_val);
    int min_val = stack.get_Min();
    System.out.println("\nMinimum value in stack: " + min_val);
  }
}

Sample Output:

Initialize a stack:

Input some elements on the stack:
Stack elements: -1 5 7 0 2 3 1

Maximum value in stack: 7

Minimum value in stack: -1

Flowchart:

Flowchart: Java  Exercises: Find the maximum and minimum elements in a stack.


Flowchart: Java  Exercises: Find the maximum and minimum elements in a stack.


Flowchart: Java  Exercises: Find the maximum and minimum elements in a stack.


For more Practice: Solve these Related Problems:

  • Write a Java program to traverse a stack and find both the maximum and minimum elements using iteration.
  • Write a Java program to implement a stack that maintains the current maximum and minimum values with each push and pop.
  • Write a Java program to use recursion to determine the maximum and minimum elements in a stack without modifying it.
  • Write a Java program to compare the maximum and minimum elements from two stacks and output the difference.

Go to:


PREV : Reverse the elements of a stack.
NEXT : Remove all the elements from a stack.

Live Demo:

Java Code Editor:

Improve this sample solution and post your code through Disqus

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.