Continue to use class to design solution, also, start to use STL, such as container (vector, stack, set, ...). save time and to be more professional.
The design without popat method is similar to the one to devide a single array into 3 stacks.
But the class of setofstack with popat should be more carefully.
for example:
bool empty(){ // empty of the whole set
//if(cur==0) return st[cur].empty();
// this is not working because it might have
// blank substack in the middle of full stack
// so need to have while condition to find the last
// not-empty stack
// if (cur!=-1 && st[cur].empty()) --cur; --->
// this one only find the previous stack of
// empty stack, need to use while condition instead
while(cur!=-1 && st[cur].empty()) --cur;
if (cur==-1) return true;
return false;
}
Full code. Still, this is modified from Hawstein's code but may not the best solution, learn from others too..
// solution to Ch 3-3
// Ch3-3: popAt(int idx) should be careful in designing the
// corner conditions, such as the empty method of setofstack
// (with popat, due to the condition of empty substack in
// middle, need to use while condition instead of if condition)
//
#include
using namespace std;
const int STACK_SIZE = 10;
const int STACK_NUM = 5;
class stack{
public:
stack(int size=STACK_SIZE){
buf = new int[size];
ptop = -1;
this->capacity = size;
}
~stack(){
delete[] buf;
}
void push(int val){
buf[++ptop] = val;
//cout << "push" << endl;
}
void pop(){
--ptop;
//cout << "pop" << endl;
}
int top(){
return buf[ptop];
cout << "T" << endl;
}
bool empty(){
return ptop==-1;
cout << "E" << endl;
}
bool full(){
return ptop==capacity-1;
cout << "F" << endl;
}
private:
int* buf;
int ptop;
int capacity;
};
class SetOfStacks{//without popAt()
public:
SetOfStacks(int size = STACK_NUM){
st = new stack[size];
cur = 0;
this->capacity = size;
}
~SetOfStacks(){
delete[] st;
}
void push(int val){
if(st[cur].full()) ++cur;
st[cur].push(val);
}
void pop(){
if(st[cur].empty()) --cur;
st[cur].pop();
}
int top(){
if(st[cur].empty()) --cur;
return st[cur].top();
}
bool empty(){ // empty of the whole set
if(cur==0) return st[cur].empty();
return false;
}
bool full(){
if(cur==capacity-1) return st[cur].full();
return false;
}
private:
stack *st;
int cur;
int capacity;
};
class SetOfStacks1{//with popAt()
public:
SetOfStacks1(int size = STACK_NUM){
st = new stack[size];
cur = 0;
this->capacity = size;
}
~SetOfStacks1(){
delete[] st;
}
void push(int val){
if(st[cur].full()) ++cur;
st[cur].push(val);
}
void pop(){
while(st[cur].empty()) --cur;
st[cur].pop();
}
/*
void popAt(int idx){ // should not pop if that stack is empty
if(~st[idx].empty()) cout << "no popat" <
// this one only find the previous stack of
// empty stack, need to use while condition instead
while(cur!=-1 && st[cur].empty()) --cur;
if (cur==-1) return true;
return false;
}
bool full(){
if(cur==capacity-1) return st[cur].full();
return false;
}
private:
stack *st;
int cur;
int capacity;
};
int main(){
#if 0
SetOfStacks ss0;
for(int i=0; i
output:
Executing the program....
$demo
19
18
17
16
15
14
13
12
11
10
huxiaolin ...TOT