3.3 Stacks are like stacks of plates, and when the plates are stacked too high, they tilt down. Therefore, in the real world, when a stack of plates exceeds a certain height, we will start a pile and fold it from scratch. Implement the data structure setofstacks to simulate this situation. The setofstacks consists of several stacks, and when the current stack exceeds capacity, a new stack is created to hold the data. Setofstacks.push () and Setofstacks.pop () should behave as if they were only one stack.
Further,
Implements the function Popat (int index) to perform a pop operation on the specified sub-stack.
Implement the first step:
#include <iostream>#include<New>using namespacestd;//Mystack represents a stackclassmystack{Private: int*buf; intcapacity; intcur; Public: Mystack (intSize=Ten) {buf=New int[size]; Capacity=size; Cur=-1; } ~Mystack () {delete[] buf; } voidPushintx) {buf[++cur]=x; } voidpop () {cur--; } intTop () {returnBuf[cur]; } BOOLempty () {returncur==-1; } BOOLFull () {returncur==capacity-1; }};//an array consisting of a stackclasssetofstacks{Private: Mystack*St; intcur; intcapacity; Public: Setofstacks (intcap=Ten) {St=NewMystack[cap]; Capacity=cap; Cur=0; } ~setofstacks () {delete[] st; } voidPushintx) {if(St[cur].full ()) Cur++; if(cur>capacity-1) return; St[cur].push (x); } voidpop () {if(St[cur].empty ()) Cur--; if(cur<0) return; St[cur].pop (); } intTop () {if(St[cur].empty ()) Cur--; if(cur<0) return 0; Else returnSt[cur].top (); } BOOLempty () {returncur==0&&St[cur].empty (); } BOOLFull () {returncur==capacity-1&&St[cur].full (); }};intMain () {Setofstacks ss1; for(intI=0; i<3*Ten+1; ++i) {ss1.push (i); } while(!Ss1.empty ()) {cout<<ss1.top () <<Endl; Ss1.pop (); } return 0;}
When the Popat function is added, the situation becomes complicated. Because this time the data distribution may appear in the middle of some of the sub-stacks using Popat to clear them, and the subsequent sub-stack has data. For convenience, we do not consider the wasted space caused by Popat. That is, if I use the Popat to clear some of the middle of the sub-stack, do not put the back-face stack of data moving forward. In this way, cur points to the "last" stack of the operation, the subsequent sub-stack must be empty, and it itself and the preceding sub-stack due to the Popat function may be empty. If there is no Popat function, the cur in front of the stack must be full (see example above). In this way, push still only needs to determine whether the current sub-stack is full. However, the pop function is always looking forward from cur until a non-empty sub-stack is found for the pop operation. Similarly, Popat,top,empty is the same.
#include <iostream>#include<New>using namespacestd;//Mystack represents a stackclassmystack{Private: int*buf; intcapacity; intcur; Public: Mystack (intSize=Ten) {buf=New int[size]; Capacity=size; Cur=-1; } ~Mystack () {delete[] buf; } voidPushintx) {buf[++cur]=x; } voidpop () {cur--; } intTop () {returnBuf[cur]; } BOOLempty () {returncur==-1; } BOOLFull () {returncur==capacity-1; }};//an array consisting of a stackclasssetofstacks{Private: Mystack*St; intcur; intcapacity; Public: Setofstacks (intcap=Ten) {St=NewMystack[cap]; Capacity=cap; Cur=0; } ~setofstacks () {delete[] st; } voidPushintx) {if(St[cur].full ()) Cur++; if(cur>capacity-1) return; St[cur].push (x); } voidpop () { while(St[cur].empty ()) Cur--; if(cur<0) return; St[cur].pop (); } intTop () { while(St[cur].empty ()) Cur--; if(cur<0) return 0; Else returnSt[cur].top (); } BOOLempty () { while(cur>=0&&st[cur].empty ()) Cur--; returncur==-1; } BOOLFull () {returncur==capacity-1&&St[cur].full (); } voidPopat (intidx) { while(St[idx].empty ())--idx; St[idx].pop (); }};intMain () {Setofstacks ss1; for(intI=0; i<3*Ten+1; ++i) {ss1.push (i); } for(intI=0; i<Ten; ++i) {Ss1.popat (0); //Ss1.popat (1);Ss1.popat (2); } while(!Ss1.empty ()) {cout<<ss1.top () <<Endl; Ss1.pop (); } return 0;}
careercup-Stack and queue 3.3