voidMinStack::push(int val){ if (isEmpty()) { Top = newMinStack(val); size++; return; } MinStack *newNode = newMinStack(val); newNode->next = Top; Top = newNode; size++; }
voidMinStack::pop(){ if (isEmpty()) { return; } MinStack *tempNode = Top; Top = Top->next; delete tempNode; size--; }
intMinStack::top(){ if (!isEmpty()) { return Top->val; } return-1; }
intMinStack::getMin(){ if (isEmpty()) { return-1; } MinStack *ptr = Top; int minValue = ptr->val; while (ptr != NULL) { if (ptr->val < minValue) { minValue = ptr->val; } ptr = ptr->next; } return minValue; }
boolMinStack::isEmpty(){ return Top == nullptr; }
/** * Your MinStack object will be instantiated and called as such: * MinStack* obj = new MinStack(); * obj->push(val); * obj->pop(); * int param_3 = obj->top(); * int param_4 = obj->getMin(); */