基于数组的堆栈实现
Stack implementation based on arrays
这是我的代码,但是有一个我无法理解的问题:每次当我在堆栈中push一个整数时,堆栈的其余部分(未填充的部分)由0
值填充。请解释一下我的错误。
#include <iostream>
using namespace std;
class Stack
{
private:
int* base;
int max_size;
int top;
public:
Stack(int size = 100)
{
base = new int[size];
max_size = size;
top = -1;
}
~Stack()
{
delete[] base;
}
bool empty() const
{
return top == -1;
}
bool full() const
{
return top == max_size - 1;
}
void push(int element)
{
if (top == max_size-1) {
cout<< "Stack overflow" << endl;
return;
}
base[++top] = element;
}
void pop()
{
if(top == -1) {
cout << "Stack underflow" << endl;
return;
}
top--;
}
int & read_top() const
{
return base[top];
}
void print()
{
int i = 0 ;
cout << "Stack is ";
while(i <= max_size)
{
cout << base[i] <<" ";
i++;
}
cout << endl;
}
};
int main()
{
Stack first(10);
for(int i = 0; i<=5; i++)
{
first.push(i*i);
first.print();
}
return 0;
}
考虑:
Stack s(10);
for(int i = 0; i<=5; i++)
s.push(i);
在print
中你有:
std::cout << "Stack is ";
while(i <= max_size)
std::cout << base[i] << ' ';
在上面的例子中,s.max_size
是10,所以你要打印base[0,10)的值,其中5个是未初始化的。因为应用程序还没有对内存做太多的处理,所以当你执行分配时,你很可能会得到一个原始的(零)内存块。
你的代码可能的变化:
#include <iostream>
// using namespace std ; // unlearn the habbit of doing this.
#include <memory> // for unique_ptr
class Stack
{
// note: class is private by default.
// unique_ptr will automatically free memory for us.
using ptr_t = std::unique_ptr<int[]>;
ptr_t base_;
int* top_;
int* end_;
public :
Stack(int size=100)
: base_(new int[size])
, top_(base_.get())
, end_(top_ + size)
{}
bool empty() const
{ return top_ == base_.get(); }
bool full() const
{ return top_ == end_; }
// don't print errors from a library
bool push(int value)
{
if (full()) // written in terms of our own members
return false;
*(top_++) = value;
return true;
}
bool pop()
{
if (empty()) // written in terms of our own members
return false;
--top_;
return true;
}
const int& read_top() const // const ref so it's not modifiable
{ return *(top_-1); }
int size() const { return end_ - top_; }
int capacity() const { return end_ - base_.get(); }
std::ostream& print(std::ostream& to)
{
to << "Stack is ";
for (int* ptr = base_.get(); ptr != top_; ++ptr)
to << *ptr << ' ';
to << 'n';
return to;
}
// or we could do this:
friend std::ostream& operator << (std::ostream& to, const Stack& s)
{
to << '[';
for (int* ptr = s.base_.get(); ptr != s.top_; ++ptr) {
to << *ptr << ',';
}
return to << ']';
}
};
int main()
{
Stack s(5);
s.push(1);
s.push(2);
s.push(3);
s.print(std::cout);
std::cout << "stack is: " << s << 'n';
}
实时演示:http://ideone.com/JsA4EU
我不太确定我得到你的问题是正确的:你的意思是"每次当我推一个整数在我的堆栈"?据我所知,您初始化堆栈一次,然后进行一些推入,而不改变中间的底层数组。这里发生的事情是,你的数组被初始化为0(不要指望你的编译器每次都这样做,但这次它会这样做-很可能你在编译时没有优化),然后你添加单个元素,正如有人已经回答的那样,你不只是打印堆栈(即从0到顶),而是整个底层数组。如果编译优化,您可能会得到垃圾值而不是零(数组没有在构造函数中初始化为一些初始值,并且未赋值的可能包含任何值)。希望能有所帮助。
代码中的一个bug:在print()
方法中,您应该有while(i < max_size)
相关文章:
- 使用模板进行堆栈实现; "name followed by :: must be a class or namespace"
- 了解使用堆栈实现队列的递归调用机制
- 为堆栈实现泛型集合
- 未分配被释放的指针(将堆栈实现为链表时)
- 在C++中使用链表的堆栈实现中,访问结构体headNode成员count和top会导致运行时错误
- 河内塔C++堆栈实现
- 线程安全堆栈实现
- 使用导致分段错误的类C++中的堆栈实现
- C++堆栈实现弹出函数不起作用
- 我的矩阵堆栈实现(OpenGL ES 2.0)有什么问题
- 堆栈实现C
- C 堆栈实现意外输出
- 指针数组堆栈实现
- C++ 堆栈实现硬件错误
- C++堆栈实现 - 两个版本的 top 方法
- 无法理解堆栈实现的isEmpty函数中的返回语句
- 在编写堆栈实现 c++ 时遇到问题
- 使用堆栈实现C++
- 测试《毁灭战士3》堆栈实现时堆栈损坏
- 使用 3 个堆栈实现排序.(分段错误 - 核心转储)