数据结构实验4:C++实现循环队列
实验4
4.1 实验目的
熟练掌握队列的顺序存储结构和链式存储结构。
熟练掌握队列的有关算法设计,并在循环顺序队列和链队列上实现。
根据具体给定的需求,合理设计并实现相关结构和算法。
4.2 实验要求
4.2.1 循环顺序队列的实验要求
循环顺序队列结构和运算定义,算法的实现以库文件方式实现,不得在测试主程序中直接实现;
实验程序有较好可读性,各运算和变量的命名直观易懂,符合软件工程要求;
程序有适当的注释。
4.3 实验任务
4.3.1 循环顺序队列实验任务
编写算法实现下列问题的求解。
<1>初始化一个队列。
<2>判断是否队空。
<3>判断是否队满。
设队列最大长度:MaxLen=100
第一组数据:入队n个元素,判断队满
第二组数据:用循环方式将1到99,99个元素入队,判队满
<4>入队
第一组数据:4,7,8,12,20,50
第二组数据:a,b,c,d,f,g
<5>出队
<6>取队头元素
<7>求当前队列中元素个数
<8>编写算法实现
①初始化空循环队列;
②当键盘输入奇数时,此奇数入队;
③当键盘输入偶数时,队头出队;
④当键盘输入0时,算法退出;
⑤每当键盘输入后,输出当前队列中的所有元素。
4.5 运行结果截图及说明

图1 测试(1)、(2)、(3)、(5)、(6)、(7)

图2 测试(4)

图3 测试(4)

