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计算方法
本篇文章给大家分享了关于JS函数中stack size计算方法的相关知识点,对此有需要的朋友可以跟着学习下。
numpy.stack()函数详解[项目代码]
本文详细解析了Python中numpy.stack()函数的使用方法和核心概念。作者通过定义四个2行3列的数组,展示了在不同axis参数下(axis=0、1、2)的堆叠效果,并提供了形象化的理解方式。文章还对比了numpy.stack()与numpy.concatenate()函数的区别,指出stack()会增加数组维度而concatenate()不会。最后,作者总结了数组和维度的抽象概念,帮助读者更好地理解该函数的实际应用场景,如图像处理中的多通道数据堆叠等。
C++ STL Stack用法详解[可运行源码]
本文详细介绍了C++ STL中stack容器的常用方法,包括stack的定义、元素访问方式以及常用函数如push()、top()、pop()、empty()和size()的使用实例。stack是一种后进先出(LIFO)的数据结构,常用于模拟递归算法以避免内存溢出等问题。文章通过代码示例展示了如何定义stack、访问栈顶元素、入栈、出栈以及检查栈是否为空和获取栈内元素个数等操作,为读者提供了实用的参考指南。
ndk-stack调试 cocos2d-x
ndk-stack调试.so库
Stack
Stack


新手乐园

