热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

数据结构实验4:C++实现循环队列

实验44.1实验目的熟练掌握队列的顺序存储结构和链式存储结构。熟练掌握队列的有关算法设计,并在循环顺序队列和链队列上实现。根据具体给定的需求,合理设计并实现相关结构和算法。4.2

实验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 
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 <3) <" ";
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 <" 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 <" 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 <3) <" ";
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总结

注意细节!


推荐阅读
  • Linux环境变量函数getenv、putenv、setenv和unsetenv详解
    本文详细解释了Linux中的环境变量函数getenv、putenv、setenv和unsetenv的用法和功能。通过使用这些函数,可以获取、设置和删除环境变量的值。同时给出了相应的函数原型、参数说明和返回值。通过示例代码演示了如何使用getenv函数获取环境变量的值,并打印出来。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 开发笔记:实验7的文件读写操作
    本文介绍了使用C++的ofstream和ifstream类进行文件读写操作的方法,包括创建文件、写入文件和读取文件的过程。同时还介绍了如何判断文件是否成功打开和关闭文件的方法。通过本文的学习,读者可以了解如何在C++中进行文件读写操作。 ... [详细]
  • 本文介绍了Swing组件的用法,重点讲解了图标接口的定义和创建方法。图标接口用来将图标与各种组件相关联,可以是简单的绘画或使用磁盘上的GIF格式图像。文章详细介绍了图标接口的属性和绘制方法,并给出了一个菱形图标的实现示例。该示例可以配置图标的尺寸、颜色和填充状态。 ... [详细]
  • 本文详细介绍了GetModuleFileName函数的用法,该函数可以用于获取当前模块所在的路径,方便进行文件操作和读取配置信息。文章通过示例代码和详细的解释,帮助读者理解和使用该函数。同时,还提供了相关的API函数声明和说明。 ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • Java序列化对象传给PHP的方法及原理解析
    本文介绍了Java序列化对象传给PHP的方法及原理,包括Java对象传递的方式、序列化的方式、PHP中的序列化用法介绍、Java是否能反序列化PHP的数据、Java序列化的原理以及解决Java序列化中的问题。同时还解释了序列化的概念和作用,以及代码执行序列化所需要的权限。最后指出,序列化会将对象实例的所有字段都进行序列化,使得数据能够被表示为实例的序列化数据,但只有能够解释该格式的代码才能够确定数据的内容。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • 本文讨论了在Windows 8上安装gvim中插件时出现的错误加载问题。作者将EasyMotion插件放在了正确的位置,但加载时却出现了错误。作者提供了下载链接和之前放置插件的位置,并列出了出现的错误信息。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • eclipse学习(第三章:ssh中的Hibernate)——11.Hibernate的缓存(2级缓存,get和load)
    本文介绍了eclipse学习中的第三章内容,主要讲解了ssh中的Hibernate的缓存,包括2级缓存和get方法、load方法的区别。文章还涉及了项目实践和相关知识点的讲解。 ... [详细]
  • 计算机存储系统的层次结构及其优势
    本文介绍了计算机存储系统的层次结构,包括高速缓存、主存储器和辅助存储器三个层次。通过分层存储数据可以提高程序的执行效率。计算机存储系统的层次结构将各种不同存储容量、存取速度和价格的存储器有机组合成整体,形成可寻址存储空间比主存储器空间大得多的存储整体。由于辅助存储器容量大、价格低,使得整体存储系统的平均价格降低。同时,高速缓存的存取速度可以和CPU的工作速度相匹配,进一步提高程序执行效率。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
author-avatar
AD518最丶设计
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有