【发布时间】:2020-10-14 10:46:56
【问题描述】:
我想实现一个字符堆栈实现,但我认为它有问题,因为当我尝试将它用于我的其他功能时,它没有单词和库堆栈工作。你能帮忙找出问题吗:
using namespace std;
Stack::Stack(int size)
{
arr = new char[size];
capacity = size;
t = -1;
}
int Stack::size()
{
return (t + 1);
}
Stack::~Stack()
{
delete[] arr;
}
bool Stack::empty()
{
return size()==0;
}
void Stack::push(char x)
{
if (size()==capacity) {
cout<<"Push to full stack";
arr[++t]=x;
}
}
char Stack::pop()
{
if (empty()) {
cout<<"Pop from empty stack";
--t;
}
return 0;
}
char Stack::top()
{
if (!empty())
return arr[t];
else
cout<<"Top of the stack is empty";
return 0;
}
我想实现一个字符堆栈实现,但我认为它有问题,因为当我尝试将它用于我的其他功能时,它没有单词和库堆栈工作。你能帮忙找出一个问题吗: 提前谢谢!
【问题讨论】:
-
首先“它不起作用”并不是对您的问题的一个很好的描述。 如何它不工作?当你做某事时,应该发生什么?实际发生了什么?请花一些时间阅读the help pages,阅读SO tour,阅读How to Ask,以及this question checklist。还可以了解如何edit 您的问题以改进它们,例如添加minimal reproducible example。
-
作为一个可能的提示,请在您的
pop函数中执行一些rubber duck debugging(并密切注意您的块和范围)。
标签: c++ stack implementation