数据结构实验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数据结构————队列(列队) * 队列和栈一样,都是使用数组,但是队列多了一个队头,队头访问数 ...
随机推荐
- _bzoj1257 [CQOI2007]余数之和sum【小技巧】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1257 最近刚做了一道莫比乌斯的题,需要用到这种方法. 应该让k / i相等的一连串k % i ...
- 题解报告:poj 1738 An old Stone Game(区间dp)
Description There is an old stone game.At the beginning of the game the player picks n(1<=n<=5 ...
- python函数基础(3)
第1章 编码补充 1.1 字符编码对照表 1.2 编码特性 1.4 encode/decode第2章 集合 2.1 特点 2.2 [重点]作用:去重 2.3 常用操作 2.3.1 删除 2.3.2 交 ...
- qconshanghai2016
http://2016.qconshanghai.com/schedule 大会日程 2016年10月20日 星期四 07:45 开始签到 09:00 开场致辞 专题 前端技术实践 主题演讲 业务上云 ...
- C# winform 创建快捷方式
using System;using IWshRuntimeLibrary;using System.IO; namespace UavSystem.Common{ public class S ...
- xcopy递归拷贝
递归拷贝 ::xcopy SOURCE_DIR DES_DIR\ /s SOURCE_DIR后面不需要加反斜杠
- 设置QtreeWidget水平滚动条
转载请注明出处:http://www.cnblogs.com/dachen408/p/7552603.html //设置treewidget水平滚动条 ui.treeWidget->header ...
- vijos 1053 Easy sssp
描述 输入数据给出一个有N(2 <= N <= 1,000)个节点,M(M <= 100,000)条边的带权有向图. 要求你写一个程序, 判断这个有向图中是否存在负权回路. 如果从一 ...
- MongoDB最简单的入门教程之二 使用nodejs访问MongoDB
在前一篇教程 MongoDB最简单的入门教程之一 环境搭建 里,我们已经完成了MongoDB的环境搭建. 在localhost:27017的服务器上,在数据库admin下面创建了一个名为person的 ...
- SpringBoot集成FastDFS+Nginx整合基于Token的防盗链
为什么要用SpringBoot? SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人 ...