Stack 函数问题

Mr.chan ?? ?? Mr.chan ?? ?? 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
如果判断堆栈是否满了。下面的是我代码,还有就是“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;
}


...全文

相关推荐

评论 2
收藏 已收藏
分享
返回顶部
Mr.chan ?? ??
新手乐园 33,315 成员 41,769 内容