Design and Implement Special Stack Data Structure
Question: Design a Data Structure SpecialStack that supports all the stack operations like push(), pop() and an additional operation getMin() which should return minimum element from the SpecialStack. All these operations of SpecialStack must be O(1).
Method 1: Let's say this specialStack contains two stacks, dataStack and minStack,
dataStack stores normal elements, minStack always stores the least element,
1) if it happens that an incoming new element is less than the top elements, push it in on both stacks.
2) if an incoming new element is greater than the top elements, push it onto the dataStack and skip the minStack.
[code]
class SpecialStack{
public:
stack<int> dataStack;
stack<int> minStack;
int getMin(void)
{
if(!minStack.empty()) {
int value = minStack.top();
return value;
}
cout << "empty stack" << endl;
};
void push(int x)
{
if(minStack.empty())
minStack.push(x);
else {
if (minStack.top() >= x)
minStack.push(x);
}
dataStack.push(x);
};
int pop(void)
{
if (!dataStack.empty()) {
int value = dataStack.top();
if (minStack.top() == dataStack.top())
minStack.pop();
dataStack.pop();
return value;
}
};
};
Method 2: Still, this specialStack contains two stacks, dataStack and minStack,
1) if it happens that an incoming new element is less than the top elements, push it in on both stacks.
2) if an incoming new element is greater than the top elements, push it onto the dataStack and get the top element from the minStack and push it into the minStack again.
[Code]
class SpecialStack{
public:
stack<int> dataStack;
stack<int> minStack;
int getMin(void)
{
if(!minStack.empty()) {
int value = minStack.top();
return value;
}
cout << "empty stack" << endl;
};
void push(int x)
{
if (minStack.empty()) minStack.push(x);
else {
if (x >minStack.top()) {
minStack.push(minStack.top());
}
else minStack.push(x);
}
dataStack.push(x);
};
int pop(void)
{
if (!dataStack.empty()) {
int value = dataStack.top();
dataStack.pop();
minStack.pop();
return value;
}
};
};
Design and Implement Queue using two Stacks
Question: Design a Data Structure SpecialStack that supports all the stack operations like push(), pop() and an additional operation getMin() which should return minimum element from the SpecialStack. All these operations of SpecialStack must be O(1).
Method 1: Let's say this specialStack contains two stacks, dataStack and minStack,
dataStack stores normal elements, minStack always stores the least element,
1) if it happens that an incoming new element is less than the top elements, push it in on both stacks.
2) if an incoming new element is greater than the top elements, push it onto the dataStack and skip the minStack.
[code]
class SpecialStack{
public:
stack<int> dataStack;
stack<int> minStack;
int getMin(void)
{
if(!minStack.empty()) {
int value = minStack.top();
return value;
}
cout << "empty stack" << endl;
};
void push(int x)
{
if(minStack.empty())
minStack.push(x);
else {
if (minStack.top() >= x)
minStack.push(x);
}
dataStack.push(x);
};
int pop(void)
{
if (!dataStack.empty()) {
int value = dataStack.top();
if (minStack.top() == dataStack.top())
minStack.pop();
dataStack.pop();
return value;
}
};
};
Method 2: Still, this specialStack contains two stacks, dataStack and minStack,
1) if it happens that an incoming new element is less than the top elements, push it in on both stacks.
2) if an incoming new element is greater than the top elements, push it onto the dataStack and get the top element from the minStack and push it into the minStack again.
[Code] class SpecialStack{ public: stack<int> dataStack; stack<int> minStack; int getMin(void) { if(!minStack.empty()) { int value = minStack.top(); return value; } cout << "empty stack" << endl; }; void push(int x) { if (minStack.empty()) minStack.push(x); else { if (x >minStack.top()) { minStack.push(minStack.top()); } else minStack.push(x); } dataStack.push(x); }; int pop(void) { if (!dataStack.empty()) { int value = dataStack.top(); dataStack.pop(); minStack.pop(); return value; } }; };
Design and Implement Queue using two Stacks
No comments:
Post a Comment