加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
seqStack.cpp 1.76 KB
一键复制 编辑 原始数据 按行查看 历史
我不理解. 提交于 2023-04-01 19:17 . fix
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);
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化