Ch3-3: popat(int idx) of setofstack

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值