一、实验目的
1、 熟练掌栈和队列的结构特点,掌握栈和队列的顺序存储和链式存储结构和实现。
2、学会使用栈和队列解决实际问题。
二、实验内容
1、自己确定结点的具体数据类型和问题规模:
分别建立一个顺序栈和链栈,实现栈的压栈和出栈操作。
分别建立一个顺序队列和链队列,实现队列的入队和出队操作。
2、设计算法并写出代码,实现一个十将二进制转换成2进制数。
3.设计一个模拟饭堂排队打饭管理软件,实现“先来先打饭”的排号叫号管理。
三、编码
1.顺序栈
#include
using namespace std;
const int MAX_SIZE=10;
class SeqStack{
private:
float data[MAX_SIZE];
int top;
public:
SeqStack();
void push(float x);
float pop();
float getTop();
int isEmpty();
};
SeqStack::SeqStack(){
top=-1;
}
void SeqStack::push(float x){ //top上移,把x存到top指向的位置
if(top==MAX_SIZE-1) throw"上溢!\n";
data[++top]=x;
}
float SeqStack::pop(){ //取出栈顶元素,把top下移
if(top==-1) throw"栈空!\n";
return data[top--];
}
float SeqStack::getTop(){ //返回top指向位置的元素
if(top==-1) throw"栈空!\n";
return data[top];
}
int SeqStack::isEmpty(){ //top
if(top==-1) return 1;
else return 0;
}
int main(){
try{
SeqStack s;
try{
cout<<"