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;
}