一..实验目的
巩固栈和队列数据结构,学会运用栈和队列。
1.回顾栈和队列的逻辑结构和受限操作特点,栈和队列的物理存储结构和常见操作。
2.学习运用栈和队列的知识来解决实际问题。
3.进一步巩固程序调试方法。
4.进一步巩固模板程序设计。
二.实验时间
准备时间为第5周到第6周,具体集中实验时间为6周第2次课。2个学时。
三..实验内容
1.自己选择顺序或链式存储结构,定义一个空栈类,并定义入栈、出栈、取栈元素基本操作。然后在主程序中对给定的N个数据进行验证,输出各个操作结果。
2.自己选择顺序或链式存储结构,定义一个空栈队列,并定义入栈、出栈、取栈元素基本操作。然后在主程序中对给定的N个数据进行验证,输出各个操作结果。3.编程实现一个十进制数转换成二进制数。要求,要主程序中输出一个10进度数,输出其对应的2进制数序列。
前两题是必做题,第3题是选做题。
实验教材P183到192
五.实验报告
1.在博客中先写上实习目的和内容,画出主要操作运算算法图,然后分别上传程序代码。插入调试关键结果截图。
2.写一个博文,比较总结栈和队列。
实验3.3
#include<iostream>
using namespace std;
struct Node
{
int data;
Node *next;
};
class linkstack
{
public:
linkstack()
{top=NULL;}
void push(int x);
int pop();
bool isempty()
{
if(top==NULL)
return 1;
else
return 0;
}
private:
Node *top;
Node *s;//临时结点
int x;//临时数据
};
void linkstack::push(int x)
{
s=new Node;
s->data=x;
s->next=top;
top=s;
}
int linkstack::pop()
{
if(top==NULL)
throw"下溢";
x=top->data;
s=top;
top=top->next;
delete s;
return x;
}
void main()
{
linkstack stack;
int decimal;//十进制数字
int remainder;//余数
cout<<"输入一个十进制数字:";
cin>>decimal;
while(decimal)
{
remainder=decimal%2;
stack.push(remainder);
decimal/=2;
}
cout<<"二进制数为:";
while(!stack.isempty())
cout<<stack.pop();
cout<<endl;
}