数据结构实验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 附源代码
1 // stdafx.h : include file for standard system include files, 2 // or project specific include files that are used frequently, but 3 // are changed infrequently 4 // 5 6 #if !defined(AFX_STDAFX_H__8FA49CDF_FC99_4984_AB37_46921F7ED357__INCLUDED_) 7 #define AFX_STDAFX_H__8FA49CDF_FC99_4984_AB37_46921F7ED357__INCLUDED_ 8 9 #if _MSC_VER > 1000 10 #pragma once 11 #endif // _MSC_VER > 1000 12 13 #include <stdc++.h> 14 15 using namespace std; 16 17 typedef int elementType; 18 typedef char elementType1; 19 const int maxn = 100; 20 21 // TODO: reference additional headers your program requires here 22 23 //{{AFX_INSERT_LOCATION}} 24 // Microsoft Visual C++ will insert additional declarations immediately before the previous line. 25 26 #endif // !defined(AFX_STDAFX_H__8FA49CDF_FC99_4984_AB37_46921F7ED357__INCLUDED_)
1 // _SeqCircleQueue.h: interface for the _SeqCircleQueue class. 2 // 3 // 4 5 #if !defined(AFX__SEQCIRCLEQUEUE_H__FCBC0603_27E1_4352_833C_6BED9B418B96__INCLUDED_) 6 #define AFX__SEQCIRCLEQUEUE_H__FCBC0603_27E1_4352_833C_6BED9B418B96__INCLUDED_ 7 8 #if _MSC_VER > 1000 9 #pragma once 10 #endif // _MSC_VER > 1000 11 12 class _SeqCircleQueue 13 { 14 public: 15 _SeqCircleQueue(); 16 virtual ~_SeqCircleQueue(); 17 bool emptySeqCircleQueue(); 18 bool fullSeqCircleQueue(); 19 bool enQueue( elementType value ); 20 bool deQueue( elementType &value ); 21 bool getFront( elementType &value ); 22 int length(); 23 void oddOrEven( elementType value ); 24 friend ostream &operator<<( ostream &os, _SeqCircleQueue &scq ) 25 { 26 if( ( scq._front - 1 ) % maxn == scq._rear ) 27 return os; 28 int column = 0; 29 for( int i = scq._front; i % maxn != scq._rear; i = ( i + 1 ) % maxn ) 30 { 31 os << setw(3) << setiosflags(ios::left) << scq.data[i] << " "; 32 column ++; 33 if( column % 10 == 0 ) 34 os << endl; 35 } 36 os << endl; 37 } 38 private: 39 elementType data[maxn]; 40 int _front; 41 int _rear; 42 43 }; 44 45 #endif // !defined(AFX__SEQCIRCLEQUEUE_H__FCBC0603_27E1_4352_833C_6BED9B418B96__INCLUDED_)
1 // _SeqCircleQueue.cpp: implementation of the _SeqCircleQueue class. 2 // 3 // 4 5 #include "stdafx.h" 6 #include "_SeqCircleQueue.h" 7 8 // 9 // Construction/Destruction 10 // 11 12 _SeqCircleQueue::_SeqCircleQueue() 13 { 14 _front = _rear = 0; 15 } 16 17 _SeqCircleQueue::~_SeqCircleQueue() 18 { 19 ios::sync_with_stdio(false); 20 cout << "The _SeqCircleQueue destruction has been called!" << endl; 21 } 22 23 bool _SeqCircleQueue::emptySeqCircleQueue() 24 { 25 return _front == _rear; 26 } 27 28 bool _SeqCircleQueue::fullSeqCircleQueue() 29 { 30 return ( _rear + 1 ) % maxn == _front; 31 } 32 33 bool _SeqCircleQueue::enQueue( elementType value ) 34 { 35 if( fullSeqCircleQueue() ) 36 { 37 cerr << "Seq-Circle-Queue is full!Error in _SeqCircleQueue::enQueue()!" << endl; 38 return false; 39 } 40 data[_rear] = value; 41 _rear = ( _rear + 1 ) % maxn; 42 return true; 43 } 44 45 bool _SeqCircleQueue::deQueue( elementType &value ) 46 { 47 if( emptySeqCircleQueue() ) 48 { 49 cerr << "Seq-Circle-Queue is empty!Error in _SeqCircleQueue::popFront()!" << endl; 50 return false; 51 } 52 value = data[_front]; 53 _front = ( _front + 1 ) % maxn; 54 return true; 55 } 56 57 bool _SeqCircleQueue::getFront( elementType &value ) 58 { 59 if( emptySeqCircleQueue() ) 60 { 61 cerr << "Seq-Circle-Queue is empty!Error in _SeqCircleQueue::getFront()!" << endl; 62 return false; 63 } 64 value = data[_front]; 65 return true; 66 } 67 68 int _SeqCircleQueue::length() 69 { 70 if( emptySeqCircleQueue() ) 71 { 72 cerr << "Seq-Circle-Queue is empty!Error in _SeqCircleQueue::length()!" << endl; 73 return -1; 74 } 75 return ( _rear - _front + maxn ) % maxn; 76 } 77 78 void _SeqCircleQueue::oddOrEven( elementType value ) 79 { 80 if( value & 1 ) 81 { 82 enQueue(value); 83 cout << value << " will be added to the queue!" << endl; 84 cout << (*this); 85 } 86 else if( !( value & 1) && value != 0 ) 87 { 88 elementType x; 89 deQueue(x); 90 cout << x << " has been deleted from the queue!" << endl; 91 cout << (*this); 92 } 93 else //if( value == 0 ) 94 { 95 cout << "The _SeqCircleQueue::oddOrEven() has been stoped!" << endl; 96 return; 97 } 98 }
1 // charSeqCircleQueue.h: interface for the charSeqCircleQueue class. 2 // 3 // 4 5 #if !defined(AFX_CHARSEQCIRCLEQUEUE_H__FBB4F8DD_2EF9_43A6_8E23_FD7E4C56908E__INCLUDED_) 6 #define AFX_CHARSEQCIRCLEQUEUE_H__FBB4F8DD_2EF9_43A6_8E23_FD7E4C56908E__INCLUDED_ 7 8 #if _MSC_VER > 1000 9 #pragma once 10 #endif // _MSC_VER > 1000 11 12 class charSeqCircleQueue 13 { 14 public: 15 charSeqCircleQueue(); 16 virtual ~charSeqCircleQueue(); 17 bool emptyCharSeqCircleQueue(); 18 bool fullCharSeqCircleQueue(); 19 bool enQueue( elementType1 value ); 20 bool deQueue( elementType1 &value ); 21 bool getFront( elementType1 &value ); 22 int length(); 23 friend ostream &operator<<( ostream &os, charSeqCircleQueue &cscq ) 24 { 25 if( ( cscq._front - 1 ) % maxn == cscq._rear ) 26 return os; 27 int column = 0; 28 for( int i = cscq._front; i % maxn != cscq._rear; i = ( i + 1 ) % maxn ) 29 { 30 os << setw(3) << setiosflags(ios::left) << cscq.data[i] << " "; 31 column ++; 32 if( column % 10 == 0 ) 33 os << endl; 34 } 35 os << endl; 36 } 37 private: 38 elementType1 data[maxn]; 39 int _front; 40 int _rear; 41 42 }; 43 44 #endif // !defined(AFX_CHARSEQCIRCLEQUEUE_H__FBB4F8DD_2EF9_43A6_8E23_FD7E4C56908E__INCLUDED_)
1 // charSeqCircleQueue.cpp: implementation of the charSeqCircleQueue class. 2 // 3 // 4 5 #include "stdafx.h" 6 #include "charSeqCircleQueue.h" 7 8 // 9 // Construction/Destruction 10 // 11 12 charSeqCircleQueue::charSeqCircleQueue() 13 { 14 _front = _rear = 0; 15 } 16 17 charSeqCircleQueue::~charSeqCircleQueue() 18 { 19 ios::sync_with_stdio(false); 20 cout << "The charSeqCircleQueue destruction has been called!" << endl; 21 } 22 23 bool charSeqCircleQueue::emptyCharSeqCircleQueue() 24 { 25 return _front == _rear; 26 } 27 28 bool charSeqCircleQueue::fullCharSeqCircleQueue() 29 { 30 return ( _rear + 1 ) % maxn == _front; 31 } 32 33 bool charSeqCircleQueue::enQueue( elementType1 value ) 34 { 35 if( fullCharSeqCircleQueue() ) 36 { 37 cerr << "Seq-Circle-Queue is full!Error in charSeqCircleQueue::::enQueue()!" << endl; 38 return false; 39 } 40 data[_rear] = value; 41 _rear = ( _rear + 1 ) % maxn; 42 return true; 43 } 44 45 bool charSeqCircleQueue::deQueue( elementType1 &value ) 46 { 47 if( emptyCharSeqCircleQueue() ) 48 { 49 cerr << "Seq-Circle-Queue is empty!Error in charSeqCircleQueue::popFront()!" << endl; 50 return false; 51 } 52 value = data[_front]; 53 _front = ( _front + 1 ) % maxn; 54 return true; 55 } 56 57 bool charSeqCircleQueue::getFront( elementType1 &value ) 58 { 59 if( emptyCharSeqCircleQueue() ) 60 { 61 cerr << "Seq-Circle-Queue is empty!Error in charSeqCircleQueue::::getFront()!" << endl; 62 return false; 63 } 64 value = data[_front]; 65 return true; 66 } 67 68 int charSeqCircleQueue::length() 69 { 70 if( emptyCharSeqCircleQueue() ) 71 { 72 cerr << "Seq-Circle-Queue is empty!Error in charSeqCircleQueue::::length()!" << endl; 73 return -1; 74 } 75 return ( _rear - _front + maxn ) % maxn; 76 }
4.7 调试过程中出现的bug总结
注意细节!
转载于:https://www.cnblogs.com/25th-engineer/p/9940885.html
数据结构实验4:C++实现循环队列相关推荐
- 数据结构实验报告二 栈和队列
一.实验目的 1.掌握栈的结构特性及其入栈,出栈操作: 2.掌握队列的结构特性及其入队.出队的操作,掌握循环队列的特点及其操作. 二.实验内容和要求 1.阅读下面程序,将函数Push和函数Pop补充完 ...
- 数据结构实验报告3————栈和队列及其应用
实验内容 (1)栈的定义.基本操作的实现,以及典型应用 ①编程实现顺序栈/链栈的基本操作(如:栈的初始化.判断栈空.求栈的长度.取栈顶.入栈.出栈等),并设计菜单调用.(必做) ②利用栈结构,实现将任 ...
- 2021 - 9 -下旬 数据结构- 线性表 -双端循环队列 - java实现
//循环双端队列:Circle Double Ended Queue //本质是对动态数组的优化 //队头队尾都可以添加或删除元素 //相比于普通循环队列需要注意的点是在队头插入元素时的对front前 ...
- 数据结构笔记(十)-- 循环队列
队列的顺序表示和实现 一.循环队列概述 循环队列 是把顺序队列首尾相连,把存储队列元素的表从逻辑上看成一个环,成为循环队列. 队头指针(front) 指向队列的队头元素. 队尾指针(rear) 指向队 ...
- 20200120 数据结构和算法之 数组循环队列的实现
数组循环队列针对数据量不大的情况下使用,可以快速地实现元素的入队和出队.入队和出队遵循先进先出(FIFO)的原则.结构体组成如下: typedef int datatype; typedef stru ...
- 【数据结构与算法 2】循环队列
一.队列 队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表. 进行插入操作的端称为队尾,进 ...
- [数据结构-严蔚敏版]P64循环队列-队列的顺序存储结构
代码如下: #include <iostream> using namespace std;const int MAXQSIZE = 10;typedef int ElemType;typ ...
- 【数据结构与算法】设计循环队列
文章目录
- 【swjtu】数据结构实验3_基于循环队列的排队买票模拟程序
实验内容及要求: 编程建立循环队列存储结构,对排队买票过程进行模拟.要求程序在控制台屏幕上显示字符菜单: 1. 排队--输入新到达的买票人姓名,加入买票队列中: 2. 售票--排队队列中最前面的人购票 ...
- 循环队列–C语言实现–数据结构
循环队列–C语言实现–数据结构 目录 循环队列C语言实现数据结构 目录 一 要求 二 循环队列 三 循环队列的算法设计 1 建立循环队列 2 置空队列 3 入队 4 出队 5 打印队 四 程序 1 程 ...
最新文章
- Windows server下部署php环境
- 虚拟机ping不通开发板如何解决
- DevExpress的TreeList怎样给树节点设置图标
- 教你一招如何使用几行代码实现zookeeper作为springcloud的服务注册中心
- 【Unity3D技巧】一个简单的Unity-UI框架的实现
- Codeforces - 65D - Harry Potter and the Sorting Hat - 简单搜索
- vue 给iframe设置src_vue组件中使用iframe元素
- ​不容错过的 13 个 JavaScript 实用技巧!
- dos从优盘启动计算机,制作U盘dos启动盘的五大步骤
- UEFI shell - 标准应用程序的编译和加载过程
- PHP 二元线性拟合函数
- 微信小程序之扫普通链接二维码打开小程序实现动态传递参数及踩坑总结
- 欧洲赢麻了!互联网巨头被迫拆围墙;git常用命令速查;Diffusion扩散模型实例教程集;高效C++机器学习库;前沿论文 | ShowMeAI资讯日报
- Emacs_HotKey
- matlab中单刀双掷开关,单刀双掷开关与双刀双掷开关的区别
- 由I2C data信号低电平不到0,再思考I2C及GPIO
- SOLIDWORKS Electrical无缝集成电气和机械设计
- C#中在鼠标经过Button控件时显示提示信息(弹出气泡提示框)
- c语言is_int(),C程序设计英文试题
- Java抽象类方法和抽象类
热门文章
- Ubuntu 16.04 64位安装arm-linux-gcc交叉编译器以及samba服务器
- 逐行分析Hadoop的HelloWorld
- 跨界创立PayPal、特斯拉、SpaceX……,埃隆·马斯克是这样“掌控”知识的
- MYSQL注入天书之order by后的injection
- 对模型方差和偏差的解释之一:过拟合
- MySQL事件调度器(Event Scheduler)介绍
- 【数据挖掘笔记一】引论
- oracle可视化工具_零代码玩转数据可视化
- Create an Apex class that returns contacts based on incoming parameters
- JavaScript 技术篇-js正则表达式匹配字符串左右两边是否包含空格