Stack 函数问题
如果判断堆栈是否满了。下面的是我代码,还有就是“stack is full”这句话为什么不管怎么样都会打印出来。请教大神。
#include <iostream>
#include "iomanip"
#include<string>
#include <cmath>
using namespace std;
class Stack{
private:
char *arr;
int top;
int capacity;
int size;
public:
Stack(){
int size = 5;
arr = new char[size];
top = -1;
capacity = size;
}
bool isEmpty()
{
if(top == -1)
return true;
return false;
}
void push(char line)
{
if(top == size - 1)
cout<<"Stack is full"<<endl;
arr[++top] = line;
}
void pop()
{
if(!isEmpty())
top--;
}
char peek()
{
if(!isEmpty())
return arr[top];
return '0';
}
};
int main(){
string line;
Stack stack;
cout<<"Enter a string: "<<endl;
cin>>line;
for(int i = 0; i<line.length();i++)
{
stack.push(line[i]);
}
while(!stack.isEmpty())
{
cout<<stack.peek();
stack.pop();
}
return 0;
}
#include <iostream>
#include "iomanip"
#include<string>
#include <cmath>
using namespace std;
class Stack{
private:
char *arr;
int top;
int capacity;
int size;
public:
Stack(){
int size = 5;
arr = new char[size];
top = -1;
capacity = size;
}
bool isEmpty()
{
if(top == -1)
return true;
return false;
}
void push(char line)
{
if(top == size - 1)
cout<<"Stack is full"<<endl;
arr[++top] = line;
}
void pop()
{
if(!isEmpty())
top--;
}
char peek()
{
if(!isEmpty())
return arr[top];
return '0';
}
};
int main(){
string line;
Stack stack;
cout<<"Enter a string: "<<endl;
cin>>line;
for(int i = 0; i<line.length();i++)
{
stack.push(line[i]);
}
while(!stack.isEmpty())
{
cout<<stack.peek();
stack.pop();
}
return 0;
}
...全文
相关推荐
详解JS函数stack size计算方法
堆栈溢出"通常是指调用栈(call stack)的大小超过了虚拟机或运行时环境所允许的最大大小。为了解决这类问题,有必要了解JS中函数的堆栈大小(stack size)是如何计算的。
numpy.stack()函数详解[项目代码]
为了加深读者的理解,作者还探讨了数组和维度的抽象概念,并且以图像处理中的多通道数据堆叠为例,说明了numpy.stack()函数在处理类似问题时的应用场景。
C++ STL Stack用法详解[可运行源码]
C++ STL中的stack容器是处理LIFO问题的强大工具,它简洁的接口和高效的操作使得它成为编程中处理类似问题的首选。
ndk-stack调试 cocos2d-x
ndk-stack 的优点使用 ndk-stack 有很多优点,例如:* 快速定位问题:ndk-stack 可以快速将崩溃的日志信息转换成可读的调用栈信息,从而帮助开发者快速定位问题。
Stack
了解并熟练掌握栈这一数据结构及其在Java中的实现,对于提升编程技能和解决复杂问题有着至关重要的作用。通过不断地实践和学习,可以更好地运用栈解决实际问题。


新手乐园
