要求定义和实现栈类Stack,并有相应的测试程序
答案:2 悬赏:0
解决时间 2021-02-22 02:03
- 提问者网友:曖昧情执
- 2021-02-21 07:04
要求定义和实现栈类Stack,并有相应的测试程序
最佳答案
- 二级知识专家网友:陪我到地狱流浪
- 2021-02-21 07:52
//stack.h
#ifndef STACK_H_
#define STACK_H_
class Stack
{
private:
int top;
enum {MAXLEN = 10};
int array[MAXLEN];
public:
Stack(){top = 0;}
bool empty();
bool full();
bool pop(int & e);
bool push(int e);;
};
#endif
//stack.cpp
#include "Stack.h"
bool Stack::empty()
{
if(top == 0)
return 1;
else
return 0;
};
bool Stack::full()
{
if(top == MAXLEN)
return 1;
else
return 0;
}
bool Stack::pop(int & e)
{
if(empty()) return 0;
--top;
e = array[top];
return 1;
}
bool Stack::push(int e)
{
if(full()) return 0;
array[top] = e;
top++;
return 1;
}
//usestack.cpp
#include "iostream"
#include "Stack.h"
using namespace std;
int main()
{
Stack st;
st.push(1);
st.push(2);
int a = 0;
st.pop(a);
cout << "a = " << a << endl;
st.pop(a);
cout << "a = " << a << endl;
}
#ifndef STACK_H_
#define STACK_H_
class Stack
{
private:
int top;
enum {MAXLEN = 10};
int array[MAXLEN];
public:
Stack(){top = 0;}
bool empty();
bool full();
bool pop(int & e);
bool push(int e);;
};
#endif
//stack.cpp
#include "Stack.h"
bool Stack::empty()
{
if(top == 0)
return 1;
else
return 0;
};
bool Stack::full()
{
if(top == MAXLEN)
return 1;
else
return 0;
}
bool Stack::pop(int & e)
{
if(empty()) return 0;
--top;
e = array[top];
return 1;
}
bool Stack::push(int e)
{
if(full()) return 0;
array[top] = e;
top++;
return 1;
}
//usestack.cpp
#include "iostream"
#include "Stack.h"
using namespace std;
int main()
{
Stack st;
st.push(1);
st.push(2);
int a = 0;
st.pop(a);
cout << "a = " << a << endl;
st.pop(a);
cout << "a = " << a << endl;
}
全部回答
- 1楼网友:我叫很个性
- 2021-02-21 08:38
你说呢...
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