一.实验目的

巩固栈和队列数据结构,学会运用栈和队列。

1.回顾栈和队列的逻辑结构和受限操作特点,栈和队列的物理存储结构和常见操作。

2.学习运用栈和队列的知识来解决实际问题。

3.进一步巩固程序调试方法。

4.进一步巩固模板程序设计。

二.实验内容

1.自己选择顺序或链式存储结构,定义一个空栈类,并定义入栈、出栈、取栈元素基本操作。然后在主程序中对给定的N个数据进行验证,输出各个操作结果。

2.自己选择顺序或链式存储结构,定义一个空栈队列,并定义入栈、出栈、取栈元素基本操作。然后在主程序中对给定的N个数据进行验证,输出各个操作结果。

3.编程实现一个十进制数转换成二进制数。要求,要主程序中输出一个10进度数,输出其对应的2进制数序列。

前两题是必做题,第3题是选做题。

三.实验结果

实验1:

#include<iostream.h>
const int StackSize=10;

template<class DataType>
class SeqStack
{
public:
SeqStack(){top=-1;}
SeqStack(int a[],int n);
~SeqStack(){}
void Push(DataType x);
DataType Pop();
DataType GetTop(){if(top!=-1) return data[top];}
int Empty(){return (top==-1)? 1 : 0;}
private:
DataType data[StackSize];
int top;
};

template<class DataType>
SeqStack<DataType>::SeqStack(int a[],int n)
{
if(n>StackSize) throw"参数非法";
for(int i=0;i<n;i++)
data[i]=a[i];
top=n-1;
}

template<class DataType>
void SeqStack<DataType>::Push(DataType x)
{
if(top==StackSize-1) throw"上溢";
data[++top]=x;
}

template<class DataType>
DataType SeqStack<DataType>::Pop()
{
DataType x;
if(top==-1) throw"下溢";
x=data[top--];
return x;
}

void main()
{
int a[5]={2,3,5,7,11};
SeqStack<int> S(a,5);
if(S.Empty())
cout<<"栈为空"<<endl;
else
cout<<"栈非空"<<endl;
cout<<"对13执行入栈操作"<<endl;
S.Push(13);
cout<<"栈顶元素为:"<<endl;
cout<<S.GetTop()<<endl;
cout<<"执行一次出栈操作"<<endl;
S.Pop();
cout<<"栈顶元素为:"<<endl;
cout<<S.GetTop()<<endl;
}

实验2:

#include<iostream.h>

template<class DataType>
struct Node
{
DataType data;
Node<DataType> *next;
};

template<class DataType>
class LinkQueue
{
public:
LinkQueue();
~LinkQueue();
void EnQueue(DataType x);
DataType DeQueue();
DataType GetQueue();
int Empty();
private:
Node<DataType> *front,*rear;
};

template<class DataType>
LinkQueue<DataType>::LinkQueue()
{
Node<DataType> *s=NULL;
s=new Node<DataType>;
s->next=NULL;
front=rear=s;
}

template<class DataType>
LinkQueue<DataType>::~LinkQueue()

Node<DataType> *p=NULL;
while(front!=NULL)
{
p=front;
front=front->next;
delete p;
}
}

template<class DataType>
void LinkQueue<DataType>::EnQueue(DataType x)
{
Node<DataType> *s=NULL;
s=new Node<DataType>;
s->data=x;
s->next=NULL;
rear->next=s;rear=s;
}

template<class DataType>
DataType LinkQueue<DataType>::DeQueue()
{
Node<DataType> *p;
int x;
if(rear==front) throw"下溢";
p=front->next;x=p->data;
front->next=p->next;
if(p->next==NULL) rear=front;
delete p;
return x;
}

template<class DataType>
DataType LinkQueue<DataType>::GetQueue()
{
if(front!=rear)
return front->next->data;
}

template<class DataType>
int LinkQueue<DataType>::Empty()
{
if(front==rear)
return 1;
else
return 0;
}

void main()
{
LinkQueue<int> Q;
if(Q.Empty())
cout<<"队列为空"<<endl;
else cout<<"队列非空"<<endl;
cout<<"元素10和15执行入队操作!"<<endl;
Q.EnQueue(10);
Q.EnQueue(15);
cout<<"查看队头元素:";
cout<<Q.GetQueue()<<endl;
cout<<"执行出队操作!"<<endl;
Q.DeQueue();
cout<<"查看队头元素:"<<Q.GetQueue()<<endl;
}

三.实验总结

