﻿ C++ Find the minimum element in a stack (using an array)

# C++ Stack Exercises: Find the minimum element in a stack (using an array)

## C++ Stack: Exercise-7 with Solution

Write a C++ program to find the minimum element in a stack (using an array).

Test Data:
Input some elements onto the stack:
Stack elements: 0 1 5 2 4 7
Minimum value: 0

Sample Solution:

C++ Code:

``````#include <iostream>

using namespace std;
#define MAX_SIZE 15 // Maximum size of stack

class Stack {
private:
int top; // Index of top element
int arr[MAX_SIZE]; // Array to store elements
public:
Stack() {
top = -1; // Initialize top index to -1 (empty stack)
}

bool push(int x) {
if (isFull()) {
cout << "Stack overflow" << endl;
return false;
}
// Increment top index and add element to array
arr[++top] = x;
return true;
}

int pop() {
if (isEmpty()) {
cout << "Stack underflow" << endl;
return 0;
}
return arr[top--];
}

int peek() {
if (isEmpty()) {
cout << "Stack is empty" << endl;
return 0;
}
return arr[top];
}

bool isEmpty() {
// Stack is empty if top index is -1
return (top < 0);
}

bool isFull() {
// Stack is full if top index is equal to MAX_SIZE - 1
return (top >= MAX_SIZE - 1);
}

void display() {
if (top < 0) {
cout << "Stack is empty" << endl;
return;
}
cout << "\nStack elements: ";
for (int i = top; i >= 0; i--)
cout << arr[i] << " ";
cout << endl;
}

int min() {
if (isEmpty()) {
// Return the maximum possible integer if the stack is empty
return INT_MAX;
}
int minVal = arr[0];
for (int i = 1; i <= top; i++) {
if (arr[i] < minVal) {
// Update the minimum value if we find a smaller element
minVal = arr[i];
}
}
// Return the minimum value
return minVal;
}
};

int main() {
//Initialize the stack stk
Stack stk;
cout << "Input some elements onto the stack:";
stk.push(7);
stk.push(4);
stk.push(2);
stk.push(5);
stk.push(1);
stk.push(0);
stk.display();
// Find the maximum value
int max_val = stk.min();
cout << "\nMinimum value: " << max_val << endl; // Display the minimum value
cout << "\nRemove two elements:";
stk.pop();
stk.pop();
stk.display();
cout << "\nInput two more elements";
stk.push(-1);
stk.push(10);
stk.display();
max_val = stk.min();
cout << "\nMinimum value: " << max_val << endl; // Display the minimum value
cout << endl;
return 0;
}
``````

Sample Output:

```Input some elements onto the stack:
Stack elements: 0 1 5 2 4 7

Minimum value: 0

Remove two elements:
Stack elements: 5 2 4 7

Input two more elements
Stack elements: 10 -1 5 2 4 7

Minimum value: -1
```

Flowchart:

CPP Code Editor:

Previous C++ Exercise: Find the maximum element in a stack (using an array).
Next C++ Exercise: Find the middle element of a stack (using an array).

What is the difficulty level of this exercise?

﻿

## C++ Programming: Tips of the Day

Can you remove elements from a std::list while iterating through it?

You have to increment the iterator first (with i++) and then remove the previous element (e.g., by using the returned value from i++). You can change the code to a while loop like so:

```std::list<item*>::iteratori = items.begin();
while (i != items.end())
{
boolisActive = (*i)->update();
if (!isActive)
{
items.erase(i++);  // alternatively, i = items.erase(i);
}
else
{
other_code_involving(*i);
++i;
}
}
```

Ref : https://bit.ly/3smQY4u