C++ 堆栈
classseqstack//头文件{public:int*data;inttop;intmaxsize;public:seqstack(intmx){top=-1;ma...
class seqstack//头文件
{
public:
int *data;
int top;
int maxsize;
public:
seqstack(int mx)
{
top=-1;
maxsize=mx;
data=new int[maxsize];
}
bool isempty();//是否为空
void push(int value);//入栈
void pop();//出栈
int gettop();//取顶点元素
bool isfull();//是否已满
};
#include"iostream.h"
#include"stdlib.h"
bool seqstack::isempty()
{
if(top==-1)
return 1;
return 0;
}
bool seqstack::isfull()
{
if(top==maxsize-1)
return 1;
return 0;
}
void seqstack::push(int value)
{
if(seqstack::isfull())
{
cout<<"the stack is full"<<endl;
abort();
}
top++;
data[top]=value;
}
void seqstack::pop()
{
if(seqstack::isempty())
{
cout<<"the stack is empty"<<endl;
abort();
}
top--;
}
int seqstack::gettop()
{
if(isempty())
{
cout<<"the stack is full"<<endl;
abort();
}
int value=data[top];
return value;
}
#include"iostream.h"
#include"seqstack.h"
void main()
{
int mx,n;
int value;
cout<<"max size of the stack"<<endl;
cin>>mx;
seqstack s(mx);
cout<<s.isempty();
cout<<"push how many value"<<endl;
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"push value";
cin>>value;
s.push(value);
}
cout<<s.gettop()<<endl;
s.pop();
s.pop();
s.gettop();
s.pop();
s.pop();
s.pop();
s.pop();
cin>>value;
s.push(value);
} 展开
{
public:
int *data;
int top;
int maxsize;
public:
seqstack(int mx)
{
top=-1;
maxsize=mx;
data=new int[maxsize];
}
bool isempty();//是否为空
void push(int value);//入栈
void pop();//出栈
int gettop();//取顶点元素
bool isfull();//是否已满
};
#include"iostream.h"
#include"stdlib.h"
bool seqstack::isempty()
{
if(top==-1)
return 1;
return 0;
}
bool seqstack::isfull()
{
if(top==maxsize-1)
return 1;
return 0;
}
void seqstack::push(int value)
{
if(seqstack::isfull())
{
cout<<"the stack is full"<<endl;
abort();
}
top++;
data[top]=value;
}
void seqstack::pop()
{
if(seqstack::isempty())
{
cout<<"the stack is empty"<<endl;
abort();
}
top--;
}
int seqstack::gettop()
{
if(isempty())
{
cout<<"the stack is full"<<endl;
abort();
}
int value=data[top];
return value;
}
#include"iostream.h"
#include"seqstack.h"
void main()
{
int mx,n;
int value;
cout<<"max size of the stack"<<endl;
cin>>mx;
seqstack s(mx);
cout<<s.isempty();
cout<<"push how many value"<<endl;
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"push value";
cin>>value;
s.push(value);
}
cout<<s.gettop()<<endl;
s.pop();
s.pop();
s.gettop();
s.pop();
s.pop();
s.pop();
s.pop();
cin>>value;
s.push(value);
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询