通过这一次的实验,我不仅实现了栈和队列的算法,还解决了上次作业遗留下来的问题。通过百度,我知道了“ ? : ”操作符是需要返回值的,所以问号后面的和冒号后面的都是需要一个表达式,而不是一个完整的语句。那么 top==NULL?return 1:return 0; 语句就应该改为 return (top==-1)?1:0; 语句,程序才得以运行成功。当然,除了这一种方法以外,还可以使用 if else语句,例子在本次实验的实验2中。

《数据结构》实验三:栈和队列实验 (实验报告)相关推荐

  1. 2019数据结构考研(三)------栈和队列

    栈和队列 知识架构 栈 栈的基本概念 栈的定义:栈是一种只允许在一端进行插入或者删除的线性表(后进先出的线性表) 栈顶:栈中允许插入和删除的一端 栈底:固定的 空栈 栈的顺序存储结构 栈的顺序表示 # ...

  2. 数据结构 实验三 栈的基本运算

    栈的基本运算 任务一: 顺序栈的基本操作 任务描述: 本关任务:实现顺序栈的基本操作,包括栈的初始化.置空栈.进栈.出栈.判栈空.栈的输出(遍历)等. 相关知识: 为了完成本关任务,你需要掌握: - ...

  3. SDUT-2449_数据结构实验之栈与队列十:走迷宫

    数据结构实验之栈与队列十:走迷宫 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 一个由n * m 个格子组成的迷宫,起 ...

  4. sdut 2088 数据结构实验之栈与队列十一:refresh的停车场

    数据结构实验之栈与队列十一:refresh的停车场 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Discuss Problem ...

  5. sdut 1479 数据结构实验之栈与队列九:行编辑器

    数据结构实验之栈与队列九:行编辑器 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Discuss Problem Descript ...

  6. sdut 3335 数据结构实验之栈与队列八:栈的基本操作

    数据结构实验之栈与队列八:栈的基本操作 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Discuss Problem Descri ...

  7. sdut 3333 数据结构实验之栈与队列六:下一较大值(二)

    数据结构实验之栈与队列六:下一较大值(二) Time Limit: 150MS Memory Limit: 8000KB Submit Statistic Discuss Problem Descri ...

  8. sdut-3332 数据结构实验之栈与队列五:下一较大值(一)

    数据结构实验之栈与队列五:下一较大值(一) Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Discuss Problem Desc ...

  9. sdut 2134 数据结构实验之栈与队列四:括号匹配

    数据结构实验之栈与队列四:括号匹配 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Discuss Problem Descript ...

  10. SDUT-2132_数据结构实验之栈与队列二:一般算术表达式转换成后缀式

    数据结构实验之栈与队列二:一般算术表达式转换成后缀式 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 对于一个基于二元运 ...

最新文章

  1. Python 安装selenium
  2. 儿童手工制作日历_怎么做手工儿童卡通绵羊日程管理小日历
  3. 清华学生计划表,大写的服!
  4. HTTP session的原理
  5. android qq毛玻璃,如何快速做出毛玻璃背景?有了这个网格渐变神器,1分钟搞定...
  6. Kotlin学习笔记八-数据代理类型,接口与抽象类
  7. 天朝理工学院SQL脚本
  8. Windows自带截屏-快捷键截图区域至剪切板
  9. 2021年安全员-C证(陕西省)考试资料及安全员-C证(陕西省)新版试题
  10. 特征码的使用办法_如何查询使用车架号查询车辆是否是事故
  11. 运行patsy 时报错 assert pytype not in (tokenize.NL, tokenize.NEWLINE)
  12. Code::Blocks之软件汉化
  13. 摔倒检测+yolov5
  14. 【无标题】一款功能非常强大的免费串口示波器串口助手,支持绘图,logo保存数据保存,历史数据加载与对比。
  15. tms320f2812启动流程
  16. pixhawk飞控架构
  17. vue-cli3.0中Tslint配置
  18. (含完整代码)简易Android计算器的实现
  19. Educational Codeforces Round 70 (Rated for Div. 2)
  20. Python全栈自动化测试--Python简介

热门文章

  1. Mysql常用的sql语句大全
  2. 《程序设计基础》 第五章 函数 6-6 字符金字塔 (15 分)
  3. vue+gin—— GetcharZp
  4. BookReader
  5. insmod: error inserting './scull.ko': -1 Unknown symbol in module
  6. AD学习记录03-规则
  7. 摄像头的像素如何计算
  8. ant design pro入门踩坑:删除页面文件报错
  9. 【XSY2538】/【HDU6155】Subsequence Count(矩阵乘法+线段树)
  10. [转载]Linux SWAP 交换分区配置说明