/*写一个类栈类,要求完成进栈与出栈*/
#include<iostream>
using namespace std;
const int MAX=5; //假定栈中最多保存5个数据
//定义名为stack的类,其具有栈功能
class stack {
//数据成员
float num[MAX]; //存放栈数据的数组
int top; //指示栈顶位置的变量
public:
//成员函数
void init(void) { top=0; } //初始化函数
void push(float x) //入栈函数
{
if (top==MAX){
cout<<"Stack is full !"<<endl;
return;
};
num[top]=x;
top++;
}
float pop(void) //出栈函数
{
top--;
if (top<0){
cout<<"Stack is underflow !"<<endl;
return 0;
};
return num[top];
}
}
//以下是main()函数,其用stack类创建栈对象,并使用了这些对象
main(void)
{
//声明变量和对象
int i;
float x;
stack a,b; //声明(创建)栈对象
//以下对栈对象初始化
a.init();
b
C++写一个简单的堆栈类
最新推荐文章于 2020-03-27 22:41:09 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)