CareerCup-3.1
来源:互联网 发布:004淘宝店 编辑:程序博客网 时间:2024/05/16 15:06
Describe how you could use a single array to implement three stacks
Approach 1:
#include <iostream>using namespace std;#define SIZE 60#define PERSIZE SIZE/3typedef int Data;class Stack{public: Stack(int type):mType(type) { if(type < 0 || type > 2) { throw "Error Stack Type"; } else { tag = mType * PERSIZE - 1; } }; void push(Data d) { if(tag >= (mType + 1) * PERSIZE - 1) { throw "Out of Size"; } else { data[++tag] = d; } }; void pop() { if(tag < mType * PERSIZE) { throw "Empty Stack"; } else { tag --; } }; Data peek() { if(tag < mType * PERSIZE) { throw "Empty Stack"; } else { return data[tag]; } }; bool isEmpty(){return tag >= mType * PERSIZE;};private: static Data data[SIZE]; int mType; int tag;};Data Stack::data[SIZE];
Approach 2:
#include <iostream>using namespace std;#define SIZE 60typedef int Data;struct Node { Data data; int next; };class Stack{public: Stack(int type):mType(type) { if(type < 0 || type > 2) { throw "Error Stack Type"; } }; void push(Data d) { if(tag >= SIZE-1) { throw "Stack Full"; } data[++tag].data = d; if(top[mType] < 0) data[tag].next = -1; else data[tag].next = top[mType]; top[mType] = tag; }; void pop() { if(top[mType] < 0) { throw "Empty Stack"; } top[mType] = data[top[mType]].next; tag--; }; Data peek() { if(top[mType] < 0) { throw "Empty Stack"; } else { return data[top[mType]].data; } }; bool isEmpty(){return top[mType] >= 0;};private: static Node data[SIZE]; int mType; static int top[3]; static int tag;};Node Stack::data[SIZE];int Stack::tag = -1;int Stack::top[3] = {-1, -1, -1};
- CareerCup-3.1
- CareerCup 3.1
- CareerCup 1.3
- CareerCup 1.4
- CareerCup 5.7
- CareerCup 14.6
- CareerCup 1.1
- CareerCup 1.2
- CareerCup 1.3
- CareerCup 1.4
- CareerCup 1.5
- CareerCup 1.6
- CareerCup 1.7
- CareerCup 1.8
- CareerCup 2.1
- CareerCup 2.2
- CareerCup 2.3
- CareerCup 2.4
- HttpClient容易忽视的细节——连接关闭
- 关于HTML5令人激动的10项预测
- 没有找到一未命名文件
- 编译fedora kernel
- 串行化(Serialization)
- CareerCup-3.1
- pivot行列转换
- 交通灯管理系统
- 在WP8开发中的使用MVVM模式
- 切分和组合图片(二)
- poj 1416 Find The Multiple 同余模定理
- Web Service基础(WSDL、SOAP)
- 银行业务调度系统
- sqlite的系统表sqlite_master