图4 测试(8)
4.6 附源代码
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
// #if !defined(AFX_STDAFX_H__8FA49CDF_FC99_4984_AB37_46921F7ED357__INCLUDED_)
#define AFX_STDAFX_H__8FA49CDF_FC99_4984_AB37_46921F7ED357__INCLUDED_ #if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000 #include <stdc++.h> using namespace std; typedef int elementType;
typedef char elementType1;
const int maxn = ; // TODO: reference additional headers your program requires here //{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif // !defined(AFX_STDAFX_H__8FA49CDF_FC99_4984_AB37_46921F7ED357__INCLUDED_)
// _SeqCircleQueue.h: interface for the _SeqCircleQueue class.
//
////////////////////////////////////////////////////////////////////// #if !defined(AFX__SEQCIRCLEQUEUE_H__FCBC0603_27E1_4352_833C_6BED9B418B96__INCLUDED_)
#define AFX__SEQCIRCLEQUEUE_H__FCBC0603_27E1_4352_833C_6BED9B418B96__INCLUDED_ #if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000 class _SeqCircleQueue
{
public:
_SeqCircleQueue();
virtual ~_SeqCircleQueue();
bool emptySeqCircleQueue();
bool fullSeqCircleQueue();
bool enQueue( elementType value );
bool deQueue( elementType &value );
bool getFront( elementType &value );
int length();
void oddOrEven( elementType value );
friend ostream &operator<<( ostream &os, _SeqCircleQueue &scq )
{
if( ( scq._front - ) % maxn == scq._rear )
return os;
int column = ;
for( int i = scq._front; i % maxn != scq._rear; i = ( i + ) % maxn )
{
os << setw() << setiosflags(ios::left) << scq.data[i] << " ";
column ++;
if( column % == )
os << endl;
}
os << endl;
}
private:
elementType data[maxn];
int _front;
int _rear; }; #endif // !defined(AFX__SEQCIRCLEQUEUE_H__FCBC0603_27E1_4352_833C_6BED9B418B96__INCLUDED_)
// _SeqCircleQueue.cpp: implementation of the _SeqCircleQueue class.
//
////////////////////////////////////////////////////////////////////// #include "stdafx.h"
#include "_SeqCircleQueue.h" //////////////////////////////////////////////////////////////////////
// Construction/Destruction
////////////////////////////////////////////////////////////////////// _SeqCircleQueue::_SeqCircleQueue()
{
_front = _rear = ;
} _SeqCircleQueue::~_SeqCircleQueue()
{
ios::sync_with_stdio(false);
cout << "The _SeqCircleQueue destruction has been called!" << endl;
} bool _SeqCircleQueue::emptySeqCircleQueue()
{
return _front == _rear;
} bool _SeqCircleQueue::fullSeqCircleQueue()
{
return ( _rear + ) % maxn == _front;
} bool _SeqCircleQueue::enQueue( elementType value )
{
if( fullSeqCircleQueue() )
{
cerr << "Seq-Circle-Queue is full!Error in _SeqCircleQueue::enQueue()!" << endl;
return false;
}
data[_rear] = value;
_rear = ( _rear + ) % maxn;
return true;
} bool _SeqCircleQueue::deQueue( elementType &value )
{
if( emptySeqCircleQueue() )
{
cerr << "Seq-Circle-Queue is empty!Error in _SeqCircleQueue::popFront()!" << endl;
return false;
}
value = data[_front];
_front = ( _front + ) % maxn;
return true;
} bool _SeqCircleQueue::getFront( elementType &value )
{
if( emptySeqCircleQueue() )
{
cerr << "Seq-Circle-Queue is empty!Error in _SeqCircleQueue::getFront()!" << endl;
return false;
}
value = data[_front];
return true;
} int _SeqCircleQueue::length()
{
if( emptySeqCircleQueue() )
{
cerr << "Seq-Circle-Queue is empty!Error in _SeqCircleQueue::length()!" << endl;
return -;
}
return ( _rear - _front + maxn ) % maxn;
} void _SeqCircleQueue::oddOrEven( elementType value )
{
if( value & )
{
enQueue(value);
cout << value << " will be added to the queue!" << endl;
cout << (*this);
}
else if( !( value & ) && value != )
{
elementType x;
deQueue(x);
cout << x << " has been deleted from the queue!" << endl;
cout << (*this);
}
else //if( value == 0 )
{
cout << "The _SeqCircleQueue::oddOrEven() has been stoped!" << endl;
return;
}
}
// charSeqCircleQueue.h: interface for the charSeqCircleQueue class.
//
////////////////////////////////////////////////////////////////////// #if !defined(AFX_CHARSEQCIRCLEQUEUE_H__FBB4F8DD_2EF9_43A6_8E23_FD7E4C56908E__INCLUDED_)
#define AFX_CHARSEQCIRCLEQUEUE_H__FBB4F8DD_2EF9_43A6_8E23_FD7E4C56908E__INCLUDED_ #if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000 class charSeqCircleQueue
{
public:
charSeqCircleQueue();
virtual ~charSeqCircleQueue();
bool emptyCharSeqCircleQueue();
bool fullCharSeqCircleQueue();
bool enQueue( elementType1 value );
bool deQueue( elementType1 &value );
bool getFront( elementType1 &value );
int length();
friend ostream &operator<<( ostream &os, charSeqCircleQueue &cscq )
{
if( ( cscq._front - ) % maxn == cscq._rear )
return os;
int column = ;
for( int i = cscq._front; i % maxn != cscq._rear; i = ( i + ) % maxn )
{
os << setw() << setiosflags(ios::left) << cscq.data[i] << " ";
column ++;
if( column % == )
os << endl;
}
os << endl;
}
private:
elementType1 data[maxn];
int _front;
int _rear; }; #endif // !defined(AFX_CHARSEQCIRCLEQUEUE_H__FBB4F8DD_2EF9_43A6_8E23_FD7E4C56908E__INCLUDED_)
// charSeqCircleQueue.cpp: implementation of the charSeqCircleQueue class.
//
////////////////////////////////////////////////////////////////////// #include "stdafx.h"
#include "charSeqCircleQueue.h" //////////////////////////////////////////////////////////////////////
// Construction/Destruction
////////////////////////////////////////////////////////////////////// charSeqCircleQueue::charSeqCircleQueue()
{
_front = _rear = ;
} charSeqCircleQueue::~charSeqCircleQueue()
{
ios::sync_with_stdio(false);
cout << "The charSeqCircleQueue destruction has been called!" << endl;
} bool charSeqCircleQueue::emptyCharSeqCircleQueue()
{
return _front == _rear;
} bool charSeqCircleQueue::fullCharSeqCircleQueue()
{
return ( _rear + ) % maxn == _front;
} bool charSeqCircleQueue::enQueue( elementType1 value )
{
if( fullCharSeqCircleQueue() )
{
cerr << "Seq-Circle-Queue is full!Error in charSeqCircleQueue::::enQueue()!" << endl;
return false;
}
data[_rear] = value;
_rear = ( _rear + ) % maxn;
return true;
} bool charSeqCircleQueue::deQueue( elementType1 &value )
{
if( emptyCharSeqCircleQueue() )
{
cerr << "Seq-Circle-Queue is empty!Error in charSeqCircleQueue::popFront()!" << endl;
return false;
}
value = data[_front];
_front = ( _front + ) % maxn;
return true;
} bool charSeqCircleQueue::getFront( elementType1 &value )
{
if( emptyCharSeqCircleQueue() )
{
cerr << "Seq-Circle-Queue is empty!Error in charSeqCircleQueue::::getFront()!" << endl;
return false;
}
value = data[_front];
return true;
} int charSeqCircleQueue::length()
{
if( emptyCharSeqCircleQueue() )
{
cerr << "Seq-Circle-Queue is empty!Error in charSeqCircleQueue::::length()!" << endl;
return -;
}
return ( _rear - _front + maxn ) % maxn;
}
4.7 调试过程中出现的bug总结
注意细节!
数据结构实验4:C++实现循环队列的更多相关文章
- C++实现链队类——合肥工业大学数据结构实验5:链式队列
实验5 5.1 实验目的 熟练掌握队列的顺序链式存储结构. 熟练掌握队列的有关算法设计,并在链队列上实现. 根据具体给定的需求,合理设计并实现相关结构和算法. 5.2 实验要求 5.2.1链队列实验要 ...
- TZOJ 数据结构实验--循环队列
描述 创建一个循环队列,队列元素个数为4.能够实现队列的初始化.入队列.出队列.求队列长度等操作. 循环队列数据类型定义如下: typedef struct{ int data[Max]; in ...
- 数据结构算法C语言实现(十二)--- 3.4循环队列&队列的顺序表示和实现
一.简述 空队列的处理方法:1.另设一个标志位以区别队列是空还是满:2.少用一个元素空间,约定以队列头指针在队尾指针下一位置上作为队列呈满的状态的标志. 二.头文件 //3_4_part1.h /** ...
- [置顶] ※数据结构※→☆线性表结构(queue)☆============循环队列 顺序存储结构(queue circular sequence)(十)
循环队列 为充分利用向量空间,克服"假溢出"现象的方法是:将向量空间想象为一个首尾相接的圆环,并称这种向量为循环向量.存储在其中的队列称为循环队列(Circular Queue). ...
- JavaScript数据结构与算法(四) 循环队列的实现
实现击鼓传花,需要用到上一章所述队列类Queue TypeScript方式实现源码 let hotPotato = (nameList, num) => { let queue = new Qu ...
- 【Java】 大话数据结构(7) 循环队列和链队列
本文根据<大话数据结构>一书,实现了Java版的循环队列.链队列. 队列:只允许在一端进行插入操作,而在另一端进行删除操作的线性表. 1.循环队列 队列的顺序储存结构:用数组存储队列,引入 ...
- C语言数据结构-循环队列的实现-初始化、销毁、清空、长度、队列头元素、插入、删除、显示操作
1.数据结构-循环队列的实现-C语言 #define MAXSIZE 100 //循环队列的存储结构 typedef struct { int* base; //基地址 int _front; //头 ...
- TypeScript算法与数据结构-队列和循环队列
本文涉及的源码,均在我的github.有两部分队列和循环队列.有问题的可以提个issue,看到后第一时间回复 1. 队列(Queue) 队列也是一种线性的数据结构, 队列是一种先进先出的数据结构.类似 ...
- java数据结构——队列、循环队列(Queue)
每天进步一点点,坚持就是成功. 1.队列 /** * 人无完人,如有bug,还请斧正 * 继续学习Java数据结构————队列(列队) * 队列和栈一样,都是使用数组,但是队列多了一个队头,队头访问数 ...
随机推荐
- php.ini配置文件位置
laravel之今天遇到个意想不到的问题: 我在测试文件上传,大于2M的文件时候hasFile() 方法报错,这一定是文件大小限制.接下来就跳坑了 1.首先查找php.ini的位置,就用find / ...
- HBase文档操作--练习篇
1.查询学生的所有信息 数据准备 var persons = [{ name:"jim", age:25, email:"75431457@qq.com", c ...
- 单机版solr6.3和分布式solr6.3的安装部署
一.单机版的solr部署 我的是在windows下安装的,linux同理 1. 安装JDK8,并配置好环境变量,一般我们经常开发的电脑上应该都有JDk了,所以这一步可以忽略. 2. 解压solr6.3 ...
- 关于发布WP 8.1应用信息不匹配问题的解决办法
错误提示: 与此更新关联的程序包标识符与已上传程序包中的标识符不匹配: The package identity associated with this update doesn't match ...
- [BZOJ1053][SDOI2005]反素数ant 数学
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1053 假设这个最大的反素数为$x$,那么$1<p<x$中数的因子数都没有$x$ ...
- P3371 【模板】单源最短路径
题目描述 如题,给出一个有向图,请输出从某一点出发到所有点的最短路径长度. 输入输出格式 输入格式: 第一行包含三个整数N.M.S,分别表示点的个数.有向边的个数.出发点的编号. 接下来M行每行包含三 ...
- grep的几个参数
-a 在二进制问就爱你中,以文本方式进行搜索 -c 计算找到搜索字符串的次数 -i 忽略大小写 -n 输出行号 -v 反向选择,即没有显示搜索字符串内容的那一行 grep -n '\.$' file ...
- 修改JRE system library
MyEclipse 默认的情况下JRE system library 是:MyEclipse 的,如何修改工程中的JRE system library呢?步骤如下: 1.选择工程->Proper ...
- qt5.5.1+vs2010发送邮件
最近用到了这个功能,用于验证登陆~为此在网上找了好久,发现这方面的问答并不多!唯独这篇的解答实在.原文传送 1.首先选定自己的发送方的邮箱,无论是163还是qq邮箱等,首先都得开通SMTP服务:以16 ...
- 纯手写的css3正方体旋转效果
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...