实验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++实现循环队列的更多相关文章

  1. C++实现链队类——合肥工业大学数据结构实验5:链式队列

    实验5 5.1 实验目的 熟练掌握队列的顺序链式存储结构. 熟练掌握队列的有关算法设计,并在链队列上实现. 根据具体给定的需求,合理设计并实现相关结构和算法. 5.2 实验要求 5.2.1链队列实验要 ...

  2. TZOJ 数据结构实验--循环队列

    描述 创建一个循环队列,队列元素个数为4.能够实现队列的初始化.入队列.出队列.求队列长度等操作. 循环队列数据类型定义如下: typedef struct{ int data[Max];    in ...

  3. 数据结构算法C语言实现(十二)--- 3.4循环队列&队列的顺序表示和实现

    一.简述 空队列的处理方法:1.另设一个标志位以区别队列是空还是满:2.少用一个元素空间,约定以队列头指针在队尾指针下一位置上作为队列呈满的状态的标志. 二.头文件 //3_4_part1.h /** ...

  4. [置顶] ※数据结构※→☆线性表结构(queue)☆============循环队列 顺序存储结构(queue circular sequence)(十)

    循环队列 为充分利用向量空间,克服"假溢出"现象的方法是:将向量空间想象为一个首尾相接的圆环,并称这种向量为循环向量.存储在其中的队列称为循环队列(Circular Queue). ...

  5. JavaScript数据结构与算法(四) 循环队列的实现

    实现击鼓传花,需要用到上一章所述队列类Queue TypeScript方式实现源码 let hotPotato = (nameList, num) => { let queue = new Qu ...

  6. 【Java】 大话数据结构(7) 循环队列和链队列

    本文根据<大话数据结构>一书,实现了Java版的循环队列.链队列. 队列:只允许在一端进行插入操作,而在另一端进行删除操作的线性表. 1.循环队列 队列的顺序储存结构:用数组存储队列,引入 ...

  7. C语言数据结构-循环队列的实现-初始化、销毁、清空、长度、队列头元素、插入、删除、显示操作

    1.数据结构-循环队列的实现-C语言 #define MAXSIZE 100 //循环队列的存储结构 typedef struct { int* base; //基地址 int _front; //头 ...

  8. TypeScript算法与数据结构-队列和循环队列

    本文涉及的源码,均在我的github.有两部分队列和循环队列.有问题的可以提个issue,看到后第一时间回复 1. 队列(Queue) 队列也是一种线性的数据结构, 队列是一种先进先出的数据结构.类似 ...

  9. java数据结构——队列、循环队列(Queue)

    每天进步一点点,坚持就是成功. 1.队列 /** * 人无完人,如有bug,还请斧正 * 继续学习Java数据结构————队列(列队) * 队列和栈一样,都是使用数组,但是队列多了一个队头,队头访问数 ...

随机推荐

  1. JavaSE基础知识结构

  2. 接口测试_RESTClient基本使用

    火狐浏览器插件RESTClient基本使用. 消息头: Content-Type : application/x-www-form-urlencoded

  3. 基于python的request库,模拟登录csdn博客

    以前爬虫用urllib2来实现,也用过scrapy的爬虫框架,这次试试requests,刚开始用,用起来确实比urllib2好,封装的更好一些,使用起来简单方便很多. 安装requests库     ...

  4. 123 Best Time to Buy and Sell Stock III 买卖股票的最佳时机 III

    假设你有一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格.设计一个算法来找到最大的利润.你最多可以完成两笔交易.注意:你不可同时参与多笔交易(你必须在再次购买前出售掉之前的股票).详见: ...

  5. qconshanghai2015

    http://2015.qconshanghai.com/schedule 大会日程 2015年10月15日 星期四 08:30 开场致辞   地点 光大宴会厅 专题 主题演讲 数据分析与移动开发工具 ...

  6. calc() 计算CSS属性值

    calc()是css3的一个新增的功能,用来指定元素的长度.比如说,你可以使用calc()给元素的border.margin.pading.font-size和width等属性设置动态值.calc() ...

  7. STM32 modbus CRC16校验

    typedef unsigned char uint8_t; typedef unsigned short uint16_t; typedef int int32_t; const uint16_t ...

  8. JMeter进入接口压力测试

    关键字: Jmeter.单接口.压力测试.插件监听.服务器端 摘要: 使用Jmeter对单个接口进行压力测试:监听并发量对接口响应时间.服务器资源占量.Jmeter本身只能获取到Tomcat的状态,所 ...

  9. 【HEVC帧间预测论文】P1.6 A Fast HEVC Inter CU Selection Method Based on Pyramid Motion Divergence

    A Fast HEVC Inter CU Selection Method Based on Pyramid Motion Divergence <HEVC标准介绍.HEVC帧间预测论文笔记&g ...

  10. exit - 使程序正常中止

    SYNOPSIS 总览 #include <stdlib.h> void exit(int status); DESCRIPTION 描述 函数 exit() 使得程序正常中止,statu ...