0% found this document useful (0 votes)
5 views2 pages

MinMaxStack Implementation in C++

The document presents a C++ implementation of a MinMaxStack class that supports standard stack operations while also allowing retrieval of the minimum and maximum elements in constant time. It includes methods for pushing and popping elements, checking if the stack is empty, getting the size, and clearing the stack. The main function demonstrates the usage of the MinMaxStack by pushing and popping elements and testing the clear operation.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

MinMaxStack Implementation in C++

The document presents a C++ implementation of a MinMaxStack class that supports standard stack operations while also allowing retrieval of the minimum and maximum elements in constant time. It includes methods for pushing and popping elements, checking if the stack is empty, getting the size, and clearing the stack. The main function demonstrates the usage of the MinMaxStack by pushing and popping elements and testing the clear operation.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <iostream>

#include <stack>
#include <climits>

class MinMaxStack {
private:
std::stack<int> mainStack;
std::stack<int> minStack;
std::stack<int> maxStack;

public:
void push(int element) {
[Link](element);
if ([Link]() || element <= [Link]()) {
[Link](element);
}
if ([Link]() || element >= [Link]()) {
[Link](element);
}
}

int pop() {
if (isEmpty()) {
throw std::runtime_error("Stack is empty");
}
int topElement = [Link]();
[Link]();
if (topElement == [Link]()) {
[Link]();
}
if (topElement == [Link]()) {
[Link]();
}
return topElement;
}

int peek() {
if (isEmpty()) {
throw std::runtime_error("Stack is empty");
}
return [Link]();
}

bool isEmpty() {
return [Link]();
}

int size() {
return [Link]();
}

int getMin() {
if (isEmpty()) {
throw std::runtime_error("Stack is empty");
}
return [Link]();
}

int getMax() {
if (isEmpty()) {
throw std::runtime_error("Stack is empty");
}
return [Link]();
}

void clear() {
while (!isEmpty()) {
pop();
}
}
};

int main() {
MinMaxStack stack;
std::cout << "Pushing elements: 5, 3, 7, 1, 9" << std::endl;
[Link](5);
[Link](3);
[Link](7);
[Link](1);
[Link](9);
std::cout << "Stack size: " << [Link]() << std::endl;
std::cout << "Top element: " << [Link]() << std::endl;
std::cout << "Minimum element: " << [Link]() << std::endl;
std::cout << "Maximum element: " << [Link]() << std::endl;
std::cout << "\nPopping elements:" << std::endl;
while (![Link]()) {
std::cout << "Popped: " << [Link]() << std::endl;
if (![Link]()) {
std::cout << "New min: " << [Link]() << std::endl;
std::cout << "New max: " << [Link]() << std::endl;
}
}
std::cout << "\nTesting clear operation" << std::endl;
[Link](10);
[Link](20);
std::cout << "Size before clear: " << [Link]() << std::endl;
[Link]();
std::cout << "Size after clear: " << [Link]() << std::endl;
return 0;
}

You might also like