C语言 求栈的简单例子

来源:百度知道 编辑:UC知道 时间:2024/06/08 10:52:54
C语言 求栈的简单实例
1、要求编写含有指针和没有指针的简单代码
2、要求运用函数调用

注意:含有指针和没有指针的代码

#include"iostream.h"
const int maxsize=6;
class stack{
float data[maxsize];
int top;
public:
stack(void);
~stack(void);
void push(float a);
bool empty(void);
float pop(void);
};
stack::stack(void){
top=0;
cout<<"stack initialized."<<endl;
}
stack::~stack(void){
cout<<"stack destoryed"<<endl;
}
bool stack::empty(void){
return top==0;
}
void stack::push(float a){
if(top==maxsize){
cout<<"Stack overflow!"<<endl;
return ;
}
data[top]=a;
top++;
}
float stack::pop(void){
if(top==0){
cout<<"An empty stack!"<<endl;
return 0;
}
top--;
return data[top];
}
int main()
{
stack s1,s2;
float i=0.0;
for(;i<=maxsize;i++)
s1.push(2*i);
for(i=1;i<=max