min-stack 1.0.0
Min Stack
Loading...
Searching...
No Matches
MinStack Class Reference
Collaboration diagram for MinStack:

Public Member Functions

 MinStack ()
 
void push (int val)
 
void pop ()
 
int top ()
 
int getMin ()
 

Private Attributes

stack< int, deque< int > > s
 
priority_queue< int, vector< int >, greater< int > > q
 

Detailed Description

Definition at line 5 of file main.cpp.

Constructor & Destructor Documentation

◆ MinStack()

MinStack::MinStack ( )
inline

Definition at line 12 of file main.cpp.

12: s(), q() {}
priority_queue< int, vector< int >, greater< int > > q
Definition main.cpp:9
stack< int, deque< int > > s
Definition main.cpp:8

Member Function Documentation

◆ getMin()

int MinStack::getMin ( )
inline

Definition at line 62 of file main.cpp.

63 {
64 if (s.empty() || q.empty())
65 {
66 if (s.empty())
67 throw runtime_error("min-stack.getMin: empty stack");
68 else
69 throw runtime_error("min-stack.getMin: empty priority queue");
70 }
71 return q.top();
72 }

References q, and s.

◆ pop()

void MinStack::pop ( )
inline

Definition at line 20 of file main.cpp.

21 {
22 if (s.empty() || q.empty())
23 {
24 if (s.empty())
25 throw runtime_error("min-stack.pop: empty stack");
26 else
27 throw runtime_error("min-stack.pop: empty priority queue");
28 return;
29 }
30 int el = s.top();
31 queue<int> temp;
32 while (!q.empty() && q.top() != el)
33 {
34 temp.push(q.top());
35 q.pop();
36 }
37 if (!q.empty())
38 {
39 q.pop();
40 while (!temp.empty())
41 {
42 q.push(temp.front());
43 temp.pop();
44 }
45 }
46 s.pop();
47 }

References q, and s.

◆ push()

void MinStack::push ( int  val)
inline

Definition at line 14 of file main.cpp.

15 {
16 s.push(val);
17 q.push(val);
18 }

References q, and s.

◆ top()

int MinStack::top ( )
inline

Definition at line 49 of file main.cpp.

50 {
51 if (s.empty() || q.empty())
52 {
53 if (s.empty())
54 throw runtime_error("min-stack.top: empty stack");
55 else
56 throw runtime_error("min-stack.top: empty priority queue");
57 return -1;
58 }
59 return s.top();
60 }

References q, and s.

Field Documentation

◆ q

priority_queue<int, vector<int>, greater<int> > MinStack::q
private

Definition at line 9 of file main.cpp.

Referenced by getMin(), pop(), push(), and top().

◆ s

stack<int, deque<int> > MinStack::s
private

Definition at line 8 of file main.cpp.

Referenced by getMin(), pop(), push(), and top().


The documentation for this class was generated from the following file: