实验三 栈和队列
来源:互联网 发布:昆山华道数据地址 编辑:程序博客网 时间:2024/06/10 00:08
《数据结构》实验三: 栈和队列实验
一..实验目的
巩固栈和队列数据结构,学会运用栈和队列。
1.回顾栈和队列的逻辑结构和受限操作特点,栈和队列的物理存储结构和常见操作。
2.学习运用栈和队列的知识来解决实际问题。
3.进一步巩固程序调试方法。
4.进一步巩固模板程序设计。
二.实验时间
准备时间为第5周到第6周,具体集中实验时间为6周第2次课。2个学时。
三..实验内容
1.自己选择顺序或链式存储结构,定义一个空栈类,并定义入栈、出栈、取栈元素基本操作。然后在主程序中对给定的N个数据进行验证,输出各个操作结果。
头文件
# ifndef SeqStack_H
# define SeqStack_H
const int StackSize=10;
template<class DataType>
class SeqStack
{
public:
SeqStack();
~SeqStack(){}
void Push(DataType x);
DataType Pop();
DataType GetTop();
int Empty();
private:
DataType data[StackSize];
int top;
};
# endif
源程序文件
# include "Seqstack.h"
template<class DataType>
SeqStack<DataType>::SeqStack()
{
top=1;
}
template<class DataType>
void SeqStack<DataType>::Push(DataType x)
{
if(top==StackSize-1) throw"上溢";
top++;
data[top]=x;
}
template <class DataType>
DataType SeqStack<DataType>::Pop()
{
DataType x;
if(top==-1) throw"下溢";
x=data[top--];
return x;
}
template <class DataType>
DataType SeqStack<DataType>::GetTop()
{
if(top!=-1)
return data[top];
}
template <class DataType>
int SeqStack<DataType>::Empty()
{
if(top==-1) return 1;
else return 0;
}
主函数文件
# include <iostream>
using namespace std;
# include"SeqStack.cpp"
void main()
{
SeqStack<int>S;
if(S.Empty())
cout<<"栈为空"<<endl;
else
cout<<"栈非空"<<endl;
cout<<"对15和10执行入栈操作"<<endl;
S.Push(15);
S.Push(10);
cout<<"栈顶元素为:"<<endl;
cout<<S.GetTop()<<endl;
cout<<"执行一次出栈操作"<<endl;
S.Pop();
cout<<"栈顶元素:"<<endl;
cout<<S.GetTop()<<endl;
}
调试结果:
- 实验三 栈和队列
- 实验三:栈和队列
- 实验三栈和队列
- 实验三 栈和队列
- 实验三 栈和队列
- 【实验三】栈和队列
- 实验三 栈和队列
- 实验三-栈和队列
- 实验三: 栈和队列实验
- 《数据结构》实验三: 栈和队列实验
- 《数据结构》实验三: 栈和队列实验
- 《数据结构》实验三:栈和队列实验
- 《数据结构》实验三: 栈和队列实验
- 《数据结构》实验三: 栈和队列实验
- 《数据结构》实验三:栈和队列实验
- 数据结构:实验三 栈和队列实验
- 《数据结构》实验三: 栈和队列实验
- 实验三:栈和队列实验
- Gabor的OpenCV代码
- C++学习笔记(二)变量定义及初始化
- Nodejs系列-3基于Express+ejs的博客
- 《深入理解计算机系统》关于csapp.h和csapp.c的编译问题
- Ubuntu 13.04 关闭独立显卡
- 实验三 栈和队列
- 软件1班输出杨辉三角作业
- 详细介绍Scoket编程 包含最简单实现但客户端、多客户端多会话 、上传
- hibernate 多对多(many-to-many)
- linux之cal命令
- 存储器与寄存器有什么不同?
- 需求的层次
- 学习Unity3D第九天之数组和遍历器。
- 0-6岁宝宝发育指导Android