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();}