Gstack广义栈界面
来源:百度文库 编辑:神马文学网 时间:2024/04/27 17:11:06
广义栈不同于普通栈
Gstack广义栈界面:
template
struct Gstack {
int top_; //当前元素个数;
T* data_; //元素类型的指针;(迭代子)
int const cpct_; //容量;
Gstack(int cpcty) //构造函数,给广义栈一个容量;
: cpct_(cpcty), top_(0), data_(new T [cpcty]) { }
~Gstack(void) { delete[ ] data_;} //析构函数;
int capacity(void) const { return cpct_;} //返回广义栈的容量;
int size(void) const { return top_;} //返回当前元素个数;
bool full(void) const { return top_ == cpct_;} //判满函数;
bool empty(void) const { return top_ == 0;} //判空函数;
T const& top(void) const { return data_[top_ - 1]; } //返回对栈顶元素的引用;
T& top(void) { return data_[top_ - 1];} //返回对栈顶元素的引用;
void push(T const& v) { data_[top_++] = v;} //压栈;
void pop(void) { --top_; } //弹栈;
void clear(void) { top_ = 0; } //清空广义栈;
T& operator[](int h) { return data_[h]; } //返回对第h+1个元素的引用;
T const& operator[](int h) const //返回对第h+1个元素的引用;
{ return data_[h]; }
};
广义栈不需要向量或链表做底层容器,可以返回对
指定元素的引用,但容量的预定义;
Gstack广义栈界面:
template
struct Gstack {
int top_; //当前元素个数;
T* data_; //元素类型的指针;(迭代子)
int const cpct_; //容量;
Gstack(int cpcty) //构造函数,给广义栈一个容量;
: cpct_(cpcty), top_(0), data_(new T [cpcty]) { }
~Gstack(void) { delete[ ] data_;} //析构函数;
int capacity(void) const { return cpct_;} //返回广义栈的容量;
int size(void) const { return top_;} //返回当前元素个数;
bool full(void) const { return top_ == cpct_;} //判满函数;
bool empty(void) const { return top_ == 0;} //判空函数;
T const& top(void) const { return data_[top_ - 1]; } //返回对栈顶元素的引用;
T& top(void) { return data_[top_ - 1];} //返回对栈顶元素的引用;
void push(T const& v) { data_[top_++] = v;} //压栈;
void pop(void) { --top_; } //弹栈;
void clear(void) { top_ = 0; } //清空广义栈;
T& operator[](int h) { return data_[h]; } //返回对第h+1个元素的引用;
T const& operator[](int h) const //返回对第h+1个元素的引用;
{ return data_[h]; }
};
广义栈不需要向量或链表做底层容器,可以返回对
指定元素的引用,但容量的预定义;