3.2 Stack Min: How would you design a stack which, in addition to push and pop, has a function min which returns the minimum element? Push, pop and min should all operate in 0(1) time.
Brain storm:
Build a stack that contains stackNodes. Every time an a element is push onto the the stack, the minimum value will be assessed. The minimum will always be contained onto top of the stack and will always be the minimum of its substack. We will maintain order of the min values as we add or remove more elements into the stack.
3.3 Stack of Plates: Imagine a (literal) stack of plates. If the stack gets too high, it might topple. Therefore, in real life, we would likely start a new stack when the previous stack exceeds some threshold. Implement a data structure SetOfStacks that mimics this. SetOfStacks should be composed of several stacks and should create a new stack once the previous one exceeds capacity. SetOfStacks. push () and SetOfStacks. pop () should behave identically to a single stack (that is, pop ( ) should return the same values as it would if there were just a single stack).
FOLLOW UP Implement a function popAt (int index) which performs a pop operation on a specific sub-stack.
template <classT>classStack {public:Stack(int capacity) { cap_s = capacity; iter_s =0; data =newT[capacity]; }~Stack() {delete[]data; }boolpush_s(T thing) {if (full()) {returnfalse; }else {data[iter_s] = thing; iter_s++;returntrue; } }boolpop_s() {if (empty_s()) {returnfalse; }else { iter_s--;returntrue; } }Ttop_s() {if (empty_s()) {return INT_MAX; }returndata[iter_s -1]; }boolempty_s() {return iter_s ==0; }private: // array T *data;int cap_s;int iter_s;boolfull() {return iter_s == cap_s; }};template <classT>classsetOfStacks {public:setOfStacks(constint capacity) { Stack<T>*s =newStack<T>(capacity);stacks.push_back(s); cap = capacity; iter =0; }voidpush(T thing) { // current stack is fullif (stacks.at(iter)->push_s(thing) ==false) { Stack<T>*s =newStack<T>(cap);stacks.push_back(s); iter++;stacks.at(iter)->push_s(thing); } }voidpop() { // everything is emptyif (empty()) { cout <<"stack is empty"<< endl;return; } // current stack is emptyelseif (subEmpty()) { // delete current stack, and move on the previousdeletestacks.at(iter); iter--;stacks.at(iter)->pop_s(); }else {stacks.at(iter)->pop_s(); } }Ttop() { // everything is emptyif (empty()) { cout <<"stack is empty"<< endl;return INT_MAX; } // current stack is emptyelseif (subEmpty()) { // pop or top can be preformed at any point so // the share the same three linesdeletestacks.at(iter);stacks.resize(stacks.size() -1); iter--;stacks.at(iter)->top_s(); } // current stack is emptyelse {returnstacks.at(iter)->top_s(); } }boolempty() {return (stacks.size() ==1&&stacks.at(iter)->empty_s()); }private:boolsubEmpty() {return (stacks.at(iter)->empty_s()); }int cap;int iter; vector<Stack<T>*> stacks;};intmain() { cout <<"// testing stack"<< endl; Stack<int>s(50);for (int i =0; i <=50; i++) {s.push_s(i); } // overflow cout <<s.push_s(51);pause();while (!s.empty_s()) { cout <<s.top_s() <<" ";s.pop_s(); } // underflow cout <<s.pop_s();pause(); cout <<"// testing set of Stacks"<< endl; setOfStacks<int>ss(5);for (int i =0; i <=100; i++) {ss.push(i); cout <<ss.top() <<" "; //pause(); } // next stackss.push(101); //pause();while (!ss.empty()) { cout <<ss.top() <<" ";ss.pop(); }pause(); // completely emptyss.pop();pause();}
3.4 Queue via Stacks: Implement a MyQueue class which implements a queue using two stacks.
The idea is that stack.top() pushed back into another stack consequtively reverses the stack, so it can effectively become a queue.
3.5 Sort Stack: Write a program to sort a stack such that the smallest items are on the top. You can use an additional temporary stack, but you may not copy the elements into any other data structure (such as an array). The stack supports the following operations: push, pop, peek, and isEmpty.
The main idea is to hold a temporary variable for the current element on the top, and then continuing pushing off elements from the second stack until you find an appropriate spot for the temp var.
O(n^2) time, O(N) space
template <typenameT>stack<T> sortStack(stack<T> s1) {if (s1.size() <=1) return s1; // start us off stack<T> temp;int pop_count =0;temp.push(s1.top());s1.pop();while (!s1.empty()) { // grab top element of s1 T temp2 =s1.top();s1.pop(); // find correct position in tempwhile (!temp.empty() &&temp.top() < temp2) {s1.push(temp.top());temp.pop(); pop_count++; }temp.push(temp2); // place backfor (int i =0; i < pop_count; i++) {temp.push(s1.top());s1.pop(); } pop_count =0; }return temp;}intmain() { vector<int> v = { 1,5,2,10,3,3,3,3,3,100,1001,232 }; stack<int> s;for (auto i : v) {s.push(i); } stack<int> s2 =sortStack(s);while (!s2.empty()) { cout <<s2.top() <<" ";s2.pop(); }pause();}