代码拉取完成,页面将自动刷新
template <typename T>
class seqStack
{
public:
seqStack(int size = 10) : size_(size), top_(0), stack_(new T[size]) {}
seqStack(seqStack<T>& stack)
{
this->size_ = stack.size_;
this->top_ = stack.top_;
delete[] this->stack_;
this->stack_ = new T[this->size_];
for (int i = 0; i < this->size_; i++)
{
stack_[i] = stack.stack_[i];
}
}
seqStack<T> &operator=(seqStack<T> stack)
{
if (this == stack)
{
return *this;
}
this->size_ = stack.size_;
this->top_ = stack.top_;
delete[] this->stack_;
this->stack_ = new T[this->size_];
for (int i = 0; i < this->size_; i++)
{
stack_[i] = stack.stack_[i];
}
}
bool full()
{
return this->size_ == this->top_;
}
bool empty()
{
return this->top_ == 0;
}
T top()
{
return this->stack_[this->top_ - 1];
}
void pop()
{
if(this->empty()) {
return;
}
this->top_--;
}
void push(const T &val)
{
if (this->full())
{
this->expend();
}
this->stack_[this->top_++] = val;
}
private:
void expend()
{
this->size_ *= 2;
T *tmp = new T[this->size_];
for (int i = 0; i < this->top_; i++)
{
tmp[i] = this->stack_[i];
}
delete[] stack_;
this->stack_ = tmp;
}
T *stack_;
int top_;
int size_;
};
int main() {
seqStack<int> s1(5);
s1.push(1);
s1.push(2);
s1.push(2);
s1.push(2);
s1.push(2);
s1.push(2);
s1.push(2);
s1.push(2);
s1.push(2);
s1.push(2);
s1.push(2);
s1.push(2);
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。