热门标签 | 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总结

注意细节!


推荐阅读
  • 超级简单加解密工具的方案和功能
    本文介绍了一个超级简单的加解密工具的方案和功能。该工具可以读取文件头,并根据特定长度进行加密,加密后将加密部分写入源文件。同时,该工具也支持解密操作。加密和解密过程是可逆的。本文还提到了一些相关的功能和使用方法,并给出了Python代码示例。 ... [详细]
  • 欢乐的票圈重构之旅——RecyclerView的头尾布局增加
    项目重构的Git地址:https:github.comrazerdpFriendCircletreemain-dev项目同步更新的文集:http:www.jianshu.comno ... [详细]
  • STL迭代器的种类及其功能介绍
    本文介绍了标准模板库(STL)定义的五种迭代器的种类和功能。通过图表展示了这几种迭代器之间的关系,并详细描述了各个迭代器的功能和使用方法。其中,输入迭代器用于从容器中读取元素,输出迭代器用于向容器中写入元素,正向迭代器是输入迭代器和输出迭代器的组合。本文的目的是帮助读者更好地理解STL迭代器的使用方法和特点。 ... [详细]
  • 本文由编程笔记#小编为大家整理,主要介绍了logistic回归(线性和非线性)相关的知识,包括线性logistic回归的代码和数据集的分布情况。希望对你有一定的参考价值。 ... [详细]
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • Commit1ced2a7433ea8937a1b260ea65d708f32ca7c95eintroduceda+Clonetraitboundtom ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文介绍了PE文件结构中的导出表的解析方法,包括获取区段头表、遍历查找所在的区段等步骤。通过该方法可以准确地解析PE文件中的导出表信息。 ... [详细]
  • WebSocket与Socket.io的理解
    WebSocketprotocol是HTML5一种新的协议。它的最大特点就是,服务器可以主动向客户端推送信息,客户端也可以主动向服务器发送信息,是真正的双向平等对话,属于服务器推送 ... [详细]
  • 重入锁(ReentrantLock)学习及实现原理
    本文介绍了重入锁(ReentrantLock)的学习及实现原理。在学习synchronized的基础上,重入锁提供了更多的灵活性和功能。文章详细介绍了重入锁的特性、使用方法和实现原理,并提供了类图和测试代码供读者参考。重入锁支持重入和公平与非公平两种实现方式,通过对比和分析,读者可以更好地理解和应用重入锁。 ... [详细]
  • 本文介绍了Codeforces Round #321 (Div. 2)比赛中的问题Kefa and Dishes,通过状压和spfa算法解决了这个问题。给定一个有向图,求在不超过m步的情况下,能获得的最大权值和。点不能重复走。文章详细介绍了问题的题意、解题思路和代码实现。 ... [详细]
  • 本文介绍了在Android开发中使用软引用和弱引用的应用。如果一个对象只具有软引用,那么只有在内存不够的情况下才会被回收,可以用来实现内存敏感的高速缓存;而如果一个对象只具有弱引用,不管内存是否足够,都会被垃圾回收器回收。软引用和弱引用还可以与引用队列联合使用,当被引用的对象被回收时,会将引用加入到关联的引用队列中。软引用和弱引用的根本区别在于生命周期的长短,弱引用的对象可能随时被回收,而软引用的对象只有在内存不够时才会被回收。 ... [详细]
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社区 版权所有