#include <iostream> using namespace std; struct stack { int* _pelem; // Pointer to element data int _capacity; int _top; stack ( int n ) :_capacity ( n) { _top = 0; _pelem = new int [ _capacity]; } ~stack () &NBSP;&NBsp; { delete []_pElem; _pElem = NULL; } }; class Stack { private: stack s; stack minstack; public: stack (int n) :s ( n) , minstack ( n) {} bool pop () { if (s._top == 0) return False ; size_t value = s._pelem [--s._top]; //determines if the minimum element will be popped if it is updates the minimum element stack if (Value == minstack._pelem[minstack._top-1]) --minstack._top; return true ; } bool push ( int value ) { if (s._top == s._capacity) return false ; if (minstack._top==0 | | &NBSP;VALUE&NBSP;<=MINSTACK._PELEM[MINSTACK._TOP-1]) minstack._pElem[minstack._top++] = value; s._pElem[s._top++] = value; return true ; } Int min () { if ( minstack._top == 0) Return null ; int pmin = minstack._ pelem[minstack._top-1]; return pmin; } };
Implement a stack, push,pop,min, and ensure time complexity is O (1)